Skip to content

Commit ed962f1

Browse files
committed
tests/float: Test domain errors for more combos of args to math funcs.
Instead of having a special set of arguments to test for each math-module function, just test all functions with all sets of arguments. This gives improved test cases to prevent regressions. Signed-off-by: Damien George <[email protected]>
1 parent 47dc7d0 commit ed962f1

File tree

3 files changed

+94
-44
lines changed

3 files changed

+94
-44
lines changed

tests/float/math_domain.py

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@
1010
nan = float("nan")
1111

1212
# single argument functions
13-
for name, f, args in (
14-
("fabs", math.fabs, ()),
15-
("ceil", math.ceil, ()),
16-
("floor", math.floor, ()),
17-
("trunc", math.trunc, ()),
18-
("sqrt", math.sqrt, (-1, 0)),
19-
("exp", math.exp, ()),
20-
("log", math.log, ()),
21-
("sin", math.sin, ()),
22-
("cos", math.cos, ()),
23-
("tan", math.tan, ()),
24-
("asin", math.asin, (-1.1, 1, 1.1)),
25-
("acos", math.acos, (-1.1, 1, 1.1)),
26-
("atan", math.atan, ()),
27-
("ldexp", lambda x: math.ldexp(x, 0), ()),
28-
("radians", math.radians, ()),
29-
("degrees", math.degrees, ()),
13+
for name, f in (
14+
("fabs", math.fabs),
15+
("ceil", math.ceil),
16+
("floor", math.floor),
17+
("trunc", math.trunc),
18+
("sqrt", math.sqrt),
19+
("exp", math.exp),
20+
("log", math.log),
21+
("sin", math.sin),
22+
("cos", math.cos),
23+
("tan", math.tan),
24+
("asin", math.asin),
25+
("acos", math.acos),
26+
("atan", math.atan),
27+
("ldexp", lambda x: math.ldexp(x, 0)),
28+
("radians", math.radians),
29+
("degrees", math.degrees),
3030
):
31-
for x in args + (inf, -inf, nan):
31+
for x in (0, 1, 1.1, -1, -1.1, inf, -inf, nan):
3232
try:
3333
ans = "%.4f" % f(x)
3434
except ValueError:
@@ -38,35 +38,54 @@
3838
print("%s(%.4f) = %s" % (name, x, ans))
3939

4040
# double argument functions
41-
for name, f, args in (
42-
(
43-
"pow",
44-
math.pow,
45-
(
46-
(0, 2),
47-
(-1, 2),
48-
(0, -1),
49-
(-1, 2.3),
50-
(0.5, inf),
51-
(-0.5, inf),
52-
(0.5, -inf),
53-
(-0.5, -inf),
54-
(1.5, inf),
55-
(-1.5, inf),
56-
(1.5, -inf),
57-
(-1.5, -inf),
58-
(nan, 0),
59-
(1, nan),
60-
),
61-
),
62-
("log", math.log, ()),
63-
("fmod", math.fmod, ((1.2, inf), (1.2, -inf), (1.2, 0), (inf, 1.2))),
64-
("atan2", math.atan2, ((0, 0), (-inf, inf), (-inf, -inf), (inf, -inf))),
65-
("copysign", math.copysign, ()),
41+
for name, f in (
42+
("pow", math.pow),
43+
("log", math.log),
44+
("fmod", math.fmod),
45+
("atan2", math.atan2),
46+
("copysign", math.copysign),
6647
):
67-
for x in args + ((0, inf), (inf, 0), (inf, inf), (inf, nan), (nan, inf), (nan, nan)):
48+
for x in (
49+
(0, 0),
50+
(0, 2),
51+
(0, -1),
52+
(1, 0),
53+
(1.2, 0),
54+
(-1, 0),
55+
(-1, 2),
56+
(-1, 2.3),
57+
(0, inf),
58+
(0.5, inf),
59+
(0.5, -inf),
60+
(0.9, inf),
61+
(0.9, -inf),
62+
(1.2, inf),
63+
(1.2, -inf),
64+
(-0.5, inf),
65+
(-0.5, -inf),
66+
(-0.9, inf),
67+
(-0.9, -inf),
68+
(-1.2, inf),
69+
(-1.2, -inf),
70+
(inf, 0),
71+
(inf, 1.2),
72+
(inf, -1.2),
73+
(inf, inf),
74+
(inf, -inf),
75+
(-inf, inf),
76+
(-inf, -inf),
77+
(0, nan),
78+
(nan, 0),
79+
(1, nan),
80+
(nan, 1),
81+
(inf, nan),
82+
(nan, inf),
83+
(nan, nan),
84+
):
6885
try:
6986
ans = "%.4f" % f(*x)
7087
except ValueError:
7188
ans = "ValueError"
89+
except ZeroDivisionError:
90+
ans = "ZeroDivisionError"
7291
print("%s(%.4f, %.4f) = %s" % (name, x[0], x[1], ans))

tests/float/math_domain_python311.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Tests domain errors in math functions.
2+
# This is split out from math_domain.py because math.pow(0, -inf) was changed
3+
# in Python 3.11, and so this test requires a .py.exp file.
4+
# (See https://github.com/python/cpython/issues/88505)
5+
6+
try:
7+
import math
8+
except ImportError:
9+
print("SKIP")
10+
raise SystemExit
11+
12+
inf = float("inf")
13+
14+
for name, f in (
15+
("pow", math.pow),
16+
("log", math.log),
17+
("fmod", math.fmod),
18+
("atan2", math.atan2),
19+
("copysign", math.copysign),
20+
):
21+
for x in ((0, -inf),):
22+
try:
23+
ans = "%.4f" % f(*x)
24+
except ValueError:
25+
ans = "ValueError"
26+
print("%s(%.4f, %.4f) = %s" % (name, x[0], x[1], ans))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pow(0.0000, -inf) = inf
2+
log(0.0000, -inf) = ValueError
3+
fmod(0.0000, -inf) = 0.0000
4+
atan2(0.0000, -inf) = 3.1416
5+
copysign(0.0000, -inf) = -0.0000

0 commit comments

Comments
 (0)