Skip to content

Commit

Permalink
[3.12] pythongh-123836: workaround fmod(x, y) bug on Windows (pythonG…
Browse files Browse the repository at this point in the history
…H-124171)

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)

Co-authored-by: Sergey B Kirpichev <[email protected]>
  • Loading branch information
skirpichev committed Sep 17, 2024
1 parent 6840b61 commit 4e13c82
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add workaround for broken :c:func:`!fmod()` implementations on Windows, that
loose zero sign (e.g. ``fmod(-10, 1)`` returns ``0.0``). Patch by Sergey B
Kirpichev.
9 changes: 9 additions & 0 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2343,6 +2343,15 @@ math_fmod_impl(PyObject *module, double x, double y)
return PyFloat_FromDouble(x);
errno = 0;
r = fmod(x, y);
#ifdef _MSC_VER
/* Windows (e.g. Windows 10 with MSC v.1916) loose sign
for zero result. But C99+ says: "if y is nonzero, the result
has the same sign as x".
*/
if (r == 0.0 && y != 0.0) {
r = copysign(r, x);
}
#endif
if (Py_IS_NAN(r)) {
if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
errno = EDOM;
Expand Down

0 comments on commit 4e13c82

Please sign in to comment.