Fix math.atanh near -1
Summary
math.atanh loses most of its digits for arguments near -1, on every platform, including WASM.
| x | Monty 1.6.0 (macOS, Linux and WASM) | CPython 3.14.9 and 3.15.2 (macOS), 3.12.3 (Linux) | relative error |
|---|---|---|---|
-0.9999999999999999 | -18.36840028483855 | -18.714973875118524 | 1.9e-2 |
-0.9999999999999997 | -18.36840028483855 | -18.165667730784467 | 1.1e-2 |
-0.9999999999999994 | -17.819094140504497 | -17.910254918901472 | 5.1e-3 |
- Correct values. The CPython values agree with mpmath at 1,200 bits, rounded to nearest.
- Spread. These are the worst of 1,000,000 seeded inputs. On the 500,738 negative inputs, Monty differs from CPython on 258,540, by more than 2 ulps on 40,359. On the 499,077 positive inputs it differs on 150, by 1 ulp.
Cause. math_atanh (crates/monty/src/modules/math.rs line 988) returns f.atanh(). Unlike most f64 math methods, f64::atanh is not a C library call. Rust std computes it as
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
(library/std/src/num/f64.rs, Rust 1.99.1). Near -1, 1.0 - self rounds to about 2, so the argument of ln_1p is close to -1, and the subtraction inside ln_1p cancels most of the significant bits.
Fix. Call libm::atanh(f) from the libm crate. It is already a dependency, used here for nextafter, remainder, modf, frexp, ldexp, gamma and erf. libm::atanh (0.2.16, src/math/atanh.rs) works on |x| and restores the sign at the end. For |x| ≥ 0.5 it computes 0.5 * log1p(2 * (|x| / (1 - |x|))). There 1 - |x| is exact, so the ratio is large and positive, and log1p does not cancel. No other function changes.
Tests
Two assertions in crates/monty/test_cases/math__module.py, next to the existing atanh cases:
assert math.isclose(math.atanh(-0.9999999999999999), -18.714973875118524, rel_tol=1e-15)
assert math.isclose(math.atanh(-0.9999999999999994), -17.910254918901472, rel_tol=1e-15)
cargo test -p monty --no-fail-fast and cargo run -p monty-datatest, macOS arm64, rustc 1.99.1, CPython 3.14.9 for the CPython side:
- Without the fix (main a7cd48c6 plus the two assertions):
run_test_cases_monty::math__module.pyfails at the first new line:AssertionErrorat line 947.run_test_cases_cpython::math__module.pypasses.- Datatest: 1,573 passed, 1 failed.
cargo test: 1,202 passed, 3 failed.
- With the fix:
- Datatest: 1,574 passed, 0 failed, both the Monty and the CPython side of
math__module.pyincluded. cargo test: 1,202 passed, 3 failed.
- Datatest: 1,574 passed, 0 failed, both the Monty and the CPython side of
- The 3
cargo testfailures are the same both times: theheap_reader_compile_failsnapshot testsreadonly_mutable_projection,smuggle_and_swap_readerandsmuggle_heap_read, whose expected compiler diagnostics differ under this toolchain. They are unrelated to this change. - Not run:
make lint-rsandmake format-rs(nightly rustfmt), the binding tests, Linux and Windows.
Prior discussion
None for this bug. #1390 proposes routing all float math through libm, which would include this change. This PR is only the atanh fix and does not depend on it.
- I have read the contribution guide.
- I understand and can explain the proposed changes, including any generated with AI assistance.
Summary by cubic
Fixes math.atanh returning values with large relative error for arguments near -1. Rust's f64::atanh uses a formula that cancels significant digits there; switching to libm::atanh (already a dependency) matches CPython results at full precision.
- Adds regression tests for
atanh(-0.9999999999999999)andatanh(-0.9999999999999994). - The
libmcrate is already used for other math functions, so no new dependencies.