Skip to content

Commit 35b3e3a

Browse files
msimaceksteve-s
authored andcommitted
Fix comparing NaN to a long
1 parent 980ca0c commit 35b3e3a

File tree

2 files changed

+56
-1
lines changed
  • graalpython

2 files changed

+56
-1
lines changed

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

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2023, 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
@@ -229,6 +229,58 @@ def test_compare(self):
229229
self.assertTrue(i > f)
230230
self.assertTrue(i >= f)
231231

232+
def test_compare_infinite(self):
233+
nan = float('nan')
234+
inf = float('inf')
235+
ninf = float('-inf')
236+
positives = [0.0, 1.0, 1, 1125899906842624, 1 << 100]
237+
negatives = [-x for x in positives]
238+
for num in [nan, inf, ninf, *positives, *negatives]:
239+
self.assertFalse(nan == num)
240+
self.assertTrue(nan != num)
241+
self.assertFalse(nan < num)
242+
self.assertFalse(nan <= num)
243+
self.assertFalse(nan > num)
244+
self.assertFalse(nan >= num)
245+
self.assertFalse(num == nan)
246+
self.assertTrue(num != nan)
247+
self.assertFalse(num < nan)
248+
self.assertFalse(num <= nan)
249+
self.assertFalse(num > nan)
250+
self.assertFalse(num >= nan)
251+
252+
self.assertTrue(inf == inf)
253+
self.assertFalse(inf != inf)
254+
for num in [ninf, *positives, *negatives]:
255+
self.assertFalse(inf == num)
256+
self.assertFalse(num == inf)
257+
self.assertTrue(inf != num)
258+
self.assertTrue(num != inf)
259+
self.assertTrue(inf > num)
260+
self.assertTrue(inf >= num)
261+
self.assertFalse(inf < num)
262+
self.assertFalse(inf <= num)
263+
self.assertFalse(num > inf)
264+
self.assertFalse(num >= inf)
265+
self.assertTrue(num < inf)
266+
self.assertTrue(num <= inf)
267+
268+
self.assertTrue(ninf == ninf)
269+
self.assertFalse(ninf != ninf)
270+
for num in [*positives, *negatives]:
271+
self.assertFalse(ninf == num)
272+
self.assertFalse(num == ninf)
273+
self.assertTrue(ninf != num)
274+
self.assertTrue(num != ninf)
275+
self.assertFalse(ninf > num)
276+
self.assertFalse(ninf >= num)
277+
self.assertTrue(ninf < num)
278+
self.assertTrue(ninf <= num)
279+
self.assertTrue(num > ninf)
280+
self.assertTrue(num >= ninf)
281+
self.assertFalse(num < ninf)
282+
self.assertFalse(num <= ninf)
283+
232284

233285
fromHex = float.fromhex
234286

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,9 @@ public static double compareDoubleToLargeInt(double v, PInt w) {
792792

793793
@TruffleBoundary
794794
private static double compareUsingBigDecimal(double v, BigInteger w) {
795+
if (!Double.isFinite(v)) {
796+
return v;
797+
}
795798
return new BigDecimal(v).compareTo(new BigDecimal(w));
796799
}
797800
}

0 commit comments

Comments
 (0)