Skip to content

Commit d622562

Browse files
committed
Fix missing comparisons between float and PInt
Add <= and >= comparisons between float and large integer. Fixes #117.
1 parent 200d23a commit d622562

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2019, Oracle and/or its affiliates.
1+
# Copyright (c) 2018, 2020, Oracle and/or its affiliates.
22
# Copyright (c) 2013, Regents of the University of California
33
#
44
# All rights reserved.
@@ -181,3 +181,17 @@ def assert_exception(op, ex_type):
181181
raise e
182182
else:
183183
assert type(e) == ex_type, "expected exception %s but got %s" % (ex_type, type(e))
184+
185+
186+
def test_comparison_numeric_types():
187+
large_int = 2 ** 65
188+
numbers = [-large_int, -float(large_int), -1.4, -1, -1.0, -0.0, 0, 0.0, 1, 1.0, 1.4, large_int, float(large_int)]
189+
indices = [1, 1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 7, 7]
190+
for i, a in zip(indices, numbers):
191+
for j, b in zip(indices, numbers):
192+
assert (a < b) == (i < j)
193+
assert (a <= b) == (i <= j)
194+
assert (a == b) == (i == j)
195+
assert (a != b) == (i != j)
196+
assert (a > b) == (i > j)
197+
assert (a >= b) == (i >= j)

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

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2020, Oracle and/or its affiliates.
33
* Copyright (c) 2014, Regents of the University of California
44
*
55
* All rights reserved.
@@ -1118,6 +1118,11 @@ boolean doDL(double x, long y) {
11181118
return x <= y;
11191119
}
11201120

1121+
@Specialization
1122+
boolean doPI(double x, PInt y) {
1123+
return x <= y.doubleValue();
1124+
}
1125+
11211126
@Specialization(guards = "fromNativeNode.isFloatSubtype(frame, y, getClass, isSubtype, context)", limit = "1")
11221127
boolean doDN(VirtualFrame frame, double x, PythonNativeObject y,
11231128
@SuppressWarnings("unused") @CachedContext(PythonLanguage.class) PythonContext context,
@@ -1243,6 +1248,50 @@ boolean doDL(double x, long y) {
12431248
return x >= y;
12441249
}
12451250

1251+
@Specialization
1252+
boolean doPI(double x, PInt y) {
1253+
return x >= y.doubleValue();
1254+
}
1255+
1256+
@Specialization(guards = "fromNativeNode.isFloatSubtype(frame, y, getClass, isSubtype, context)", limit = "1")
1257+
boolean doDN(VirtualFrame frame, double x, PythonNativeObject y,
1258+
@SuppressWarnings("unused") @CachedContext(PythonLanguage.class) PythonContext context,
1259+
@SuppressWarnings("unused") @Cached GetClassNode getClass,
1260+
@SuppressWarnings("unused") @Cached IsSubtypeNode isSubtype,
1261+
@Cached FromNativeSubclassNode fromNativeNode) {
1262+
return x >= fromNativeNode.execute(frame, y);
1263+
}
1264+
1265+
@Specialization(guards = {
1266+
"nativeLeft.isFloatSubtype(frame, x, getClass, isSubtype, context)",
1267+
"nativeRight.isFloatSubtype(frame, y, getClass, isSubtype, context)"}, limit = "1")
1268+
boolean doNN(VirtualFrame frame, PythonNativeObject x, PythonNativeObject y,
1269+
@SuppressWarnings("unused") @CachedContext(PythonLanguage.class) PythonContext context,
1270+
@SuppressWarnings("unused") @Cached GetClassNode getClass,
1271+
@SuppressWarnings("unused") @Cached IsSubtypeNode isSubtype,
1272+
@Cached FromNativeSubclassNode nativeLeft,
1273+
@Cached FromNativeSubclassNode nativeRight) {
1274+
return nativeLeft.execute(frame, x) >= nativeRight.execute(frame, y);
1275+
}
1276+
1277+
@Specialization(guards = "fromNativeNode.isFloatSubtype(frame, x, getClass, isSubtype, context)", limit = "1")
1278+
boolean doND(VirtualFrame frame, PythonNativeObject x, double y,
1279+
@SuppressWarnings("unused") @CachedContext(PythonLanguage.class) PythonContext context,
1280+
@SuppressWarnings("unused") @Cached GetClassNode getClass,
1281+
@SuppressWarnings("unused") @Cached IsSubtypeNode isSubtype,
1282+
@Cached FromNativeSubclassNode fromNativeNode) {
1283+
return fromNativeNode.execute(frame, x) >= y;
1284+
}
1285+
1286+
@Specialization(guards = "fromNativeNode.isFloatSubtype(frame, x, getClass, isSubtype, context)", limit = "1")
1287+
boolean doNL(VirtualFrame frame, PythonNativeObject x, long y,
1288+
@SuppressWarnings("unused") @CachedContext(PythonLanguage.class) PythonContext context,
1289+
@SuppressWarnings("unused") @Cached GetClassNode getClass,
1290+
@SuppressWarnings("unused") @Cached IsSubtypeNode isSubtype,
1291+
@Cached FromNativeSubclassNode fromNativeNode) {
1292+
return fromNativeNode.execute(frame, x) >= y;
1293+
}
1294+
12461295
@Fallback
12471296
@SuppressWarnings("unused")
12481297
PNotImplemented doGeneric(Object a, Object b) {

0 commit comments

Comments
 (0)