Fix math.atanh near -1 #1401
All Vouch review confidence checks passed
Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 559 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.379
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1401"
general
weight: 0.35
davidhewitt
Summary
math.atanhloses 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 |Cause.
math_atanh(crates/monty/src/modules/math.rsline 988) returnsf.atanh(). Unlike mostf64math methods,f64::atanhis not a C library call. Rust std computes it as(
library/std/src/num/f64.rs, Rust 1.99.1). Near -1,1.0 - selfrounds to about 2, so the argument ofln_1pis close to -1, and the subtraction insideln_1pcancels most of the significant bits.Fix. Call
libm::atanh(f)from thelibmcrate. It is already a dependency, used here fornextafter,remainder,modf,frexp,ldexp,gammaanderf.libm::atanh(0.2.16,src/math/atanh.rs) works on |x| and restores the sign at the end. For |x| ≥ 0.5 it computes0.5 log1p(2 (|x| / (1 - |x|))). There1 - |x|is exact, so the ratio is large and positive, andlog1pdoes not cancel. No other function changes.Tests
Two assertions in
crates/monty/test_cases/math__module.py, next to the existingatanhcases:cargo test -p monty --no-fail-fastandcargo run -p monty-datatest, macOS arm64, rustc 1.99.1, CPython 3.14.9 for the CPython side:-
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.- Datatest: 1,574 passed, 0 failed, both the Monty and the CPython side of
math__module.pyincluded.-
cargo test: 1,202 passed, 3 failed.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.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 theatanhfix and does not depend on it.<!-- This is an auto-generated description by cubic. -->
---
Summary by cubic
Fixes
math.atanhreturning values with large relative error for arguments near -1. Rust'sf64::atanhuses a formula that cancels significant digits there; switching tolibm::atanh(already a dependency) matches CPython results at full precision.atanh(-0.9999999999999999)andatanh(-0.9999999999999994).libmcrate is already used for other math functions, so no new dependencies.<sup>Written for commit 1dfe8034787a020c32f3a7c780f7f5720d394361. Summary will update on new commits.</sup>
<a href="https://cubic.dev/pr/pydantic/monty/pull/1401?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->