[pypy-svn] r72692 - in pypy/trunk/pypy/objspace/std: . test

arigo at codespeak.net arigo at codespeak.net
Wed Mar 24 09:49:35 CET 2010


Author: arigo
Date: Wed Mar 24 09:49:33 2010
New Revision: 72692

Modified:
   pypy/trunk/pypy/objspace/std/floatobject.py
   pypy/trunk/pypy/objspace/std/test/test_floatobject.py
Log:
Be more precise in handling ValueErrors from math.pow() in
'float**float'.  This improves error messages in one case,
and allows '-1.0**largenum' to succeed (and return 1.0) even
though math.pow() fails on the same arguments.  That's a big
strange, but the way it is in CPython 2.6.


Modified: pypy/trunk/pypy/objspace/std/floatobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/floatobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/floatobject.py	Wed Mar 24 09:49:33 2010
@@ -318,7 +318,20 @@
         raise FailedToImplementArgs(space.w_OverflowError,
                                     space.wrap("float power"))
     except ValueError:
-        if x == 0.0 and y < 0.0:
+        # special case: "(-1.0) ** bignum" should not raise ValueError,
+        # unlike "math.pow(-1.0, bignum)".  See http://mail.python.org/
+        # -           pipermail/python-bugs-list/2003-March/016795.html
+        if x < 0.0:
+            if math.floor(y) != y:
+                raise OperationError(space.w_ValueError,
+                                     space.wrap("negative number cannot be "
+                                                "raised to a fractional power"))
+            if x == -1.0:
+                if math.floor(y * 0.5) * 2.0 == y:
+                     return space.wrap(1.0)
+                else:
+                     return space.wrap( -1.0)
+        elif x == 0.0 and y < 0.0:
             raise OperationError(space.w_ZeroDivisionError,
                 space.wrap("0.0 cannot be raised to a negative power"))
         raise OperationError(space.w_ValueError,

Modified: pypy/trunk/pypy/objspace/std/test/test_floatobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/test/test_floatobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/test/test_floatobject.py	Wed Mar 24 09:49:33 2010
@@ -146,8 +146,7 @@
         raises(ValueError, pw, -1.0, 0.5)
         assert pw(-1.0, 2.0) == 1.0
         assert pw(-1.0, 3.0) == -1.0
-        #assert pw(-1.0, 1e200) == 1.0 -- either 1.0, or ValueError, are
-        #                              -- acceptable answers IMHO
+        assert pw(-1.0, 1e200) == 1.0
 
     def test_pow_neg_base(self):
         def pw(x, y):



More information about the Pypy-commit mailing list