Skip to content

Commit 894af95

Browse files
authored
gh-155966: Correct handling of math.tanpi poles (#155980)
``math.tanpi`` now raises a ``ValueError`` for half-integer values (e.g., ``tanpi(0.5)``), as these are poles of the function.
1 parent 5f14441 commit 894af95

4 files changed

Lines changed: 20 additions & 8 deletions

File tree

Lib/test/mathdata/math_testcases.txt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,9 +1311,9 @@ atan2pi20000 atan2pi inf 0 -> 0.5
13111311
atan2pi20001 atan2pi -inf 0 -> -0.5
13121312
atan2pi20002 atan2pi nan 0 -> nan
13131313

1314-
atan2pi20000 atan2pi inf -0 -> 0.5
1315-
atan2pi20001 atan2pi -inf -0 -> -0.5
1316-
atan2pi20002 atan2pi nan -0 -> nan
1314+
atan2pi21000 atan2pi inf -0 -> 0.5
1315+
atan2pi21001 atan2pi -inf -0 -> -0.5
1316+
atan2pi21002 atan2pi nan -0 -> nan
13171317

13181318
atan2pi20003 atan2pi inf 1 -> 0.5
13191319
atan2pi20004 atan2pi -inf 1 -> -0.5
@@ -1339,9 +1339,9 @@ atan2pi30000 atan2pi 0 inf -> 0.0
13391339
atan2pi30001 atan2pi 0 -inf -> 1.0
13401340
atan2pi30002 atan2pi 0 nan -> nan
13411341

1342-
atan2pi30000 atan2pi -0 inf -> -0.0
1343-
atan2pi30001 atan2pi -0 -inf -> -1.0
1344-
atan2pi30002 atan2pi -0 nan -> nan
1342+
atan2pi31000 atan2pi -0 inf -> -0.0
1343+
atan2pi31001 atan2pi -0 -inf -> -1.0
1344+
atan2pi31002 atan2pi -0 nan -> nan
13451345

13461346
atan2pi30003 atan2pi 1 inf -> 0.0
13471347
atan2pi30004 atan2pi 1 -inf -> 1.0
@@ -2247,3 +2247,8 @@ tanpi10275 tanpi -1.6591963470121216 -> 1.829921944286168
22472247
tanpi20001 tanpi inf -> nan invalid
22482248
tanpi20002 tanpi -inf -> nan invalid
22492249
tanpi20003 tanpi nan -> nan
2250+
2251+
tanpi30001 tanpi 0.5 -> inf divide-by-zero
2252+
tanpi30002 tanpi -0.5 -> -inf divide-by-zero
2253+
tanpi30003 tanpi 1.5 -> -inf divide-by-zero
2254+
tanpi30004 tanpi -1.5 -> inf divide-by-zero

Lib/test/test_math.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,7 +2144,12 @@ def test_mtestfile(self):
21442144
fail_fmt = "{}: {}{!r}: {}"
21452145

21462146
failures = []
2147+
ids = set()
21472148
for id, fn, args, expected, flags in parse_mtestfile(math_testcases):
2149+
if id in ids:
2150+
failures.append(f"Duplicate test id {id}")
2151+
ids.add(id)
2152+
21482153
func = getattr(math, fn)
21492154

21502155
if 'invalid' in flags or 'divide-by-zero' in flags:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`math.tanpi` now raises :exc:`ValueError` for half-integer values
2+
(e.g., ``tanpi(1.5)``), as these are poles of the function.

Modules/mathmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,10 +1343,10 @@ FUNC1D(tan, tan, 0,
13431343
FUNC1(tanh, tanh, 0,
13441344
"tanh($module, x, /)\n--\n\n"
13451345
"Return the hyperbolic tangent of x.")
1346-
FUNC1D(tanpi, m_tanpi, 1,
1346+
FUNC1D(tanpi, m_tanpi, 0,
13471347
"tanpi($module, x, /)\n--\n\n"
13481348
"Return the tangent of x (measured in half-turns).",
1349-
"expected a finite input, got %s")
1349+
"expected a finite input not equal to a half-integer, got %s")
13501350

13511351
/* Precision summation function as msum() by Raymond Hettinger in
13521352
<https://code.activestate.com/recipes/393090-binary-floating-point-summation-accurate-to-full-p/>,

0 commit comments

Comments
 (0)