Merged revisions 87032 via svnmerge from
svn+ssh://pythondev@svn.python.org/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. ........
This commit is contained in:
parent
c1d0ed2907
commit
ecf8ec67ce
|
@ -161,6 +161,26 @@ class GeneralFloatCases(unittest.TestCase):
|
||||||
# distingishes -0.0 and 0.0.
|
# distingishes -0.0 and 0.0.
|
||||||
self.assertEqual((a, copysign(1.0, a)), (b, copysign(1.0, b)))
|
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
|
@requires_IEEE_754
|
||||||
def test_float_pow(self):
|
def test_float_pow(self):
|
||||||
# test builtin pow and ** operator for IEEE 754 special cases.
|
# test builtin pow and ** operator for IEEE 754 special cases.
|
||||||
|
|
|
@ -9,6 +9,10 @@ What's New in Python 2.7.2?
|
||||||
Core and Builtins
|
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.
|
||||||
|
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
|
|
@ -716,11 +716,21 @@ float_rem(PyObject *v, PyObject *w)
|
||||||
#endif
|
#endif
|
||||||
PyFPE_START_PROTECT("modulo", return 0)
|
PyFPE_START_PROTECT("modulo", return 0)
|
||||||
mod = fmod(vx, wx);
|
mod = fmod(vx, wx);
|
||||||
/* note: checking mod*wx < 0 is incorrect -- underflows to
|
if (mod) {
|
||||||
0 if wx < sqrt(smallest nonzero double) */
|
/* ensure the remainder has the same sign as the denominator */
|
||||||
if (mod && ((wx < 0) != (mod < 0))) {
|
if ((wx < 0) != (mod < 0)) {
|
||||||
mod += wx;
|
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)
|
PyFPE_END_PROTECT(mod)
|
||||||
return PyFloat_FromDouble(mod);
|
return PyFloat_FromDouble(mod);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue