[pypy-svn] r76096 - in pypy/branch/fast-forward/pypy/rlib: . test

benjamin at codespeak.net benjamin at codespeak.net
Sat Jul 10 19:18:38 CEST 2010


Author: benjamin
Date: Sat Jul 10 19:18:37 2010
New Revision: 76096

Modified:
   pypy/branch/fast-forward/pypy/rlib/rarithmetic.py
   pypy/branch/fast-forward/pypy/rlib/test/test_rarithmetic.py
Log:
add replacement for libm round()

Modified: pypy/branch/fast-forward/pypy/rlib/rarithmetic.py
==============================================================================
--- pypy/branch/fast-forward/pypy/rlib/rarithmetic.py	(original)
+++ pypy/branch/fast-forward/pypy/rlib/rarithmetic.py	Sat Jul 10 19:18:37 2010
@@ -143,6 +143,15 @@
             return (u - 1.) * x / math.log(u)
         return math.exp(x) - 1.
 
+def round_away(x):
+    # round() from libm
+    absx = abs(x)
+    if absx - math.floor(absx) >= .5:
+        r = math.ceil(absx)
+    else:
+        r = math.floor(absx)
+    return copysign(r, x)
+
 def intmask(n):
     if isinstance(n, int):
         return int(n)   # possibly bool->int

Modified: pypy/branch/fast-forward/pypy/rlib/test/test_rarithmetic.py
==============================================================================
--- pypy/branch/fast-forward/pypy/rlib/test/test_rarithmetic.py	(original)
+++ pypy/branch/fast-forward/pypy/rlib/test/test_rarithmetic.py	Sat Jul 10 19:18:37 2010
@@ -379,3 +379,13 @@
     assert copysign(-1, -1) == -1
     assert copysign(1, -1) == -1
     assert copysign(1, -0.) == -1
+
+def test_round_away():
+    assert round_away(.1) == 0.
+    assert round_away(.5) == 1.
+    assert round_away(.7) == 1.
+    assert round_away(1.) == 1.
+    assert round_away(-.5) == -1.
+    assert round_away(-.1) == 0.
+    assert round_away(-.7) == -1.
+    assert round_away(0.) == 0.



More information about the Pypy-commit mailing list