diff options
author | Tim Peters <tim.peters@gmail.com> | 2001-11-01 23:12:27 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2001-11-01 23:12:27 (GMT) |
commit | d2e40d6691899d5969b68cdf967d7b392b4e6e6b (patch) | |
tree | d4623700d71dd98e1a0ab86c838b4170b9a54ad1 /Objects/floatobject.c | |
parent | faf0cd21edde392d4d30e2e4f7d9fa2fb9973b71 (diff) | |
download | cpython-d2e40d6691899d5969b68cdf967d7b392b4e6e6b.zip cpython-d2e40d6691899d5969b68cdf967d7b392b4e6e6b.tar.gz cpython-d2e40d6691899d5969b68cdf967d7b392b4e6e6b.tar.bz2 |
SF bug #477221: abs and divmod act oddly with -0.0
Try to ensure that divmod(-0.0, 1.0) -> (-0.0, +0.0) across platforms.
It always did on Windows, and still does. It didn't on Linux. Alas,
there's no platform-independent way to write a test case for this.
Bugfix candidate.
Diffstat (limited to 'Objects/floatobject.c')
-rw-r--r-- | Objects/floatobject.c | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 1efef4b..7e12a09 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -464,17 +464,34 @@ float_divmod(PyObject *v, PyObject *w) it will always be very close to one. */ div = (vx - mod) / 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; - div -= 1.0; + if (mod) { + /* ensure the remainder has the same sign as the denominator */ + if ((wx < 0) != (mod < 0)) { + mod += wx; + div -= 1.0; + } + } + 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 = 0.0; + if (wx < 0.0) + mod = -mod; } /* snap quotient to nearest integral value */ - floordiv = floor(div); - if (div - floordiv > 0.5) - floordiv += 1.0; - PyFPE_END_PROTECT(div) + if (div) { + floordiv = floor(div); + if (div - floordiv > 0.5) + floordiv += 1.0; + } + else { + /* div is zero - get the same sign as the true quotient */ + div *= div; /* hide "div = +0" from optimizers */ + floordiv = div * vx / wx; /* zero w/ sign of vx/wx */ + } + PyFPE_END_PROTECT(floordiv) return Py_BuildValue("(dd)", floordiv, mod); } |