Skip to content

Commit e697983

Browse files
committed
Fixed comparison of floats to large ints
1 parent c29f564 commit e697983

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*graalpython.lib-python.3.test.test_long.LongTest.test_long
1818
*graalpython.lib-python.3.test.test_long.LongTest.test_format
1919
*graalpython.lib-python.3.test.test_long.LongTest.test_lshift_of_zero
20+
*graalpython.lib-python.3.test.test_long.LongTest.test_mixed_compares
2021
*graalpython.lib-python.3.test.test_long.LongTest.test_mod_division
2122
*graalpython.lib-python.3.test.test_long.LongTest.test_nan_inf
2223
*graalpython.lib-python.3.test.test_long.LongTest.test_negative_shift_count

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ boolean eqDbLn(double a, long b) {
936936

937937
@Specialization
938938
boolean eqDbPI(double a, PInt b) {
939-
return Double.isFinite(a) && a == b.doubleValue();
939+
return compareDoubleToLargeInt(a, b) == 0;
940940
}
941941

942942
@Specialization
@@ -962,6 +962,23 @@ Object eqPDb(VirtualFrame frame, PythonNativeObject left, PInt right,
962962
PNotImplemented eq(Object a, Object b) {
963963
return PNotImplemented.NOT_IMPLEMENTED;
964964
}
965+
966+
// adapted from CPython's float_richcompare in floatobject.c
967+
static double compareDoubleToLargeInt(double v, PInt w) {
968+
if (!Double.isFinite(v)) {
969+
return v;
970+
}
971+
int vsign = v == 0.0 ? 0 : v < 0.0 ? -1 : 1;
972+
int wsign = w.isZero() ? 0 : w.isNegative() ? -1 : 1;
973+
if (vsign != wsign) {
974+
return vsign - wsign;
975+
}
976+
if (w.bitLength() <= 48) {
977+
return v - w.doubleValue();
978+
} else {
979+
return new BigDecimal(v).compareTo(new BigDecimal(w.getValue()));
980+
}
981+
}
965982
}
966983

967984
@Builtin(name = __NE__, minNumOfPositionalArgs = 2)
@@ -980,7 +997,7 @@ boolean neDbLn(double a, long b) {
980997

981998
@Specialization
982999
boolean neDbPI(double a, PInt b) {
983-
return !(Double.isFinite(a) && a == b.doubleValue());
1000+
return EqNode.compareDoubleToLargeInt(a, b) != 0;
9841001
}
9851002

9861003
@Specialization
@@ -1024,7 +1041,7 @@ boolean doDL(double x, long y) {
10241041

10251042
@Specialization
10261043
boolean doPI(double x, PInt y) {
1027-
return x < y.doubleValue();
1044+
return EqNode.compareDoubleToLargeInt(x, y) < 0;
10281045
}
10291046

10301047
@Specialization(guards = "fromNativeNode.isFloatSubtype(frame, y, getClass, isSubtype, context)", limit = "1")
@@ -1089,7 +1106,7 @@ boolean doDL(double x, long y) {
10891106

10901107
@Specialization
10911108
boolean doPI(double x, PInt y) {
1092-
return x <= y.doubleValue();
1109+
return EqNode.compareDoubleToLargeInt(x, y) <= 0;
10931110
}
10941111

10951112
@Specialization(guards = "fromNativeNode.isFloatSubtype(frame, y, getClass, isSubtype, context)", limit = "1")
@@ -1154,7 +1171,7 @@ boolean doDL(double x, long y) {
11541171

11551172
@Specialization
11561173
boolean doPI(double x, PInt y) {
1157-
return x > y.doubleValue();
1174+
return EqNode.compareDoubleToLargeInt(x, y) > 0;
11581175
}
11591176

11601177
@Specialization(guards = "fromNativeNode.isFloatSubtype(frame, y, getClass, isSubtype, context)", limit = "1")
@@ -1219,7 +1236,7 @@ boolean doDL(double x, long y) {
12191236

12201237
@Specialization
12211238
boolean doPI(double x, PInt y) {
1222-
return x >= y.doubleValue();
1239+
return EqNode.compareDoubleToLargeInt(x, y) >= 0;
12231240
}
12241241

12251242
@Specialization(guards = "fromNativeNode.isFloatSubtype(frame, y, getClass, isSubtype, context)", limit = "1")

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/PInt.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ public boolean isOne() {
8282
return value.equals(BigInteger.ONE);
8383
}
8484

85-
@TruffleBoundary(allowInlining = true)
8685
public boolean isZero() {
87-
return value.equals(BigInteger.ZERO);
86+
return value.signum() == 0;
8887
}
8988

9089
@ExportMessage

0 commit comments

Comments
 (0)