[Python-checkins] r87034 - in python/branches/release31-maint: Lib/test/test_float.py Misc/NEWS Objects/floatobject.c

mark.dickinson python-checkins at python.org
Sat Dec 4 13:38:19 CET 2010


Author: mark.dickinson
Date: Sat Dec  4 13:38:19 2010
New Revision: 87034

Log:
Merged revisions 87032 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r87032 | mark.dickinson | 2010-12-04 12:25:30 +0000 (Sat, 04 Dec 2010) | 3 lines
  
  Issue #10596: Fix float.__mod__ to have the same behaviour as
  float.__divmod__ with respect to signed zeros.
........


Modified:
   python/branches/release31-maint/   (props changed)
   python/branches/release31-maint/Lib/test/test_float.py
   python/branches/release31-maint/Misc/NEWS
   python/branches/release31-maint/Objects/floatobject.c

Modified: python/branches/release31-maint/Lib/test/test_float.py
==============================================================================
--- python/branches/release31-maint/Lib/test/test_float.py	(original)
+++ python/branches/release31-maint/Lib/test/test_float.py	Sat Dec  4 13:38:19 2010
@@ -231,6 +231,26 @@
             self.assertTrue(d == d, "{%r : None} not equal to itself" % f)
 
 
+    @requires_IEEE_754
+    def test_float_mod(self):
+        # Check behaviour of % operator for IEEE 754 special cases.
+        # In particular, check signs of zeros.
+        mod = operator.mod
+
+        self.assertEqualAndEqualSign(mod(-1.0, 1.0), 0.0)
+        self.assertEqualAndEqualSign(mod(-1e-100, 1.0), 1.0)
+        self.assertEqualAndEqualSign(mod(-0.0, 1.0), 0.0)
+        self.assertEqualAndEqualSign(mod(0.0, 1.0), 0.0)
+        self.assertEqualAndEqualSign(mod(1e-100, 1.0), 1e-100)
+        self.assertEqualAndEqualSign(mod(1.0, 1.0), 0.0)
+
+        self.assertEqualAndEqualSign(mod(-1.0, -1.0), -0.0)
+        self.assertEqualAndEqualSign(mod(-1e-100, -1.0), -1e-100)
+        self.assertEqualAndEqualSign(mod(-0.0, -1.0), -0.0)
+        self.assertEqualAndEqualSign(mod(0.0, -1.0), -0.0)
+        self.assertEqualAndEqualSign(mod(1e-100, -1.0), -1.0)
+        self.assertEqualAndEqualSign(mod(1.0, -1.0), -0.0)
+
 
 class FormatFunctionsTestCase(unittest.TestCase):
 

Modified: python/branches/release31-maint/Misc/NEWS
==============================================================================
--- python/branches/release31-maint/Misc/NEWS	(original)
+++ python/branches/release31-maint/Misc/NEWS	Sat Dec  4 13:38:19 2010
@@ -10,6 +10,10 @@
 Core and Builtins
 -----------------
 
+- Issue #10596: Fix float.__mod__ to have the same behaviour as
+  float.__divmod__ with respect to signed zeros.  -4.0 % 4.0 should be
+  0.0, not -0.0.
+
 Library
 -------
 

Modified: python/branches/release31-maint/Objects/floatobject.c
==============================================================================
--- python/branches/release31-maint/Objects/floatobject.c	(original)
+++ python/branches/release31-maint/Objects/floatobject.c	Sat Dec  4 13:38:19 2010
@@ -600,10 +600,20 @@
 #endif
     PyFPE_START_PROTECT("modulo", return 0)
     mod = fmod(vx, wx);
-    /* note: checking mod*wx < 0 is incorrect -- underflows to
-       0 if wx < sqrt(smallest nonzero double) */
-    if (mod && ((wx < 0) != (mod < 0))) {
-        mod += wx;
+    if (mod) {
+        /* ensure the remainder has the same sign as the denominator */
+        if ((wx < 0) != (mod < 0)) {
+            mod += wx;
+        }
+    }
+    else {
+        /* the remainder is zero, and in the presence of signed zeroes
+           fmod returns different results across platforms; ensure
+           it has the same sign as the denominator; we'd like to do
+           "mod = wx * 0.0", but that may get optimized away */
+        mod *= mod;  /* hide "mod = +0" from optimizer */
+        if (wx < 0.0)
+            mod = -mod;
     }
     PyFPE_END_PROTECT(mod)
     return PyFloat_FromDouble(mod);


More information about the Python-checkins mailing list