summaryrefslogtreecommitdiffstats
path: root/Objects/floatobject.c
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2010-12-04 12:25:30 (GMT)
committerMark Dickinson <dickinsm@gmail.com>2010-12-04 12:25:30 (GMT)
commitd2a9b20efaa15b0152856a545b0206af21d06c5e (patch)
tree7efc34a2bef5d875d674b11ab5078d12c325383a /Objects/floatobject.c
parent7f4bc00d9f85f9328e020ddcab35ddd6f120d665 (diff)
downloadcpython-d2a9b20efaa15b0152856a545b0206af21d06c5e.zip
cpython-d2a9b20efaa15b0152856a545b0206af21d06c5e.tar.gz
cpython-d2a9b20efaa15b0152856a545b0206af21d06c5e.tar.bz2
Issue #10596: Fix float.__mod__ to have the same behaviour as
float.__divmod__ with respect to signed zeros.
Diffstat (limited to 'Objects/floatobject.c')
-rw-r--r--Objects/floatobject.c18
1 files changed, 14 insertions, 4 deletions
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index d5a3c7e..7cbb240 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -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);