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

arigo at codespeak.net arigo at codespeak.net
Mon Mar 22 16:12:21 CET 2010


Author: arigo
Date: Mon Mar 22 16:12:19 2010
New Revision: 72564

Modified:
   pypy/trunk/pypy/objspace/std/floatobject.py
   pypy/trunk/pypy/objspace/std/test/test_floatobject.py
Log:
Say that x**y on floats delegates directly to math.pow(x,y).
That seems the sanest implementation, and I don't care
enough that on CPython 2.6,  math.pow(-1.0, 1e200)  gives
ValueError but  (-1.0)**1e200  gives 1.0.


Modified: pypy/trunk/pypy/objspace/std/floatobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/floatobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/floatobject.py	Mon Mar 22 16:12:19 2010
@@ -302,9 +302,6 @@
     return space.newtuple(_divmod_w(space, w_float1, w_float2))
 
 def pow__Float_Float_ANY(space, w_float1, w_float2, thirdArg):
-    # XXX it makes sense to do more here than in the backend
-    # about sorting out errors!
-
     # This raises FailedToImplement in cases like overflow where a
     # (purely theoretical) big-precision float implementation would have
     # a chance to give a result, and directly OperationError for errors
@@ -314,34 +311,18 @@
             "pow() 3rd argument not allowed unless all arguments are integers"))
     x = w_float1.floatval
     y = w_float2.floatval
-    z = 1.0
-    if y == 0.0:
-        z = 1.0
-    elif x == 0.0:
-        if y < 0.0:
+    try:
+        # We delegate to our implementation of math.pow() the error detection.
+        z = math.pow(x,y)
+    except OverflowError:
+        raise FailedToImplementArgs(space.w_OverflowError,
+                                    space.wrap("float power"))
+    except ValueError:
+        if x == 0.0 and y < 0.0:
             raise OperationError(space.w_ZeroDivisionError,
-                                    space.wrap("0.0 cannot be raised to a negative power"))
-        z = 0.0
-    else:
-        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:
-                # xxx what if y is infinity or a NaN
-                if math.floor(y * 0.5) * 2.0 == y:
-                    return space.wrap(1.0)
-                else:
-                    return space.wrap( -1.0)
-#        else:
-        try:
-            z = math.pow(x,y)
-        except OverflowError:
-            raise FailedToImplementArgs(space.w_OverflowError, space.wrap("float power"))
-        except ValueError:
-            raise FailedToImplementArgs(space.w_ValueError, space.wrap("float power")) # xxx
-
+                space.wrap("0.0 cannot be raised to a negative power"))
+        raise OperationError(space.w_ValueError,
+                             space.wrap("float power"))
     return W_FloatObject(z)
 
 

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	Mon Mar 22 16:12:19 2010
@@ -146,7 +146,8 @@
         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
+        #assert pw(-1.0, 1e200) == 1.0 -- either 1.0, or ValueError, are
+        #                              -- acceptable answers IMHO
 
     def test_pow_neg_base(self):
         def pw(x, y):



More information about the Pypy-commit mailing list