From 434a1f1ecf372b205e971ce80f50b150ff504ca2 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 9 Sep 2024 06:41:48 +0300 Subject: [PATCH 1/2] gh-123836: check zero signs in math_testcases.txt Just like cmath_testcases.txt. These tests require IEEE 754 anyway. --- Lib/test/test_math.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index 5c07d9e2c3a834..28a2964ab7bf35 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -187,6 +187,9 @@ def result_check(expected, got, ulp_tol=5, abs_tol=0.0): # Check exactly equal (applies also to strings representing exceptions) if got == expected: + if not got and not expected: + if math.copysign(1, got) != math.copysign(1, expected): + return f"expected {expected}, got {got} (zero has wrong sign)" return None failure = "not equal" @@ -2053,6 +2056,12 @@ def test_testfile(self): if id in SKIP_ON_TIGER: continue + # Skip some sqrt tests. C99+ says for math.h's sqrt: If the + # argument is +∞ or ±0, it is returned, unmodified. On another + # hand, for csqrt: If z is ±0+0i, the result is +0+0i. + if id in ['sqrt0002', 'sqrt0003', 'sqrt1001', 'sqrt1023']: + continue + func = getattr(math, fn) if 'invalid' in flags or 'divide-by-zero' in flags: From 3a96aee0b16a29514da1bf7dd8bc395d34912222 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Tue, 17 Sep 2024 05:32:07 +0300 Subject: [PATCH 2/2] adress review: correct zero sign for sqrt tests to match math.h convention --- Lib/test/test_math.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index 28a2964ab7bf35..9ac5ab63088266 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -2056,12 +2056,6 @@ def test_testfile(self): if id in SKIP_ON_TIGER: continue - # Skip some sqrt tests. C99+ says for math.h's sqrt: If the - # argument is +∞ or ±0, it is returned, unmodified. On another - # hand, for csqrt: If z is ±0+0i, the result is +0+0i. - if id in ['sqrt0002', 'sqrt0003', 'sqrt1001', 'sqrt1023']: - continue - func = getattr(math, fn) if 'invalid' in flags or 'divide-by-zero' in flags: @@ -2076,6 +2070,13 @@ def test_testfile(self): except OverflowError: result = 'OverflowError' + # C99+ says for math.h's sqrt: If the argument is +∞ or ±0, it is + # returned, unmodified. On another hand, for csqrt: If z is ±0+0i, + # the result is +0+0i. Lets correct zero sign of er to follow + # first convention. + if id in ['sqrt0002', 'sqrt0003', 'sqrt1001', 'sqrt1023']: + er = math.copysign(er, ar) + # Default tolerances ulp_tol, abs_tol = 5, 0.0