Skip to content

Commit 8095455

Browse files
committed
Fix parsing floats with very large exponents
1 parent 88ee4e7 commit 8095455

File tree

2 files changed

+9
-2
lines changed
  • graalpython
    • com.oracle.graal.python.test/src/tests
    • com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/floats

2 files changed

+9
-2
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_float.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -281,6 +281,9 @@ def test_compare_infinite(self):
281281
self.assertFalse(num < ninf)
282282
self.assertFalse(num <= ninf)
283283

284+
def test_too_large_exponent(self):
285+
self.assertEqual(float(9.6652344473471409e+13020482957104346813665225475455405808065502685256002111130525326263065544707062128277904331411231482727389186536355106830667631510992459549990606158658919), float('inf'))
286+
284287

285288
fromHex = float.fromhex
286289

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,11 @@ public static StringToDoubleResult stringToDouble(String str, int start, int len
239239
public static double parseValidString(String substr) {
240240
double d = Double.parseDouble(substr);
241241
if (!Double.isFinite(d)) {
242-
d = new BigDecimal(substr).doubleValue();
242+
try {
243+
d = new BigDecimal(substr).doubleValue();
244+
} catch (NumberFormatException e) {
245+
// Can happen when the number is too large, just let it return +-infinity then
246+
}
243247
}
244248
return d;
245249
}

0 commit comments

Comments
 (0)