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

benjamin at codespeak.net benjamin at codespeak.net
Thu Jul 1 20:51:21 CEST 2010


Author: benjamin
Date: Thu Jul  1 20:51:19 2010
New Revision: 75736

Modified:
   pypy/branch/fast-forward/pypy/rlib/rarithmetic.py
   pypy/branch/fast-forward/pypy/rlib/test/test_rarithmetic.py
Log:
import useful things from the math module when possible; add copysign

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	Thu Jul  1 20:51:19 2010
@@ -53,14 +53,25 @@
 INFINITY = 1e200 * 1e200
 NAN = INFINITY / INFINITY
 
-def isinf(x):
-    return x != 0.0 and x / 2 == x
+try:
+    from math import isinf, isnan, copysign
+except ImportError:
+    def isinf(x):
+        return x != 0.0 and x / 2 == x
+
+    # To get isnan, working x-platform and both on 2.3 and 2.4, is a
+    # horror.  I think this works (for reasons I don't really want to talk
+    # about), and probably when implemented on top of pypy, too.
+    def isnan(v):
+        return v != v*1.0 or (v == 1.0 and v == 2.0)
+
+    def copysign(x, y):
+        """Return x with the sign of y"""
+        if y > 0. or (y == 0. and math.atan2(y, -1.) > 0.):
+            return math.fabs(x)
+        else:
+            return -math.fabs(x)
 
-# To get isnan, working x-platform and both on 2.3 and 2.4, is a
-# horror.  I think this works (for reasons I don't really want to talk
-# about), and probably when implemented on top of pypy, too.
-def isnan(v):
-    return v != v*1.0 or (v == 1.0 and v == 2.0)
 
 def intmask(n):
     if isinstance(n, 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	Thu Jul  1 20:51:19 2010
@@ -372,3 +372,10 @@
 def test_int_real_union():
     from pypy.rpython.lltypesystem.rffi import r_int_real
     assert compute_restype(r_int_real, r_int_real) is r_int_real
+
+def test_copysign():
+    assert copysign(1, 1) == 1
+    assert copysign(-1, 1) == 1
+    assert copysign(-1, -1) == -1
+    assert copysign(1, -1) == -1
+    assert copysign(1, -0.) == -1



More information about the Pypy-commit mailing list