[pypy-svn] pypy default: long.truediv should work identically regardless of the sign

amauryfa commits-noreply at bitbucket.org
Fri Feb 11 10:10:20 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: 
Changeset: r41814:a73b5d006110
Date: 2011-02-11 10:04 +0100
http://bitbucket.org/pypy/pypy/changeset/a73b5d006110/

Log:	long.truediv should work identically regardless of the sign (rshift
	does not: -7 >> 1 == -4)

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
@@ -58,10 +58,16 @@
         f = op1.truediv(op2)
         assert f == 1.7976931348623157e+308     # exactly
 
+        op1 = rbigint.fromlong(overflowing-1)
         op2 = rbigint.fromlong(-1)
         f = op1.truediv(op2)
         assert f == -1.7976931348623157e+308    # exactly
 
+        op1 = rbigint.fromlong(-overflowing+1)
+        op2 = rbigint.fromlong(-1)
+        f = op1.truediv(op2)
+        assert f == +1.7976931348623157e+308    # exactly
+
         op1 = rbigint.fromlong(overflowing)
         op2 = rbigint.fromlong(1)
         py.test.raises(OverflowError, op1.truediv, op2)

diff --git a/pypy/rlib/rbigint.py b/pypy/rlib/rbigint.py
--- a/pypy/rlib/rbigint.py
+++ b/pypy/rlib/rbigint.py
@@ -511,12 +511,12 @@
         z._normalize()
         return z
 
-    def rshift(self, int_other):
+    def rshift(self, int_other, dont_invert=False):
         if int_other < 0:
             raise ValueError("negative shift count")
         elif int_other == 0:
             return self
-        if self.sign == -1:
+        if self.sign == -1 and not dont_invert:
             a1 = self.invert()
             a2 = a1.rshift(int_other)
             return a2.invert()
@@ -1497,7 +1497,7 @@
     if shift <= 0:
         x = a.lshift(-shift)
     else:
-        x = a.rshift(shift)
+        x = a.rshift(shift, dont_invert=True)
         # set inexact if any of the bits shifted out is nonzero
         if not a.eq(x.lshift(shift)):
             inexact = True


More information about the Pypy-commit mailing list