Skip to content

Commit e744d36

Browse files
committed
[GR-71537] Fix float.is_integer for big integers.
PullRequest: graalpython/4119
2 parents d67c31d + a11a24d commit e744d36

File tree

2 files changed

+28
-16
lines changed
  • graalpython

2 files changed

+28
-16
lines changed

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2025, 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
@@ -51,6 +51,18 @@
5151

5252
class BasicTests(unittest.TestCase):
5353

54+
def test_is_integer(self):
55+
assert not (3.14).is_integer()
56+
assert not (-3.14).is_integer()
57+
assert (3.0).is_integer()
58+
assert (-3.0).is_integer()
59+
assert (0.0).is_integer()
60+
assert (-0.0).is_integer()
61+
assert (-6.642122310788808 * 10**250).is_integer()
62+
assert not (2**52 - 1 + 0.5).is_integer()
63+
# for doubles this big, all representable values are integers...
64+
assert (2**52 + 0.5).is_integer()
65+
5466
def test_rounding(self):
5567
assert round(1.123, 0) == 1
5668
assert round(1.123, 1) == 1.1
@@ -85,8 +97,8 @@ def test___round__(self):
8597
pass
8698
else:
8799
assert False, "rounding with a float should have raised"
88-
89-
class F:
100+
101+
class F:
90102
pass
91103

92104
setattr(F, "__round__", round)
@@ -96,8 +108,8 @@ class F:
96108
pass
97109
else:
98110
assert False, "rounding with a non-float should have raised"
99-
100-
class F(float):
111+
112+
class F(float):
101113
pass
102114

103115
setattr(F, "__round__", round)
@@ -107,7 +119,7 @@ class F(float):
107119
pass
108120
else:
109121
assert False, "rounding with only 1 arg should have raised"
110-
122+
111123
round(F(4.2), 1)
112124

113125
def r(o):
@@ -391,7 +403,7 @@ def test_whitespace(self):
391403
'\n \t',
392404
'\f',
393405
# TODO fix in our implementation
394-
#'\v',
406+
#'\v',
395407
'\r'
396408
]
397409
for inp, expected in value_pairs:
@@ -735,8 +747,8 @@ def test_GR8901(self):
735747
class F(float):
736748
def _new_(cls, value):
737749
return float._new_(cls, value + 1)
738-
739-
@classmethod
750+
751+
@classmethod
740752
def fromhex(cls, value1, value2):
741753
return super(F, cls).fromhex(value1 + value2)
742754

@@ -748,7 +760,7 @@ class MyFloat(float):
748760
pass
749761

750762
class RealImagConjugateTests(unittest.TestCase):
751-
763+
752764
def test_real_imag(self):
753765
def builtinTest(number):
754766
a = float(number)
@@ -852,12 +864,12 @@ def test_format(self):
852864
# empty presentation type should format in the same way as str
853865
# (issue 5920)
854866
x = 100/7.
855-
867+
856868
self.assertEqual(format(x, ''), str(x))
857869
self.assertEqual(format(x, '-'), str(x))
858870
self.assertEqual(format(x, '>'), str(x))
859871
self.assertEqual(format(x, '2'), str(x))
860-
872+
861873
self.assertEqual(format(1.0, 'f'), '1.000000')
862874

863875
self.assertEqual(format(-1.0, 'f'), '-1.000000')
@@ -866,7 +878,7 @@ def test_format(self):
866878
self.assertEqual(format(-1.0, ' f'), '-1.000000')
867879
self.assertEqual(format( 1.0, '+f'), '+1.000000')
868880
self.assertEqual(format(-1.0, '+f'), '-1.000000')
869-
881+
870882
# % formatting
871883
self.assertEqual(format(-1.0, '%'), '-100.000000%')
872884

@@ -877,7 +889,7 @@ def test_format(self):
877889
# in particular int specifiers
878890
for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
879891
[chr(x) for x in range(ord('A'), ord('Z')+1)]):
880-
892+
881893
if not format_spec in 'eEfFgGn%rNz':
882894
self.assertRaises(ValueError, format, 0.0, format_spec)
883895
self.assertRaises(ValueError, format, 1.0, format_spec)
@@ -892,7 +904,7 @@ def test_format(self):
892904
self.assertEqual(format(NAN, 'F'), 'NAN')
893905
self.assertEqual(format(INF, 'f'), 'inf')
894906
self.assertEqual(format(INF, 'F'), 'INF')
895-
907+
896908
def test_format_testfile(self):
897909
with open(format_testfile) as testfile:
898910
for line in testfile:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ protected ArgumentClinicProvider getArgumentClinic() {
11511151
abstract static class IsIntegerNode extends AbstractNumericUnaryBuiltin {
11521152
@Override
11531153
protected Object op(double value) {
1154-
return Double.isFinite(value) && (long) value == value;
1154+
return Double.isFinite(value) && Math.floor(value) == value;
11551155
}
11561156
}
11571157

0 commit comments

Comments
 (0)