Skip to content

Commit b6bae53

Browse files
skirpichevvstinner
andauthored
gh-85989: Add skip_if_double_rounding to test.support (#150219)
Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 39bd44f commit b6bae53

4 files changed

Lines changed: 21 additions & 31 deletions

File tree

Lib/test/support/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"run_no_yield_async_fn", "run_yielding_async_fn", "async_yield",
7474
"reset_code", "on_github_actions",
7575
"requires_root_user", "requires_non_root_user",
76+
"skip_if_double_rounding",
7677
]
7778

7879

@@ -514,6 +515,15 @@ def dec(*args, **kwargs):
514515
float.__getformat__("double").startswith("IEEE"),
515516
"test requires IEEE 754 doubles")
516517

518+
# detect evidence of double-rounding:
519+
x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer
520+
HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
521+
skip_if_double_rounding = unittest.skipIf(HAVE_DOUBLE_ROUNDING,
522+
"accuracy not guaranteed on "
523+
"machines with double rounding")
524+
del x, y, HAVE_DOUBLE_ROUNDING
525+
526+
517527
def requires_zlib(reason='requires zlib'):
518528
try:
519529
import zlib

Lib/test/test_builtin.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,14 @@
3939
from test.support.script_helper import assert_python_ok
4040
from test.support.testcase import ComplexesAreIdenticalMixin
4141
from test.support.warnings_helper import check_warnings
42-
from test.support import requires_IEEE_754
42+
from test.support import requires_IEEE_754, skip_if_double_rounding
4343
from unittest.mock import MagicMock, patch
4444
try:
4545
import pty, signal
4646
except ImportError:
4747
pty = signal = None
4848

4949

50-
# Detect evidence of double-rounding: sum() does not always
51-
# get improved accuracy on machines that suffer from double rounding.
52-
x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer
53-
HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
54-
5550
# used as proof of globals being used
5651
A_GLOBAL_VALUE = 123
5752
A_SENTINEL = sentinel("A_SENTINEL")
@@ -2235,8 +2230,7 @@ def __getitem__(self, index):
22352230
complex(2, -0.0))
22362231

22372232
@requires_IEEE_754
2238-
@unittest.skipIf(HAVE_DOUBLE_ROUNDING,
2239-
"sum accuracy not guaranteed on machines with double rounding")
2233+
@skip_if_double_rounding
22402234
@support.cpython_only # Other implementations may choose a different algorithm
22412235
def test_sum_accuracy(self):
22422236
self.assertEqual(sum([0.1] * 10), 1.0)

Lib/test/test_math.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Python test set -- math module
22
# XXXX Should not do tests around zero only
33

4-
from test.support import verbose, requires_IEEE_754
4+
from test.support import (verbose, requires_IEEE_754,
5+
skip_if_double_rounding)
56
from test import support
67
import unittest
78
import fractions
@@ -23,11 +24,6 @@
2324
FLOAT_MAX = sys.float_info.max
2425
FLOAT_MIN = sys.float_info.min
2526

26-
# detect evidence of double-rounding: fsum is not always correctly
27-
# rounded on machines that suffer from double rounding.
28-
x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer
29-
HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
30-
3127
# locate file with test values
3228
if __name__ == '__main__':
3329
file = sys.argv[0]
@@ -683,8 +679,7 @@ def testfrexp(name, result, expected):
683679
self.assertTrue(math.isnan(math.frexp(NAN)[0]))
684680

685681
@requires_IEEE_754
686-
@unittest.skipIf(HAVE_DOUBLE_ROUNDING,
687-
"fsum is not exact on machines with double rounding")
682+
@skip_if_double_rounding
688683
def testFsum(self):
689684
# math.fsum relies on exact rounding for correct operation.
690685
# There's a known problem with IA32 floating-point that causes
@@ -920,8 +915,7 @@ def testHypot(self):
920915
self.assertRaises(TypeError, math.hypot, *([1.0]*18), 'spam')
921916

922917
@requires_IEEE_754
923-
@unittest.skipIf(HAVE_DOUBLE_ROUNDING,
924-
"hypot() loses accuracy on machines with double rounding")
918+
@skip_if_double_rounding
925919
@support.skip_on_newlib
926920
def testHypotAccuracy(self):
927921
# Verify improved accuracy in cases that were known to be inaccurate.
@@ -1412,8 +1406,7 @@ def __rmul__(self, other):
14121406
self.assertEqual(sumprod(*args), 0.0)
14131407

14141408
@requires_IEEE_754
1415-
@unittest.skipIf(HAVE_DOUBLE_ROUNDING,
1416-
"sumprod() accuracy not guaranteed on machines with double rounding")
1409+
@skip_if_double_rounding
14171410
@support.cpython_only # Other implementations may choose a different algorithm
14181411
def test_sumprod_accuracy(self):
14191412
sumprod = math.sumprod
@@ -1498,8 +1491,7 @@ def run(func, *args):
14981491
)
14991492

15001493
@requires_IEEE_754
1501-
@unittest.skipIf(HAVE_DOUBLE_ROUNDING,
1502-
"sumprod() accuracy not guaranteed on machines with double rounding")
1494+
@skip_if_double_rounding
15031495
@support.cpython_only # Other implementations may choose a different algorithm
15041496
@support.requires_resource('cpu')
15051497
def test_sumprod_extended_precision_accuracy(self):

Lib/test/test_statistics.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
import sys
1717
import unittest
1818
from test import support
19-
from test.support import import_helper, requires_IEEE_754, skip_on_newlib
19+
from test.support import (import_helper, requires_IEEE_754,
20+
skip_if_double_rounding, skip_on_newlib)
2021

2122
from decimal import Decimal
2223
from fractions import Fraction
@@ -28,12 +29,6 @@
2829

2930
# === Helper functions and class ===
3031

31-
# Test copied from Lib/test/test_math.py
32-
# detect evidence of double-rounding: fsum is not always correctly
33-
# rounded on machines that suffer from double rounding.
34-
x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer
35-
HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
36-
3732
def sign(x):
3833
"""Return -1.0 for negatives, including -0.0, otherwise +1.0."""
3934
return math.copysign(1, x)
@@ -2796,8 +2791,7 @@ def test_sqrtprod_helper_function_fundamentals(self):
27962791
self.assertEqual(sign(actual), sign(expected))
27972792

27982793
@requires_IEEE_754
2799-
@unittest.skipIf(HAVE_DOUBLE_ROUNDING,
2800-
"accuracy not guaranteed on machines with double rounding")
2794+
@skip_if_double_rounding
28012795
@support.cpython_only # Allow for a weaker sumprod() implementation
28022796
@skip_on_newlib
28032797
def test_sqrtprod_helper_function_improved_accuracy(self):

0 commit comments

Comments
 (0)