[pypy-svn] pypy default: use int(int(frac)) as a workaround for a CPython bug:

arigo commits-noreply at bitbucket.org
Mon Feb 7 18:25:46 CET 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r41680:9eda9264b36a
Date: 2011-02-07 18:25 +0100
http://bitbucket.org/pypy/pypy/changeset/9eda9264b36a/

Log:	use int(int(frac)) as a workaround for a CPython bug: with frac ==
	2147483647.0, int(frac) == 2147483647L

diff --git a/pypy/rlib/test/test_rbigint.py b/pypy/rlib/test/test_rbigint.py
--- a/pypy/rlib/test/test_rbigint.py
+++ b/pypy/rlib/test/test_rbigint.py
@@ -228,6 +228,9 @@
         x = 12345.6789e200
         x *= x
         assert raises(OverflowError, rbigint.fromfloat, x)
+        #
+        f1 = rbigint.fromfloat(9007199254740991.0)
+        assert f1.tolong() == 9007199254740991
 
     def test_eq(self):
         x = 5858393919192332223L

diff --git a/pypy/rlib/rbigint.py b/pypy/rlib/rbigint.py
--- a/pypy/rlib/rbigint.py
+++ b/pypy/rlib/rbigint.py
@@ -161,25 +161,25 @@
 
     def fromfloat(dval):
         """ Create a new bigint object from a float """
-        neg = 0
+        sign = 1
         if isinf(dval) or isnan(dval):
             raise OverflowError
         if dval < 0.0:
-            neg = 1
+            sign = -1
             dval = -dval
         frac, expo = math.frexp(dval) # dval = frac*2**expo; 0.0 <= frac < 1.0
         if expo <= 0:
             return rbigint()
         ndig = (expo-1) // SHIFT + 1 # Number of 'digits' in result
-        v = rbigint([NULLDIGIT] * ndig, 1)
+        v = rbigint([NULLDIGIT] * ndig, sign)
         frac = math.ldexp(frac, (expo-1) % SHIFT + 1)
         for i in range(ndig-1, -1, -1):
-            bits = int(frac)
+            # use int(int(frac)) as a workaround for a CPython bug:
+            # with frac == 2147483647.0, int(frac) == 2147483647L
+            bits = int(int(frac))
             v.setdigit(i, bits)
             frac -= float(bits)
             frac = math.ldexp(frac, SHIFT)
-        if neg:
-            v.sign = -1
         return v
     fromfloat = staticmethod(fromfloat)
 


More information about the Pypy-commit mailing list