From ae8d94a024b1648f8d896753b12a09e91ef82b98 Mon Sep 17 00:00:00 2001 From: Tim Felgentreff Date: Wed, 18 Mar 2026 12:49:24 +0100 Subject: [PATCH] [GR-71591] Preserve signed zero in math.modf --- .../com.oracle.graal.python.test/src/tests/test_math.py | 5 +++++ .../graal/python/builtins/modules/MathModuleBuiltins.java | 3 +++ 2 files changed, 8 insertions(+) diff --git a/graalpython/com.oracle.graal.python.test/src/tests/test_math.py b/graalpython/com.oracle.graal.python.test/src/tests/test_math.py index 52af5bf203..b4cd1564af 100644 --- a/graalpython/com.oracle.graal.python.test/src/tests/test_math.py +++ b/graalpython/com.oracle.graal.python.test/src/tests/test_math.py @@ -1330,6 +1330,11 @@ def testmodf(name, result, expected): self.assertTrue(math.isnan(modf_nan[0])) self.assertTrue(math.isnan(modf_nan[1])) + small_negative_fraction, small_negative_integral = math.modf(-4.0755017301539587e-25) + self.assertEqual(small_negative_fraction, -4.0755017301539587e-25) + self.assertEqual(math.copysign(1.0, small_negative_integral), -1.0) + self.assertEqual(small_negative_integral, -0.0) + # test of specializations testmodf('modf(MyFloat())', math.modf(MyFloat()), (0.6, 0.0)) self.assertRaises(TypeError, math.modf, 'ahoj') diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MathModuleBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MathModuleBuiltins.java index fa75890da9..b56a70e834 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MathModuleBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MathModuleBuiltins.java @@ -842,6 +842,9 @@ static PTuple modfD(double value, } double fraction = value % 1; double integral = value - fraction; + if (integral == 0.0) { + integral = Math.copySign(0.0, value); + } return PFactory.createTuple(language, new Object[]{fraction, integral}); }