[pypy-commit] pypy numpypy-complex2: Complex numbers with negative imag are now formatted correctly.

jbs noreply at buildbot.pypy.org
Sun Jun 24 15:03:37 CEST 2012


Author: Jasper Schulz <jasper.schulz at student.hpi.uni-potsdam.de>
Branch: numpypy-complex2
Changeset: r55796:d97ef3a2ab10
Date: 2012-06-23 15:22 +0200
http://bitbucket.org/pypy/pypy/changeset/d97ef3a2ab10/

Log:	Complex numbers with negative imag are now formatted correctly.

	``complex(1, -2)`` => ``(1-2j``

diff --git a/pypy/module/micronumpy/test/test_dtypes.py b/pypy/module/micronumpy/test/test_dtypes.py
--- a/pypy/module/micronumpy/test/test_dtypes.py
+++ b/pypy/module/micronumpy/test/test_dtypes.py
@@ -434,20 +434,17 @@
             numpy.complexfloating, numpy.inexact, numpy.number, numpy.generic,
             complex, object)
 
-        c = numpy.complex128(complex(1, 2))
-        assert c.real == 1 
-        assert c.imag == 2
-        assert repr(c) == '(1+2j)'
+        for real, imag, should in [
+            (1, 2, '(1+2j)'),
+            (0, 1, '1j'),
+            (1, 0, '(1+0j)'),
+            (-1, -2, '(-1-2j)')
+            ]:
 
-        c = numpy.complex128(complex(0, 1))
-        assert c.real == 0
-        assert c.imag == 1
-        assert repr(c) == '1j'
-
-        c = numpy.complex128(complex(1, 0))
-        assert c.real == 1
-        assert c.imag == 0
-        assert repr(c) == '(1+0j)'
+            c = numpy.complex128(complex(real, imag))
+            assert c.real == real
+            assert c.imag == imag
+            assert repr(c) == should
 
     def test_complex_dtype(self):
         import _numpypy as numpy
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -937,11 +937,14 @@
 
     def str_format(self, box):
         imag_str = str_format(box.imag) + 'j'
+        
+        # (0+2j) => 2j
         if box.real == 0:
-            return imag_str
-        
+            return imag_str        
+
         real_str = str_format(box.real)
-        return ''.join(['(', real_str, '+', imag_str, ')'])
+        op = '+' if box.imag >= 0 else ''
+        return ''.join(['(', real_str, op, imag_str, ')'])
 
 
 NonNativeComplex128 = Complex128


More information about the pypy-commit mailing list