← Back to Pull requests

Fix math.atanh near -1 #1401

Closed
unknown closed this pull request without merging on Nov 23, 2026 13:57 UTC
View on GitHub
unknown unknown commented on Nov 20, 2026 19:30 UTC
Author

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.py fails at the first new line: AssertionError at line 947.
    - run_test_cases_cpython::math__module.py passes.
    - 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.py included.
    - cargo test: 1,202 passed, 3 failed.
  • The 3 cargo test failures are the same both times: the heap_reader_compile_fail snapshot tests readonly_mutable_projection, smuggle_and_swap_reader and smuggle_heap_read, whose expected compiler diagnostics differ under this toolchain. They are unrelated to this change.

  • Not run: make lint-rs and make 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.
  • <!-- This is an auto-generated description by cubic. -->
    ---

    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) and atanh(-0.9999999999999994).

  • The libm crate 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. -->

    Diff: 559 lines (+419 / -140) across 6 files Reviewer: davidhewitt Review duration: 3987m
    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
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 48.2% (MEDIUM) · Higher = larger blast radius
    48.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19% · Safe to merge
    19%
    Repository