Skip to content

Commit e70f990

Browse files
committed
[GR-23337] Make test_strtod pass
PullRequest: graalpython/1389
2 parents 09a3546 + aac7062 commit e70f990

File tree

2 files changed

+13
-4
lines changed
  • graalpython
    • com.oracle.graal.python.test/src/tests/unittest_tags
    • com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/floats

2 files changed

+13
-4
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_strtod.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*graalpython.lib-python.3.test.test_strtod.StrtodTests.test_bigcomp
22
*graalpython.lib-python.3.test.test_strtod.StrtodTests.test_boundaries
33
*graalpython.lib-python.3.test.test_strtod.StrtodTests.test_halfway_cases
4+
*graalpython.lib-python.3.test.test_strtod.StrtodTests.test_large_exponents
45
*graalpython.lib-python.3.test.test_strtod.StrtodTests.test_oversized_digit_strings
56
*graalpython.lib-python.3.test.test_strtod.StrtodTests.test_parsing
67
*graalpython.lib-python.3.test.test_strtod.StrtodTests.test_particular

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.floats;
4242

43+
import java.math.BigDecimal;
44+
4345
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4446
import com.oracle.truffle.api.CompilerDirectives.ValueType;
4547

@@ -66,7 +68,7 @@ private static boolean isAsciiSpace(char c) {
6668
* followed by a digit and removes them. Does not create a copy if the original String does not
6769
* need any cleanup. Combines _PyUnicode_TransformDecimalAndSpaceToASCII and
6870
* _Py_string_to_number_with_underscores.
69-
*
71+
*
7072
* @param src the String to transform
7173
* @return the transformed String, {@code src} if the input does not need cleanup or
7274
* {@code null} if there are invalid underscores or unicode characters other than
@@ -145,7 +147,7 @@ public StringToDoubleResult(double value, int position) {
145147
* </ul>
146148
* Implements PyOS_string_to_double and _PyOS_ascii_strtod except error handling and handling of
147149
* locale-specific decimal point.
148-
*
150+
*
149151
* @param str the string to parse
150152
* @param start starting position in the string
151153
* @param len length of the string
@@ -217,9 +219,15 @@ public static StringToDoubleResult stringToDouble(String str, int start, int len
217219
return null;
218220
}
219221
try {
220-
return new StringToDoubleResult(Double.parseDouble(str.substring(start, i)), i);
222+
String substr = str.substring(start, i);
223+
double d = Double.parseDouble(substr);
224+
if (!Double.isFinite(d)) {
225+
d = new BigDecimal(substr).doubleValue();
226+
}
227+
return new StringToDoubleResult(d, i);
221228
} catch (NumberFormatException e) {
222-
// Should not happen since the input to Double.parseDouble should be correct
229+
// Should not happen since the input to Double.parseDouble() / BigDecimal(String) should
230+
// be correct
223231
return null;
224232
}
225233
}

0 commit comments

Comments
 (0)