[pypy-svn] r77702 - in pypy/branch/fast-forward/pypy/objspace/std: . test

afa at codespeak.net afa at codespeak.net
Thu Oct 7 23:52:35 CEST 2010


Author: afa
Date: Thu Oct  7 23:52:32 2010
New Revision: 77702

Modified:
   pypy/branch/fast-forward/pypy/objspace/std/complexobject.py
   pypy/branch/fast-forward/pypy/objspace/std/complextype.py
   pypy/branch/fast-forward/pypy/objspace/std/test/test_complexobject.py
Log:
Be more careful to preserve the sign of zero when building a complex.
Also fix the repr() to display the sign of zero in both components.


Modified: pypy/branch/fast-forward/pypy/objspace/std/complexobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/complexobject.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/complexobject.py	Thu Oct  7 23:52:32 2010
@@ -3,6 +3,7 @@
 from pypy.objspace.std.model import registerimplementation, W_Object
 from pypy.objspace.std.register_all import register_all
 from pypy.objspace.std.floatobject import W_FloatObject, _hash_float
+from pypy.rlib.rarithmetic import formatd, isinf, isnan
 
 import math
 
@@ -254,33 +255,37 @@
     #w_imag = space.call_function(space.w_float,space.wrap(-w_self.imagval))
     return space.newcomplex(w_self.realval,-w_self.imagval)
 
-app = gateway.applevel(""" 
-    import math
-    import sys
-    def possint(f):
-        ff = math.floor(f)
-        if f == ff and abs(f) < sys.maxint:
-            return int(ff)
-        return f
-
-    def repr__Complex(f):
-        if not f.real:
-            return repr(possint(f.imag))+'j'
-        imag = f.imag
-        sign = ((imag >= 0) and '+') or ''
-        return '('+repr(possint(f.real)) + sign + repr(possint(f.imag))+'j)'
-
-    def str__Complex(f):
-        if not f.real:
-            return str(possint(f.imag))+'j'
-        imag = f.imag
-        sign = ((imag >= 0) and '+') or ''
-        return '('+str(possint(f.real)) + sign + str(possint(f.imag))+'j)'
-
-""", filename=__file__) 
+def format_float(x, format):
+    # like float2string, except that the ".0" is not necessary
+    if isinf(x):
+        if x > 0.0:
+            return "inf"
+        else:
+            return "-inf"
+    elif isnan(x):
+        return "nan"
+    else:
+        return formatd(format, x)
 
-repr__Complex = app.interphook('repr__Complex') 
-str__Complex = app.interphook('str__Complex') 
+def repr_format(x):
+    return format_float(x, "%.17g")
+def str_format(x):
+    return format_float(x, "%.12g")
+
+def repr__Complex(space, w_complex):
+    print "AFA REPR", (w_complex, w_complex.realval, w_complex.imagval)
+    if w_complex.realval == 0 and math.copysign(1., w_complex.realval) == 1.:
+        return space.wrap(repr_format(w_complex.imagval) + 'j')
+    sign = (math.copysign(1., w_complex.imagval) == 1.) and '+' or ''
+    return space.wrap('(' + repr_format(w_complex.realval)
+                      + sign + repr_format(w_complex.imagval) + 'j)')
+
+def str__Complex(space, w_complex):
+    if w_complex.realval == 0 and math.copysign(1., w_complex.realval) == 1.:
+        return space.wrap(str_format(w_complex.imagval) + 'j')
+    sign = (math.copysign(1., w_complex.imagval) == 1.) and '+' or ''
+    return space.wrap('(' + str_format(w_complex.realval)
+                      + sign + str_format(w_complex.imagval) + 'j)')
 
 from pypy.objspace.std import complextype
 register_all(vars(), complextype)

Modified: pypy/branch/fast-forward/pypy/objspace/std/complextype.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/complextype.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/complextype.py	Thu Oct  7 23:52:32 2010
@@ -192,7 +192,11 @@
                                      space.wrap("complex() second arg"
                                                 " can't be a string"))
             else:
-                imagval += space.float_w(space.float(w_imag))
+                a = space.float_w(space.float(w_imag))
+                if imagval != 0.0:
+                    imagval += a
+                else:
+                    imagval = a
     # done
     w_obj = space.allocate_instance(W_ComplexObject, w_complextype)
     W_ComplexObject.__init__(w_obj, realval, imagval)

Modified: pypy/branch/fast-forward/pypy/objspace/std/test/test_complexobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/test/test_complexobject.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/test/test_complexobject.py	Thu Oct  7 23:52:32 2010
@@ -321,12 +321,16 @@
         raises(ValueError, complex, unicode("1"*500))
         
     def test_repr(self):
-        h = self.helper
-        h.assertEqual(repr(1+6j), '(1+6j)')
-        h.assertEqual(repr(1-6j), '(1-6j)')
+        assert repr(1+6j) == '(1+6j)'
+        assert repr(1-6j) == '(1-6j)'
 
-        h.assertNotEqual(repr(-(1+0j)), '(-1+-0j)')
+        assert repr(-(1+0j)) == '(-1-0j)'
+        assert repr(complex( 0.0,  0.0)) == '0j'
+        assert repr(complex( 0.0, -0.0)) == '-0j'
+        assert repr(complex(-0.0,  0.0)) == '(-0+0j)'
+        assert repr(complex(-0.0, -0.0)) == '(-0-0j)'
         assert repr(complex(1e45)) == "(" + repr(1e45) + "+0j)"
+        assert repr(complex(1e200*1e200)) == '(inf+0j)'
 
     def test_neg(self):
         h = self.helper



More information about the Pypy-commit mailing list