Skip to content

Commit 759920c

Browse files
committed
Issue #22604: Fix assertion error in debug mode when dividing a complex number by (nan+0j).
2 parents fc260a9 + 9086f92 commit 759920c

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

Lib/test/test_complex.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def assertAlmostEqual(self, a, b):
2727
unittest.TestCase.assertAlmostEqual(self, a, b)
2828

2929
def assertCloseAbs(self, x, y, eps=1e-9):
30-
"""Return true iff floats x and y "are close\""""
30+
"""Return true iff floats x and y "are close"."""
3131
# put the one with larger magnitude second
3232
if abs(x) > abs(y):
3333
x, y = y, x
@@ -62,7 +62,7 @@ def assertFloatsAreIdentical(self, x, y):
6262
self.fail(msg.format(x, y))
6363

6464
def assertClose(self, x, y, eps=1e-9):
65-
"""Return true iff complexes x and y "are close\""""
65+
"""Return true iff complexes x and y "are close"."""
6666
self.assertCloseAbs(x.real, y.real, eps)
6767
self.assertCloseAbs(x.imag, y.imag, eps)
6868

@@ -104,6 +104,11 @@ def test_truediv(self):
104104
self.assertAlmostEqual(complex.__truediv__(2+0j, 1+1j), 1-1j)
105105
self.assertRaises(ZeroDivisionError, complex.__truediv__, 1+1j, 0+0j)
106106

107+
for denom_real, denom_imag in [(0, NAN), (NAN, 0), (NAN, NAN)]:
108+
z = complex(0, 0) / complex(denom_real, denom_imag)
109+
self.assertTrue(isnan(z.real))
110+
self.assertTrue(isnan(z.imag))
111+
107112
def test_floordiv(self):
108113
self.assertRaises(TypeError, complex.__floordiv__, 3+0j, 1.5+0j)
109114
self.assertRaises(TypeError, complex.__floordiv__, 3+0j, 0+0j)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: TBA
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #22604: Fix assertion error in debug mode when dividing a complex
14+
number by (nan+0j).
15+
1316
- Issue #20152: Convert the array module to Argument Clinic.
1417

1518
- Issue #21052: Do not raise ImportWarning when sys.path_hooks or sys.meta_path

Objects/complexobject.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ _Py_c_quot(Py_complex a, Py_complex b)
7878
const double abs_breal = b.real < 0 ? -b.real : b.real;
7979
const double abs_bimag = b.imag < 0 ? -b.imag : b.imag;
8080

81-
if (abs_breal >= abs_bimag) {
81+
if (abs_breal >= abs_bimag) {
8282
/* divide tops and bottom by b.real */
8383
if (abs_breal == 0.0) {
8484
errno = EDOM;
@@ -91,14 +91,18 @@ _Py_c_quot(Py_complex a, Py_complex b)
9191
r.imag = (a.imag - a.real * ratio) / denom;
9292
}
9393
}
94-
else {
94+
else if (abs_bimag >= abs_breal) {
9595
/* divide tops and bottom by b.imag */
9696
const double ratio = b.real / b.imag;
9797
const double denom = b.real * ratio + b.imag;
9898
assert(b.imag != 0.0);
9999
r.real = (a.real * ratio + a.imag) / denom;
100100
r.imag = (a.imag * ratio - a.real) / denom;
101101
}
102+
else {
103+
/* At least one of b.real or b.imag is a NaN */
104+
r.real = r.imag = Py_NAN;
105+
}
102106
return r;
103107
}
104108

0 commit comments

Comments
 (0)