diff Objects/floatobject.c @ 66698:573a67276217

Issue #10596: Fix float.__mod__ to have the same behaviour as float.__divmod__ with respect to signed zeros.
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 04 Dec 2010 12:25:30 +0000
parents 8cabcf4f9033
children b32d19eb4338
line wrap: on
line diff
--- a/Objects/floatobject.c	Sat Dec 04 12:20:57 2010 +0000
+++ b/Objects/floatobject.c	Sat Dec 04 12:25:30 2010 +0000
@@ -600,10 +600,20 @@
     }
     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);