[Python-checkins] cpython (3.4): Issue #22604: Fix assertion error in debug mode when dividing a complex number

antoine.pitrou python-checkins at python.org
Fri Oct 10 23:57:44 CEST 2014


https://hg.python.org/cpython/rev/0c8f45caf697
changeset: 92940:0c8f45caf697
branch: 3.4
parent: 92938:c9c6082d4f9b
user: Antoine Pitrou <solipsis at pitrou.net>
date: Fri Oct 10 23:49:32 2014 +0200
summary:
 Issue #22604: Fix assertion error in debug mode when dividing a complex number by (nan+0j).
files:
 Lib/test/test_complex.py | 9 +++++++--
 Misc/NEWS | 3 +++
 Objects/complexobject.c | 8 ++++++--
 3 files changed, 16 insertions(+), 4 deletions(-)
diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py
--- a/Lib/test/test_complex.py
+++ b/Lib/test/test_complex.py
@@ -27,7 +27,7 @@
 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 @@
 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 @@
 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)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -11,6 +11,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",
 "xmlcharrefreplace", and "surrogatepass" error handlers.
 
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -78,7 +78,7 @@
 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 @@
 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 @@
 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;
 }
 
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /