Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0169af1

Browse files
committed
Merged revisions 87032 via svnmerge from
svn+ssh://[email protected]/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. ........
1 parent 3315438 commit 0169af1

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

Lib/test/test_float.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,31 @@ def test_float_containment(self):
240240
self.assertTrue(s == s, "{%r} not equal to itself" % f)
241241
self.assertTrue(d == d, "{%r : None} not equal to itself" % f)
242242

243+
def assertEqualAndEqualSign(self, a, b):
244+
# fail unless a == b and a and b have the same sign bit;
245+
# the only difference from assertEqual is that this test
246+
# distingishes -0.0 and 0.0.
247+
self.assertEqual((a, copysign(1.0, a)), (b, copysign(1.0, b)))
248+
249+
@requires_IEEE_754
250+
def test_float_mod(self):
251+
# Check behaviour of % operator for IEEE 754 special cases.
252+
# In particular, check signs of zeros.
253+
mod = operator.mod
254+
255+
self.assertEqualAndEqualSign(mod(-1.0, 1.0), 0.0)
256+
self.assertEqualAndEqualSign(mod(-1e-100, 1.0), 1.0)
257+
self.assertEqualAndEqualSign(mod(-0.0, 1.0), 0.0)
258+
self.assertEqualAndEqualSign(mod(0.0, 1.0), 0.0)
259+
self.assertEqualAndEqualSign(mod(1e-100, 1.0), 1e-100)
260+
self.assertEqualAndEqualSign(mod(1.0, 1.0), 0.0)
261+
262+
self.assertEqualAndEqualSign(mod(-1.0, -1.0), -0.0)
263+
self.assertEqualAndEqualSign(mod(-1e-100, -1.0), -1e-100)
264+
self.assertEqualAndEqualSign(mod(-0.0, -1.0), -0.0)
265+
self.assertEqualAndEqualSign(mod(0.0, -1.0), -0.0)
266+
self.assertEqualAndEqualSign(mod(1e-100, -1.0), -1.0)
267+
self.assertEqualAndEqualSign(mod(1.0, -1.0), -0.0)
243268

244269

245270
class FormatFunctionsTestCase(unittest.TestCase):

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.1.4?
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
Library
1418
-------
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
#endif
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)