[pypy-svn] r76010 - in pypy/trunk/pypy/rlib: . test

arigo at codespeak.net arigo at codespeak.net
Thu Jul 8 10:51:05 CEST 2010


Author: arigo
Date: Thu Jul  8 10:51:03 2010
New Revision: 76010

Modified:
   pypy/trunk/pypy/rlib/rarithmetic.py
   pypy/trunk/pypy/rlib/test/test_rarithmetic.py
Log:
Fix the pure Python implementation of ovfcheck_float_to_int.


Modified: pypy/trunk/pypy/rlib/rarithmetic.py
==============================================================================
--- pypy/trunk/pypy/rlib/rarithmetic.py	(original)
+++ pypy/trunk/pypy/rlib/rarithmetic.py	Thu Jul  8 10:51:03 2010
@@ -113,14 +113,14 @@
     "NOT_RPYTHON"
     return _local_ovfcheck(int(long(a) << b))
 
-FL_MAXINT = float(LONG_TEST-1)
-FL_MININT = float(-LONG_TEST)
-
 def ovfcheck_float_to_int(x):
-    _, intp = math.modf(x)
-    if FL_MININT < intp < FL_MAXINT:
-        return int(intp)
-    raise OverflowError
+    try:
+        result = int(int(x))  # -2147483648.0 => -2147483648L => -2147483648
+    except (OverflowError, ValueError): # ValueError for int(nan) on Py>=2.6
+        raise OverflowError
+    if type(result) is not int:
+        raise OverflowError
+    return result
 
 def compute_restype(self_type, other_type):
     if self_type is other_type:

Modified: pypy/trunk/pypy/rlib/test/test_rarithmetic.py
==============================================================================
--- pypy/trunk/pypy/rlib/test/test_rarithmetic.py	(original)
+++ pypy/trunk/pypy/rlib/test/test_rarithmetic.py	Thu Jul  8 10:51:03 2010
@@ -271,26 +271,29 @@
     assert ovfcheck_float_to_int(13.0) == 13
     assert ovfcheck_float_to_int(-1.0) == -1
     assert ovfcheck_float_to_int(-13.0) == -13
-    #  strange things happening for float to int on 64 bit
-    maxint32 = 2 ** 31 - 1
-    assert ovfcheck_float_to_int(float(maxint32-1)) == maxint32-1
-    #assert ovfcheck_float_to_int(float(maxint32)) == maxint32
-    assert ovfcheck_float_to_int(float(-maxint32)) == -maxint32
-    #assert ovfcheck_float_to_int(float(-maxint32-1)) == -maxint32-1
-
-    try:
-        ovfcheck_float_to_int(float(-sys.maxint-1)-1)
-    except OverflowError:
-        pass
-    else:
-        assert False
-
-    try:
-        ovfcheck_float_to_int(float(sys.maxint)+1)
-    except OverflowError:
-        pass
-    else:
-        assert False
+
+    # strange things happening for float to int on 64 bit:
+    # int(float(i)) != i  because of rounding issues
+    x = sys.maxint
+    while int(float(x)) > sys.maxint:
+        x -= 1
+    assert ovfcheck_float_to_int(float(x)) == int(float(x))
+
+    x = sys.maxint + 1
+    while int(float(x)) <= sys.maxint:
+        x += 1
+    py.test.raises(OverflowError, ovfcheck_float_to_int, x)
+
+    x = -sys.maxint-1
+    while int(float(x)) < -sys.maxint-1:
+        x += 1
+    assert ovfcheck_float_to_int(float(x)) == int(float(x))
+
+    x = -sys.maxint-1
+    while int(float(x)) >= -sys.maxint-1:
+        x -= 1
+    py.test.raises(OverflowError, ovfcheck_float_to_int, x)
+
 
 def test_abs():
     assert type(abs(r_longlong(1))) is r_longlong



More information about the Pypy-commit mailing list