[pypy-svn] r15939 - in pypy/dist/pypy/objspace/std: . test
pedronis at codespeak.net
pedronis at codespeak.net
Wed Aug 10 21:27:17 CEST 2005
Author: pedronis
Date: Wed Aug 10 21:27:15 2005
New Revision: 15939
Modified:
pypy/dist/pypy/objspace/std/floatobject.py
pypy/dist/pypy/objspace/std/test/test_floatobject.py
Log:
issue91 in-progress
put the sanity logic for pow in floatobject.py itself
the backend now only needs to appropriate code to raise ValueError|OverflowError for math.* implementations
(ll_math.h for genc)
Modified: pypy/dist/pypy/objspace/std/floatobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/floatobject.py (original)
+++ pypy/dist/pypy/objspace/std/floatobject.py Wed Aug 10 21:27:15 2005
@@ -278,15 +278,33 @@
"pow() 3rd argument not allowed unless all arguments are integers"))
x = w_float1.floatval
y = w_float2.floatval
- try:
- z = x ** y
- except OverflowError:
- raise FailedToImplement(space.w_OverflowError, space.wrap("float power"))
- except ValueError, e:
- raise FailedToImplement(space.w_ValueError, space.wrap(str(e))) # xxx
- except ZeroDivisionError, e:
- raise OperationError(space.w_ZeroDivisionError,
- space.wrap("0.0 cannot be raised to a negative power"))
+ z = 1.0
+ if y == 0.0:
+ z = 1.0
+ elif x == 0.0:
+ if y < 0.0:
+ raise FailedToImplement(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 FailedToImplement(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 FailedToImplement(space.w_OverflowError, space.wrap("float power"))
+ except ValueError:
+ raise FailedToImplement(space.w_ValueError, space.wrap("float power")) # xxx
return W_FloatObject(space, z)
Modified: pypy/dist/pypy/objspace/std/test/test_floatobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_floatobject.py (original)
+++ pypy/dist/pypy/objspace/std/test/test_floatobject.py Wed Aug 10 21:27:15 2005
@@ -106,3 +106,23 @@
def test_getnewargs(self):
assert 0.0 .__getnewargs__() == (0.0,)
+
+
+ def test_pow(self):
+ def pw(x, y):
+ return x ** y
+ def espeq(x, y):
+ return not abs(x-y) > 1e05
+ raises(ZeroDivisionError, pw, 0.0, -1)
+ assert pw(0, 0.5) == 0.0
+ assert espeq(pw(4.0, 0.5), 2.0)
+ assert pw(4.0, 0) == 1.0
+ assert pw(-4.0, 0) == 1.0
+ 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
+
+
+
+
More information about the Pypy-commit
mailing list