10000 [3.12] gh-123836: workaround fmod(x, y) bug on Windows (GH-124171) (#… · python/cpython@c7a94e7 · GitHub
[go: up one dir, main page]

Skip to content

Commit c7a94e7

Browse files
authored
[3.12] gh-123836: workaround fmod(x, y) bug on Windows (GH-124171) (#124186)
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 6840b61 commit c7a94e7

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
@@ -2343,6 +2343,15 @@ math_fmod_impl(PyObject *module, double x, double y)
23432343
return PyFloat_FromDouble(x);
23442344
errno = 0;
23452345
r = fmod(x, y);
2346+
#ifdef _MSC_VER
2347+
/* Windows (e.g. Windows 10 with MSC v.1916) loose sign
2348+
for zero result. But C99+ says: "if y is nonzero, the result
2349+
has the same sign as x".
2350+
*/
2351+
if (r == 0.0 && y != 0.0) {
2352+
r = copysign(r, x);
2353+
}
2354+
#endif
23462355
if (Py_IS_NAN(r)) {
23472356
if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
23482357
errno = EDOM;

0 commit comments

Comments
 (0)
0