Skip to content

Commit ad91f94

Browse files
committed
HHH-19749 Use binary for float and decimal digit precision for numeric types
1 parent cec4263 commit ad91f94

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DoubleJavaType.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import org.hibernate.type.descriptor.jdbc.JdbcType;
1616
import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators;
1717

18+
import static java.lang.Math.ceil;
19+
import static java.lang.Math.log;
20+
1821
/**
1922
* Descriptor for {@link Double} handling.
2023
*
@@ -23,6 +26,8 @@
2326
public class DoubleJavaType extends AbstractClassJavaType<Double> implements
2427
PrimitiveJavaType<Double> {
2528
public static final DoubleJavaType INSTANCE = new DoubleJavaType();
29+
//needed for converting precision from binary to decimal digits
30+
private static final double LOG_BASE10OF2 = log(2)/log(10);
2631

2732
public DoubleJavaType() {
2833
super( Double.class );
@@ -151,9 +156,14 @@ public long getDefaultSqlLength(Dialect dialect, JdbcType jdbcType) {
151156

152157
@Override
153158
public int getDefaultSqlPrecision(Dialect dialect, JdbcType jdbcType) {
154-
//this is the number of *binary* digits
155-
//in a double-precision FP number
156-
return dialect.getDoublePrecision();
159+
if ( jdbcType.isFloat() ) {
160+
//this is the number of *binary* digits
161+
//in a double-precision FP number
162+
return dialect.getDoublePrecision();
163+
}
164+
else {
165+
return Math.min( dialect.getDefaultDecimalPrecision(), (int) ceil( dialect.getDoublePrecision() * LOG_BASE10OF2 ) );
166+
}
157167
}
158168

159169

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/FloatJavaType.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515
import org.hibernate.type.descriptor.jdbc.JdbcType;
1616
import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators;
1717

18+
import static java.lang.Math.ceil;
19+
import static java.lang.Math.log;
20+
1821
/**
1922
* Descriptor for {@link Float} handling.
2023
*
2124
* @author Steve Ebersole
2225
*/
2326
public class FloatJavaType extends AbstractClassJavaType<Float> implements PrimitiveJavaType<Float> {
2427
public static final FloatJavaType INSTANCE = new FloatJavaType();
28+
//needed for converting precision from binary to decimal digits
29+
private static final double LOG_BASE10OF2 = log(2)/log(10);
2530

2631
public FloatJavaType() {
2732
super( Float.class );
@@ -149,9 +154,14 @@ public long getDefaultSqlLength(Dialect dialect, JdbcType jdbcType) {
149154

150155
@Override
151156
public int getDefaultSqlPrecision(Dialect dialect, JdbcType jdbcType) {
152-
//this is the number of *binary* digits
153-
//in a single-precision FP number
154-
return dialect.getFloatPrecision();
157+
if ( jdbcType.isFloat() ) {
158+
//this is the number of *binary* digits
159+
//in a single-precision FP number
160+
return dialect.getFloatPrecision();
161+
}
162+
else {
163+
return Math.min( dialect.getDefaultDecimalPrecision(), (int) ceil( dialect.getFloatPrecision() * LOG_BASE10OF2 ) );
164+
}
155165
}
156166

157167
@Override

0 commit comments

Comments
 (0)