@@ -936,7 +936,7 @@ boolean eqDbLn(double a, long b) {
936
936
937
937
@ Specialization
938
938
boolean eqDbPI (double a , PInt b ) {
939
- return Double . isFinite ( a ) && a == b . doubleValue () ;
939
+ return compareDoubleToLargeInt ( a , b ) == 0 ;
940
940
}
941
941
942
942
@ Specialization
@@ -962,6 +962,23 @@ Object eqPDb(VirtualFrame frame, PythonNativeObject left, PInt right,
962
962
PNotImplemented eq (Object a , Object b ) {
963
963
return PNotImplemented .NOT_IMPLEMENTED ;
964
964
}
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
+ }
965
982
}
966
983
967
984
@ Builtin (name = __NE__ , minNumOfPositionalArgs = 2 )
@@ -980,7 +997,7 @@ boolean neDbLn(double a, long b) {
980
997
981
998
@ Specialization
982
999
boolean neDbPI (double a , PInt b ) {
983
- return !( Double . isFinite ( a ) && a == b . doubleValue ()) ;
1000
+ return EqNode . compareDoubleToLargeInt ( a , b ) != 0 ;
984
1001
}
985
1002
986
1003
@ Specialization
@@ -1024,7 +1041,7 @@ boolean doDL(double x, long y) {
1024
1041
1025
1042
@ Specialization
1026
1043
boolean doPI (double x , PInt y ) {
1027
- return x < y . doubleValue () ;
1044
+ return EqNode . compareDoubleToLargeInt ( x , y ) < 0 ;
1028
1045
}
1029
1046
1030
1047
@ Specialization (guards = "fromNativeNode.isFloatSubtype(frame, y, getClass, isSubtype, context)" , limit = "1" )
@@ -1089,7 +1106,7 @@ boolean doDL(double x, long y) {
1089
1106
1090
1107
@ Specialization
1091
1108
boolean doPI (double x , PInt y ) {
1092
- return x <= y . doubleValue () ;
1109
+ return EqNode . compareDoubleToLargeInt ( x , y ) <= 0 ;
1093
1110
}
1094
1111
1095
1112
@ Specialization (guards = "fromNativeNode.isFloatSubtype(frame, y, getClass, isSubtype, context)" , limit = "1" )
@@ -1154,7 +1171,7 @@ boolean doDL(double x, long y) {
1154
1171
1155
1172
@ Specialization
1156
1173
boolean doPI (double x , PInt y ) {
1157
- return x > y . doubleValue () ;
1174
+ return EqNode . compareDoubleToLargeInt ( x , y ) > 0 ;
1158
1175
}
1159
1176
1160
1177
@ Specialization (guards = "fromNativeNode.isFloatSubtype(frame, y, getClass, isSubtype, context)" , limit = "1" )
@@ -1219,7 +1236,7 @@ boolean doDL(double x, long y) {
1219
1236
1220
1237
@ Specialization
1221
1238
boolean doPI (double x , PInt y ) {
1222
- return x >= y . doubleValue () ;
1239
+ return EqNode . compareDoubleToLargeInt ( x , y ) >= 0 ;
1223
1240
}
1224
1241
1225
1242
@ Specialization (guards = "fromNativeNode.isFloatSubtype(frame, y, getClass, isSubtype, context)" , limit = "1" )
0 commit comments