Skip to content

gh-157859: Fix cmath.atanh() returning inf at ±1 ± sqrt(DBL_MIN)*1j - #157860

Open
greg-const wants to merge 1 commit into
python:mainfrom
greg-const:fix-cmath-atanh-boundary
Open

greg-const wants to merge 1 commit into
python:mainfrom
greg-const:fix-cmath-atanh-boundary

Conversation

@greg-const

@greg-const greg-const commented Sep 20, 2026

Copy link
Copy Markdown

Fixes #157859.

Use the near-pole formula in cmath_atanh_impl() when
ay == CM_SQRT_DBL_MIN, as well as when it is smaller.

At the boundary, the general formula forms 4.0 / DBL_MIN, which
overflows even though the result is finite. Changing < to <= avoids
that intermediate overflow. This also fixes the corresponding
cmath.atan() inputs, since cmath.atan() uses the same implementation.

Add a regression test for both functions, covering all four sign
combinations and the immediately adjacent floating-point inputs. The test
fails without the C change and passes with it.

Testing

$ ./python -m test test_cmath -m testAtanhNearPoleBoundary
Result: SUCCESS   (FAILURE with the C change reverted)
$ ./python -m test test_cmath test_math test_complex test_float test_statistics test_builtin -j4
Total tests: run=772 skipped=17
Result: SUCCESS
$ ./python -m test -j4
Total tests: run=46,950 failures=1 skipped=2,584

The full test run reported failures in test_socket
(testInterruptedTimeout) and test.test_asyncio.test_ssl. Both passed
when rerun. testInterruptedTimeout also fails on an unpatched build in
the same environment, so these failures appear unrelated to this change.

$ make patchcheck

patchcheck passes apart from the expected NEWS entry added with blurb.

@python-cla-bot

python-cla-bot Bot commented Sep 20, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app

bedevere-app Bot commented Sep 20, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Comment thread Lib/test/test_cmath.py Outdated
…)*1j

cmath_atanh_impl() selects its near-pole formula with
`ay < CM_SQRT_DBL_MIN`.  At equality the general formula computes
4/(0 + ay*ay) = 4/DBL_MIN, which overflows, so the finite input
1 + 2**-511j (and its three sign/conjugate mirrors, and the
corresponding cmath.atan() inputs) returned inf.  Use `<=` so the
boundary word takes the near-pole formula, which is accurate there.
@greg-const
greg-const force-pushed the fix-cmath-atanh-boundary branch from eb544f9 to e385873 Compare September 20, 2026 13:20
@skirpichev
skirpichev self-requested a review September 20, 2026 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cmath.atanh() returns infinity for complex(1.0, 2.0**-511)

2 participants