FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Issue #10596: Fix float.__mod__ to have the same behaviour as · python/cpython@d2a9b20 · GitHub

Repository navigation

Commit d2a9b20

Browse files
committed
Issue #10596: Fix float.__mod__ to have the same behaviour as
float.__divmod__ with respect to signed zeros.
1 parent 7f4bc00 commit d2a9b20

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

‎Lib/test/test_float.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,26 @@ def assertEqualAndEqualSign(self, a, b):
196196
# distingishes -0.0 and 0.0.
197197
self.assertEqual((a, copysign(1.0, a)), (b, copysign(1.0, b)))
198198

199+
@requires_IEEE_754
200+
def test_float_mod(self):
201+
# Check behaviour of % operator for IEEE 754 special cases.
202+
# In particular, check signs of zeros.
203+
mod = operator.mod
204+
205+
self.assertEqualAndEqualSign(mod(-1.0, 1.0), 0.0)
206+
self.assertEqualAndEqualSign(mod(-1e-100, 1.0), 1.0)
207+
self.assertEqualAndEqualSign(mod(-0.0, 1.0), 0.0)
208+
self.assertEqualAndEqualSign(mod(0.0, 1.0), 0.0)
209+
self.assertEqualAndEqualSign(mod(1e-100, 1.0), 1e-100)
210+
self.assertEqualAndEqualSign(mod(1.0, 1.0), 0.0)
211+
212+
self.assertEqualAndEqualSign(mod(-1.0, -1.0), -0.0)
213+
self.assertEqualAndEqualSign(mod(-1e-100, -1.0), -1e-100)
214+
self.assertEqualAndEqualSign(mod(-0.0, -1.0), -0.0)
215+
self.assertEqualAndEqualSign(mod(0.0, -1.0), -0.0)
216+
self.assertEqualAndEqualSign(mod(1e-100, -1.0), -1.0)
217+
self.assertEqualAndEqualSign(mod(1.0, -1.0), -0.0)
218+
199219
@requires_IEEE_754
200220
def test_float_pow(self):
201221
# test builtin pow and ** operator for IEEE 754 special cases.

‎Misc/NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.2 Beta 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #10596: Fix float.__mod__ to have the same behaviour as
14+
float.__divmod__ with respect to signed zeros. -4.0 % 4.0 should be
15+
0.0, not -0.0.
16+
1317
- Issue #1772833: Add the -q command-line option to suppress copyright
1418
and version output in interactive mode.
1519

‎Objects/floatobject.c‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,10 +600,20 @@ float_rem(PyObject *v, PyObject *w)
600600
}
601601
PyFPE_START_PROTECT("modulo", return 0)
602602
mod = fmod(vx, wx);
603-
/* note: checking mod*wx < 0 is incorrect -- underflows to
604-
0 if wx < sqrt(smallest nonzero double) */
605-
if (mod && ((wx < 0) != (mod < 0))) {
606-
mod += wx;
603+
if (mod) {
604+
/* ensure the remainder has the same sign as the denominator */
605+
if ((wx < 0) != (mod < 0)) {
606+
mod += wx;
607+
}
608+
}
609+
else {
610+
/* the remainder is zero, and in the presence of signed zeroes
611+
fmod returns different results across platforms; ensure
612+
it has the same sign as the denominator; we'd like to do
613+
"mod = wx * 0.0", but that may get optimized away */
614+
mod *= mod; /* hide "mod = +0" from optimizer */
615+
if (wx < 0.0)
616+
mod = -mod;
607617
}
608618
PyFPE_END_PROTECT(mod)
609619
return PyFloat_FromDouble(mod);

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL