Skip to content

Commit 99185bd

Browse files
authored
[3.13] pythongh-123836: workaround fmod(x, y) bug on Windows (pythonGH-124171) (python#124187)
Buildbot failure on Windows 10 with MSC v.1916 64 bit (AMD64): FAIL: testFmod (test.test_math.MathTests.testFmod) ---------------------------------------------------------------------- Traceback (most recent call last): File "D:\buildarea\3.x.bolen-windows10\build\Lib\test\test_math.py", line 605, in testFmod self.ftest('fmod(-10, 1)', math.fmod(-10, 1), -0.0) ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\buildarea\3.x.bolen-windows10\build\Lib\test\test_math.py", line 258, in ftest self.fail("{}: {}".format(name, failure)) ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AssertionError: fmod(-10, 1): expected -0.0, got 0.0 (zero has wrong sign) Here Windows loose sign of the result; if y is nonzero, the result should have the same sign as x. This amends commit 28aea5d. (cherry picked from commit f4dd440)
1 parent 99dc089 commit 99185bd

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add workaround for broken :c:func:`!fmod()` implementations on Windows, that
2+
loose zero sign (e.g. ``fmod(-10, 1)`` returns ``0.0``). Patch by Sergey B
3+
Kirpichev.

Modules/mathmodule.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,6 +2385,15 @@ math_fmod_impl(PyObject *module, double x, double y)
23852385
return PyFloat_FromDouble(x);
23862386
errno = 0;
23872387
r = fmod(x, y);
2388+
#ifdef _MSC_VER
2389+
/* Windows (e.g. Windows 10 with MSC v.1916) loose sign
2390+
for zero result. But C99+ says: "if y is nonzero, the result
2391+
has the same sign as x".
2392+
*/
2393+
if (r == 0.0 && y != 0.0) {
2394+
r = copysign(r, x);
2395+
}
2396+
#endif
23882397
if (Py_IS_NAN(r)) {
23892398
if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
23902399
errno = EDOM;

0 commit comments

Comments
 (0)