]> granicus.if.org Git - python/commitdiff
Issue #22604: Fix assertion error in debug mode when dividing a complex number by...
authorAntoine Pitrou <solipsis@pitrou.net>
Fri, 10 Oct 2014 21:49:32 +0000 (23:49 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Fri, 10 Oct 2014 21:49:32 +0000 (23:49 +0200)
Lib/test/test_complex.py
Misc/NEWS
Objects/complexobject.c

index cd55375bdb98b6dc9898cc7a802bf79a4b976549..0ef9a7a1098e70d07a185b3866590dc22bee6f24 100644 (file)
@@ -27,7 +27,7 @@ class ComplexTest(unittest.TestCase):
                 unittest.TestCase.assertAlmostEqual(self, a, b)
 
     def assertCloseAbs(self, x, y, eps=1e-9):
-        """Return true iff floats x and y "are close\""""
+        """Return true iff floats x and y "are close"."""
         # put the one with larger magnitude second
         if abs(x) > abs(y):
             x, y = y, x
@@ -62,7 +62,7 @@ class ComplexTest(unittest.TestCase):
         self.fail(msg.format(x, y))
 
     def assertClose(self, x, y, eps=1e-9):
-        """Return true iff complexes x and y "are close\""""
+        """Return true iff complexes x and y "are close"."""
         self.assertCloseAbs(x.real, y.real, eps)
         self.assertCloseAbs(x.imag, y.imag, eps)
 
@@ -104,6 +104,11 @@ class ComplexTest(unittest.TestCase):
         self.assertAlmostEqual(complex.__truediv__(2+0j, 1+1j), 1-1j)
         self.assertRaises(ZeroDivisionError, complex.__truediv__, 1+1j, 0+0j)
 
+        for denom_real, denom_imag in [(0, NAN), (NAN, 0), (NAN, NAN)]:
+            z = complex(0, 0) / complex(denom_real, denom_imag)
+            self.assertTrue(isnan(z.real))
+            self.assertTrue(isnan(z.imag))
+
     def test_floordiv(self):
         self.assertRaises(TypeError, complex.__floordiv__, 3+0j, 1.5+0j)
         self.assertRaises(TypeError, complex.__floordiv__, 3+0j, 0+0j)
index c490be5a3d4e1c42ccbcc20e58b2a337dc154689..a9ac0b2c8393c7705cbc06ef8f623a27520f2a66 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -11,6 +11,9 @@ Release date: TBA
 Core and Builtins
 -----------------
 
+- Issue #22604: Fix assertion error in debug mode when dividing a complex
+  number by (nan+0j).
+
 - Issue #22470: Fixed integer overflow issues in "backslashreplace",
   "xmlcharrefreplace", and "surrogatepass" error handlers.
 
index a5b76f0460055b4a006681f48fee71175b2becbc..0128120e7f7f86c1cdd5027561cce89dc64db6ad 100644 (file)
@@ -78,7 +78,7 @@ c_quot(Py_complex a, Py_complex b)
      const double abs_breal = b.real < 0 ? -b.real : b.real;
      const double abs_bimag = b.imag < 0 ? -b.imag : b.imag;
 
-     if (abs_breal >= abs_bimag) {
+    if (abs_breal >= abs_bimag) {
         /* divide tops and bottom by b.real */
         if (abs_breal == 0.0) {
             errno = EDOM;
@@ -91,7 +91,7 @@ c_quot(Py_complex a, Py_complex b)
             r.imag = (a.imag - a.real * ratio) / denom;
         }
     }
-    else {
+    else if (abs_bimag >= abs_breal) {
         /* divide tops and bottom by b.imag */
         const double ratio = b.real / b.imag;
         const double denom = b.real * ratio + b.imag;
@@ -99,6 +99,10 @@ c_quot(Py_complex a, Py_complex b)
         r.real = (a.real * ratio + a.imag) / denom;
         r.imag = (a.imag * ratio - a.real) / denom;
     }
+    else {
+        /* At least one of b.real or b.imag is a NaN */
+        r.real = r.imag = Py_NAN;
+    }
     return r;
 }