Issue #10596: Fix float.__mod__ to have the same behaviour as
authorMark Dickinson <dickinsm@gmail.com>
Sat, 4 Dec 2010 12:25:30 +0000 (12:25 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Sat, 4 Dec 2010 12:25:30 +0000 (12:25 +0000)
float.__divmod__ with respect to signed zeros.

Lib/test/test_float.py
Misc/NEWS
Objects/floatobject.c

index 9bcd63dd2302cb84ef703cc28bbd51c09824072c..ddf93bcf8773f269923951d61e27e9139b029479 100644 (file)
@@ -196,6 +196,26 @@ class GeneralFloatCases(unittest.TestCase):
         # distingishes -0.0 and 0.0.
         self.assertEqual((a, copysign(1.0, a)), (b, copysign(1.0, b)))
 
+    @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)
+
     @requires_IEEE_754
     def test_float_pow(self):
         # test builtin pow and ** operator for IEEE 754 special cases.
index 3f36425c24fd6c5e5880343679e5df44fdda5e67..c5a525f5a72e65d80ad52333b70022a00a7bd7e1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@ What's New in Python 3.2 Beta 1?
 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.
+
 - Issue #1772833: Add the -q command-line option to suppress copyright
   and version output in interactive mode.
 
index d5a3c7ef231c6019cca8530a1bcaf3a50ba15051..7cbb240b38701ea414f9bec8fb594023d31a8c99 100644 (file)
@@ -600,10 +600,20 @@ float_rem(PyObject *v, PyObject *w)
     }
     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);