Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,13 @@ public static double round(@SqlType(StandardTypes.DOUBLE) double num, @SqlType(S
if (rescaledRound != Long.MAX_VALUE) {
return sign * (rescaledRound / factor);
}
if (Double.isInfinite(rescaled)) {
// num has max 17 precisions, so to make round actually do something, decimals must be smaller than 17.
// then factor must be smaller than 10^17
// then in order for rescaled to be greater than Double.MAX_VALUE, num must be greater than 1.8E291 with many trailing zeros
// in which case, rounding is no op anyway
return num;
}
return sign * DoubleMath.roundToBigInteger(rescaled, RoundingMode.HALF_UP).doubleValue() / factor;
}

Expand All @@ -858,6 +865,11 @@ public static long roundReal(@SqlType(StandardTypes.REAL) long num, @SqlType(Sta
if (rescaledRound != Long.MAX_VALUE) {
result = sign * (rescaledRound / factor);
}
else if (Double.isInfinite(rescaled)) {
// numInFloat is max at 3.4028235e+38f, to make rescale greater than Double.MAX_VALUE, decimals must be greater than 270
// but numInFloat has max 8 precision, so rounding is no op
return num;
}
else {
result = sign * (DoubleMath.roundToBigInteger(rescaled, RoundingMode.HALF_UP).doubleValue() / factor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,13 @@ public void testRound()
assertThat(assertions.function("round", "DOUBLE '3000.1234567890123456789'", "16"))
.isEqualTo(3000.1234567890124);

// 1.8E292*10^16 is infinity.
assertThat(assertions.function("round", "DOUBLE '1.8E292'", "16"))
.isEqualTo(1.8E292);

assertThat(assertions.function("round", "DOUBLE '-1.8E292'", "16"))
.isEqualTo(-1.8E292);

assertThat(assertions.function("round", "TINYINT '3'", "TINYINT '1'"))
.isEqualTo((byte) 3);

Expand Down Expand Up @@ -1702,6 +1709,13 @@ public void testRound()
assertThat(assertions.function("round", "REAL '3000.1234567890123456789'", "16"))
.isEqualTo(3000.1235f);

// 3.4028235e+38 * 10 ^ 271 is infinity
assertThat(assertions.function("round", "REAL '3.4028235e+38'", "271"))
.isEqualTo(3.4028235e+38f);

assertThat(assertions.function("round", "REAL '-3.4028235e+38'", "271"))
.isEqualTo(-3.4028235e+38f);

assertThat(assertions.function("round", "3", "1"))
.isEqualTo(3);

Expand Down