Skip to content

Commit f133923

Browse files
committed
handle non-long results in round(x, none)
1 parent bd86342 commit f133923

File tree

1 file changed

+14
-3
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/floats

1 file changed

+14
-3
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/floats/FloatBuiltins.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -954,16 +954,22 @@ private static double op(double x, long n) {
954954
}
955955

956956
@Specialization
957-
long round(double x, @SuppressWarnings("unused") PNone none,
957+
Object round(double x, @SuppressWarnings("unused") PNone none,
958958
@Cached("createBinaryProfile()") ConditionProfile nanProfile,
959-
@Cached("createBinaryProfile()") ConditionProfile infProfile) {
959+
@Cached("createBinaryProfile()") ConditionProfile infProfile,
960+
@Cached("createBinaryProfile()") ConditionProfile isLongProfile) {
960961
if (nanProfile.profile(Double.isNaN(x))) {
961962
throw raise(PythonErrorType.ValueError, ErrorMessages.CANNOT_CONVERT_S_TO_INT, "float NaN");
962963
}
963964
if (infProfile.profile(Double.isInfinite(x))) {
964965
throw raise(PythonErrorType.OverflowError, ErrorMessages.CANNOT_CONVERT_S_TO_INT, "float infinity");
965966
}
966-
return (long) round(x, 0);
967+
double result = round(x, 0);
968+
if (isLongProfile.profile(result > Long.MAX_VALUE || result < Long.MIN_VALUE)) {
969+
return factory().createInt(toBigInteger(result));
970+
} else {
971+
return (long) result;
972+
}
967973
}
968974

969975
@Fallback
@@ -974,6 +980,11 @@ long round(double x, @SuppressWarnings("unused") PNone none,
974980
throw raise(PythonErrorType.TypeError, ErrorMessages.DESCRIPTOR_REQUIRES_OBJ, "__round__", "float", x);
975981
}
976982
}
983+
984+
@TruffleBoundary
985+
private static BigInteger toBigInteger(double d) {
986+
return BigDecimal.valueOf(d).toBigInteger();
987+
}
977988
}
978989

979990
@Builtin(name = __EQ__, minNumOfPositionalArgs = 2)

0 commit comments

Comments
 (0)