@@ -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" )
0 commit comments