[pypy-commit] pypy py3k: if a complex number is created by just a negative imaginary part, the real part should be -0.0, not 0.0.

antocuni noreply at buildbot.pypy.org
Thu Oct 18 14:23:43 CEST 2012


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: py3k
Changeset: r58208:b0ae89b4e3bc
Date: 2012-10-18 14:14 +0200
http://bitbucket.org/pypy/pypy/changeset/b0ae89b4e3bc/

Log:	if a complex number is created by just a negative imaginary part,
	the real part should be -0.0, not 0.0. See CPython issue 9011:
	http://bugs.python.org/issue9011

diff --git a/pypy/objspace/std/complextype.py b/pypy/objspace/std/complextype.py
--- a/pypy/objspace/std/complextype.py
+++ b/pypy/objspace/std/complextype.py
@@ -71,7 +71,10 @@
                 imagpart = '-1.0'
             else:
                 imagpart = s[realstart:newstop]
-            return '0.0', imagpart
+            if imagpart[0] == '-':
+                return '-0.0', imagpart
+            else:
+                return '0.0', imagpart
         else:
             return s[realstart:realstop], '0.0'
 
diff --git a/pypy/objspace/std/test/test_complexobject.py b/pypy/objspace/std/test/test_complexobject.py
--- a/pypy/objspace/std/test/test_complexobject.py
+++ b/pypy/objspace/std/test/test_complexobject.py
@@ -45,7 +45,7 @@
         test_cparse('(1-6j)', '1', '-6')
         test_cparse(' ( +3.14-6J )', '+3.14', '-6')
         test_cparse(' +J', '0.0', '1.0')
-        test_cparse(' -J', '0.0', '-1.0')
+        test_cparse(' -J', '-0.0', '-1.0')
 
     def test_unpackcomplex(self):
         space = self.space
@@ -572,3 +572,20 @@
 
     def test_complex_two_arguments(self):
         raises(TypeError, complex, 5, None)
+
+    def test_negated_imaginary_literal(self):
+        def sign(x):
+            import math
+            return math.copysign(1.0, x)
+        z0 = -0j
+        z1 = -7j
+        z2 = -1e1000j
+        # Note: In versions of Python < 3.2, a negated imaginary literal
+        # accidentally ended up with real part 0.0 instead of -0.0
+        assert sign(z0.real) == -1
+        assert sign(z0.imag) == -1
+        assert sign(z1.real) == -1
+        assert sign(z1.imag) == -1
+        assert sign(z2.real) == -1
+        assert sign(z2.real) == -1
+        


More information about the pypy-commit mailing list