]> 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 59e86773ad1c93b0996f08da2d976215d2d39c60..c0383b27e66c039ea5d89ab2376822a31f32d4ea 100644 (file)
@@ -26,7 +26,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
@@ -61,7 +61,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)
 
@@ -108,6 +108,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.assertAlmostEqual(complex.__floordiv__(3+0j, 1.5+0j), 2)
         self.assertRaises(ZeroDivisionError, complex.__floordiv__, 3+0j, 0+0j)
index 4f09b02a27ef4387d5c7aeca1a6d456c75758b6c..90966f25dfc5603d42649f47e3e836e9dd5563ac 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 2.7.9?
 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" and
   "xmlcharrefreplace" error handlers.
 
index 5ee0c15e3d76afd930b85ae2b7826c15c7070ecf..2659a23cbe215ca0cc6ad5d502ff70cb6fafddd6 100644 (file)
@@ -96,7 +96,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;
@@ -109,7 +109,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;
@@ -117,6 +117,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;
 }