[pypy-svn] r75856 - in pypy/branch/fast-forward/pypy/module/math: . test

benjamin at codespeak.net benjamin at codespeak.net
Mon Jul 5 20:51:02 CEST 2010


Author: benjamin
Date: Mon Jul  5 20:50:59 2010
New Revision: 75856

Modified:
   pypy/branch/fast-forward/pypy/module/math/__init__.py
   pypy/branch/fast-forward/pypy/module/math/interp_math.py
   pypy/branch/fast-forward/pypy/module/math/test/test_math.py
Log:
add math.factorial

Modified: pypy/branch/fast-forward/pypy/module/math/__init__.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/math/__init__.py	(original)
+++ pypy/branch/fast-forward/pypy/module/math/__init__.py	Mon Jul  5 20:50:59 2010
@@ -39,5 +39,6 @@
        'isnan'          : 'interp_math.isnan',
        'trunc'          : 'interp_math.trunc',
        'fsum'           : 'interp_math.fsum',
+       'factorial'      : 'interp_math.factorial',
 }
 

Modified: pypy/branch/fast-forward/pypy/module/math/interp_math.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/math/interp_math.py	(original)
+++ pypy/branch/fast-forward/pypy/module/math/interp_math.py	Mon Jul  5 20:50:59 2010
@@ -379,3 +379,19 @@
                 hi = v
     return space.wrap(hi)
 fsum.unwrap_spec = [ObjSpace, W_Root]
+
+def factorial(space, w_x):
+    """Find x!."""
+    if space.isinstance_w(w_x, space.w_float):
+        fl = space.float_w(w_x)
+        if math.floor(fl) != fl:
+            raise OperationError(space.w_ValueError,
+                                 space.wrap("float arguments must be integral"))
+        w_x = space.long(w_x)
+    x = space.int_w(w_x)
+    if x < 0:
+        raise OperationError(space.w_ValueError, space.wrap("x must be >= 0"))
+    w_res = space.wrap(1)
+    for i in range(1, x + 1):
+        w_res = space.mul(w_res, space.wrap(i))
+    return w_res

Modified: pypy/branch/fast-forward/pypy/module/math/test/test_math.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/math/test/test_math.py	(original)
+++ pypy/branch/fast-forward/pypy/module/math/test/test_math.py	Mon Jul  5 20:50:59 2010
@@ -41,9 +41,6 @@
         assert math.ldexp(float("inf"), -10**20) == float("inf")
 
     def test_fsum(self):
-        # Python version of math.fsum, for comparison.  Uses a
-        # different algorithm based on frexp, ldexp and integer
-        # arithmetic.
         import math
 
         test_values = [
@@ -77,3 +74,14 @@
                 py.test.fail("test %d failed: got ValueError, expected %r "
                           "for math.fsum(%.100r)" % (i, expected, vals))
             assert actual == expected
+
+    def test_factorial(self):
+        import math
+        assert math.factorial(0) == 1
+        assert math.factorial(1) == 1
+        assert math.factorial(2) == 2
+        assert math.factorial(5) == 120
+        assert math.factorial(5.) == 120
+        raises(ValueError, math.factorial, -1)
+        raises(ValueError, math.factorial, -1.)
+        raises(ValueError, math.factorial, 1.1)



More information about the Pypy-commit mailing list