Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Lib/_pydecimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6050,8 +6050,9 @@ def _convert_for_comparison(self, other, equality_op=False):

# Comparisons with float and complex types. == and != comparisons
# with complex numbers should succeed, returning either True or False
# as appropriate. Other comparisons return NotImplemented.
if equality_op and isinstance(other, _numbers.Complex) and other.imag == 0:
# as appropriate. Other comparisons return NotImplemented if the
# complex object has an imaginary component.
if isinstance(other, _numbers.Complex) and other.imag == 0:
other = other.real
if isinstance(other, float):
context = getcontext()
Expand Down
11 changes: 8 additions & 3 deletions Lib/fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,15 +593,20 @@ def _richcmp(self, other, op):

Implement comparison between a Rational instance `self`, and
either another Rational instance or a float `other`. If
`other` is not a Rational instance or a float, return
NotImplemented. `op` should be one of the six standard
comparison operators.
`other` is not a Rational instance, a float, or a real-valued
complex, return NotImplemented. `op` should be one of the six
standard comparison operators.

"""
# convert other to a Rational instance where reasonable.
if isinstance(other, numbers.Rational):
return op(self._numerator * other.denominator,
self._denominator * other.numerator)
if isinstance(other, complex):
if other.imag:
return NotImplemented
else:
other = other.real
if isinstance(other, float):
if math.isnan(other) or math.isinf(other):
return op(0.0, other)
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,20 @@ def test_richcompare(self):
self.assertIs(operator.eq(1+1j, 2+2j), False)
self.assertIs(operator.ne(1+1j, 1+1j), False)
self.assertIs(operator.ne(1+1j, 2+2j), True)
for n in range(100):
for f in (int(n), float(n), complex(n)):
self.assertIs(complex.__lt__(complex(f), f), False)
self.assertIs(complex.__gt__(complex(f), f), False)
self.assertIs(complex.__le__(complex(f), f), True)
self.assertIs(complex.__ge__(complex(f), f), True)
self.assertIs(complex.__lt__(complex(f-1), f), True)
self.assertIs(complex.__gt__(complex(f-1), f), False)
self.assertIs(complex.__le__(complex(f-1), f), True)
self.assertIs(complex.__ge__(complex(f-1), f), False)
self.assertIs(complex.__lt__(complex(f), f-1), False)
self.assertIs(complex.__gt__(complex(f), f-1), True)
self.assertIs(complex.__le__(complex(f), f-1), False)
self.assertIs(complex.__ge__(complex(f), f-1), True)

def test_richcompare_boundaries(self):
def check(n, deltas, is_equal, imag = 0.0):
Expand Down
12 changes: 8 additions & 4 deletions Lib/test/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1705,10 +1705,14 @@ def test_decimal_complex_comparison(self):
self.assertNotEqual(db, (3.0+1j))
self.assertNotEqual((3.0+1j), db)

self.assertIs(db.__lt__(3.0+0j), NotImplemented)
self.assertIs(db.__le__(3.0+0j), NotImplemented)
self.assertIs(db.__gt__(3.0+0j), NotImplemented)
self.assertIs(db.__le__(3.0+0j), NotImplemented)
self.assertIs(db.__lt__(0+3.0j), NotImplemented)
self.assertIs(db.__le__(0+3.0j), NotImplemented)
self.assertIs(db.__gt__(0+3.0j), NotImplemented)
self.assertIs(db.__le__(0+3.0j), NotImplemented)
self.assertIs(db.__lt__(3.0+0j), False)
self.assertIs(db.__le__(3.0+0j), True)
self.assertIs(db.__gt__(3.0+0j), False)
self.assertIs(db.__le__(3.0+0j), True)

def test_decimal_fraction_comparison(self):
D = self.decimal.Decimal
Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,6 @@ def testBigFloatComparisons(self):

def testBigComplexComparisons(self):
self.assertFalse(F(10**23) == complex(10**23))
self.assertRaises(TypeError, operator.gt, F(10**23), complex(10**23))
self.assertRaises(TypeError, operator.le, F(10**23), complex(10**23))

x = F(3, 8)
z = complex(0.375, 0.0)
Expand All @@ -601,9 +599,11 @@ def testBigComplexComparisons(self):
self.assertFalse(x != z)
self.assertFalse(x == w)
self.assertTrue(x != w)
self.assertFalse(x > z)
self.assertFalse(x < z)
self.assertTrue(x >= z)
self.assertTrue(x <= z)
for op in operator.lt, operator.le, operator.gt, operator.ge:
self.assertRaises(TypeError, op, x, z)
self.assertRaises(TypeError, op, z, x)
self.assertRaises(TypeError, op, x, w)
self.assertRaises(TypeError, op, w, x)

Expand Down
19 changes: 13 additions & 6 deletions Lib/test/test_numeric_tower.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ def test_mixed_comparisons(self):
def test_complex(self):
# comparisons with complex are special: equality and inequality
# comparisons should always succeed, but order comparisons should
# raise TypeError.
# raise TypeError if the complex object has a nonzero imaginary
# component.
z = 1.0 + 0j
w = -3.14 + 2.7j

Expand All @@ -191,11 +192,17 @@ def test_complex(self):
self.assertNotEqual(w, v)
self.assertNotEqual(v, w)

for v in (1, 1.0, F(1), D(1), complex(1),
2, 2.0, F(2), D(2), complex(2), w):
for op in operator.le, operator.lt, operator.ge, operator.gt:
self.assertRaises(TypeError, op, z, v)
self.assertRaises(TypeError, op, v, z)
for i in (1, 1.0, F(1), D(1), complex(1)):
for j in (2, 2.0, F(2), D(2), complex(2)):
for op in operator.le, operator.lt, operator.ge, operator.gt:
self.assertLess(i, j)
self.assertLessEqual(i, j)
self.assertGreater(j, i)
self.assertGreaterEqual(j, i)
self.assertRaises(TypeError, op, w, i)
self.assertRaises(TypeError, op, i, w)
self.assertRaises(TypeError, op, w, j)
self.assertRaises(TypeError, op, j, w)


if __name__ == '__main__':
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ Ian Bruntlett
Floris Bruynooghe
Matt Bryant
Stan Bubrouski
Brandt Bucher
Colm Buckley
Erik de Bueger
Jan-Hein Bührman
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Rich comparisons of real-valued :class:`complex` objects with other numeric types no longer fail.
Patch by Brandt Bucher.
2 changes: 1 addition & 1 deletion Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2875,7 +2875,7 @@ convert_op_cmp(PyObject **vcmp, PyObject **wcmp, PyObject *v, PyObject *w,
*wcmp = PyDec_FromFloatExact(w, context);
}
}
else if (PyComplex_Check(w) && (op == Py_EQ || op == Py_NE)) {
else if (PyComplex_Check(w)) {
Py_complex c = PyComplex_AsCComplex(w);
if (c.real == -1.0 && PyErr_Occurred()) {
*wcmp = NULL;
Expand Down
37 changes: 28 additions & 9 deletions Objects/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -628,14 +628,10 @@ complex_richcompare(PyObject *v, PyObject *w, int op)
Py_complex i;
int equal;

if (op != Py_EQ && op != Py_NE) {
goto Unimplemented;
}

assert(PyComplex_Check(v));
TO_COMPLEX(v, i);

if (PyLong_Check(w)) {
if (PyLong_Check(w) || PyFloat_Check(w)) {
/* Check for 0.0 imaginary part first to avoid the rich
* comparison when possible.
*/
Expand All @@ -649,18 +645,41 @@ complex_richcompare(PyObject *v, PyObject *w, int op)
Py_DECREF(j);
return sub_res;
}
else if (op != Py_EQ && op != Py_NE) {
goto Unimplemented;
}
else {
equal = 0;
}
}
else if (PyFloat_Check(w)) {
equal = (i.real == PyFloat_AsDouble(w) && i.imag == 0.0);
}
else if (PyComplex_Check(w)) {
Py_complex j;

TO_COMPLEX(w, j);
equal = (i.real == j.real && i.imag == j.imag);

/* Any comparison is legal if both values are real.
* If so, we use PyFloat's rich comparison.
*/
if (i.imag == 0.0 && j.imag == 0.0) {
PyObject *x, *y, *sub_res;
x = PyFloat_FromDouble(i.real);
if (x == NULL)
return NULL;
y = PyFloat_FromDouble(j.real);
if (y == NULL)
return NULL;

sub_res = PyObject_RichCompare(x, y, op);
Py_DECREF(x);
Py_DECREF(y);
return sub_res;
}
else if (op != Py_EQ && op != Py_NE) {
goto Unimplemented;
}
else {
equal = (i.real == j.real && i.imag == j.imag);
}
}
else {
goto Unimplemented;
Expand Down