This repository was archived by the owner on Jul 17, 2024. It is now read-only.
File tree 4 files changed +15
-27
lines changed
jpyinterpreter/src/main/java/ai/timefold/jpyinterpreter/types/numeric
4 files changed +15
-27
lines changed Original file line number Diff line number Diff line change @@ -117,12 +117,8 @@ public String toString() {
117
117
}
118
118
119
119
public boolean equals (Object o ) {
120
- if (o instanceof PythonInteger other ) {
121
- return value .compareTo (new BigDecimal (other .value )) == 0 ;
122
- } else if (o instanceof PythonFloat other ) {
123
- return value .doubleValue () == other .value ;
124
- } else if (o instanceof PythonDecimal other ) {
125
- return value .compareTo (other .value ) == 0 ;
120
+ if (o instanceof PythonNumber number ) {
121
+ return compareTo (number ) == 0 ;
126
122
} else {
127
123
return false ;
128
124
}
Original file line number Diff line number Diff line change @@ -65,7 +65,6 @@ private static PythonLikeType registerMethods() throws NoSuchMethodException {
65
65
case "-inf" , "-infinity" -> "-Infinity" ;
66
66
default -> str .value ;
67
67
};
68
- Double .valueOf ("2" );
69
68
return new PythonFloat (Double .parseDouble (literal ));
70
69
} catch (NumberFormatException e ) {
71
70
throw new ValueError ("invalid literal for float(): %s" .formatted (value ));
@@ -232,14 +231,10 @@ public String toString() {
232
231
233
232
@ Override
234
233
public boolean equals (Object o ) {
235
- if (o instanceof Number ) {
236
- return ((Number ) o ).doubleValue () == value ;
237
- } else if (o instanceof PythonFloat ) {
238
- return ((PythonFloat ) o ).value == value ;
239
- } else if (o instanceof PythonInteger ) {
240
- return ((PythonInteger ) o ).getValue ().doubleValue () == value ;
241
- } else if (o instanceof PythonDecimal other ) {
242
- return new BigDecimal (value ).equals (other .value );
234
+ if (o instanceof Number number ) {
235
+ return number .doubleValue () == value ;
236
+ } else if (o instanceof PythonNumber number ) {
237
+ return compareTo (number ) == 0 ;
243
238
} else {
244
239
return false ;
245
240
}
Original file line number Diff line number Diff line change @@ -276,14 +276,10 @@ public byte asByte() {
276
276
277
277
@ Override
278
278
public boolean equals (Object o ) {
279
- if (o instanceof Number ) {
280
- return value .equals (BigInteger .valueOf (((Number ) o ).longValue ()));
281
- } else if (o instanceof PythonInteger other ) {
282
- return other .value .equals (value );
283
- } else if (o instanceof PythonFloat other ) {
284
- return value .doubleValue () == other .value ;
285
- } else if (o instanceof PythonDecimal other ) {
286
- return new BigDecimal (value ).equals (other .value );
279
+ if (o instanceof Number number ) {
280
+ return value .equals (BigInteger .valueOf (number .longValue ()));
281
+ } else if (o instanceof PythonNumber number ) {
282
+ return compareTo (number ) == 0 ;
287
283
} else {
288
284
return false ;
289
285
}
Original file line number Diff line number Diff line change @@ -24,17 +24,18 @@ default int compareTo(PythonNumber pythonNumber) {
24
24
if (value instanceof BigInteger self ) {
25
25
if (otherValue instanceof BigInteger other ) {
26
26
return self .compareTo (other );
27
- } else {
28
- return Double . compare ( value . longValue (), otherValue . doubleValue () );
27
+ } else if ( otherValue instanceof BigDecimal other ) {
28
+ return new BigDecimal ( self ). compareTo ( other );
29
29
}
30
30
}
31
31
if (value instanceof BigDecimal self ) {
32
32
if (otherValue instanceof BigDecimal other ) {
33
33
return self .compareTo (other );
34
- } else {
35
- return Double . compare ( value . doubleValue (), otherValue . doubleValue ( ));
34
+ } else if ( otherValue instanceof BigInteger other ) {
35
+ return self . compareTo ( new BigDecimal ( other ));
36
36
}
37
37
}
38
+ // If comparing against a float, convert both arguments to float
38
39
return Double .compare (value .doubleValue (), otherValue .doubleValue ());
39
40
}
40
41
You can’t perform that action at this time.
0 commit comments