← Back to Pull requests

Support all `Real` types in the domain-checked functions #112

Closed
unknown closed this pull request without merging on Oct 20, 2026 17:07 UTC
View on GitHub
unknown unknown commented on Oct 19, 2026 22:55 UTC
Author

Fixes #95.

The bug

Every function in the sin, cos, tan, asin, acos, acosh, atanh, log, log2, log10, log1p group threw a MethodError for BigFloat:

julia> NaNMath.sin(big(1.0))
ERROR: MethodError: no method matching sin(::BigFloat)

The ::Real entry point was a trampoline that converted its argument with float and re-dispatched. Because float is the identity on AbstractFloat, a guard threw MethodError to break the recursion, and concrete methods existed only for Union{Float16, Float32, Float64} — so BigFloat always reached the guard.

sqrt had a worse variant of the same problem. sqrt(x::Real) = sqrt(float(x)) has no guard, so a Real that is its own float fixed point and is not an AbstractFloat recursed until it hit a StackOverflowError.

The change

One ::Real method per function that converts with float exactly once and then calls Base directly, instead of re-dispatching on the converted value. That covers BigFloat and also Real types that are not AbstractFloat and that float leaves unchanged, such as ForwardDiff.Dual.

sqrt gets the same treatment through an internal _sqrt helper, which keeps the sqrt_llvm intrinsic for the narrow float union. lgamma keeps the trampoline and its guard, since it has no Base counterpart and NaNMath.lgamma(big"2.0") should stay a MethodError.

Why ::Real and not ::Number

A ::Number signature would also fix dimensionless Unitful quantities, which currently throw a DomainError. It was rejected because the opt-out list is open-ended — it is not just Complex:

| type | <:Real | <:Number | x < 0 |
|---|---|---|---|
| Quaternions.Quaternion | ✗ | ✓ | MethodError |
| TaylorSeries.Taylor1 | ✗ | ✓ | compares constant term only |
| Unitful.Quantity | ✗ | ✓ | ✓ |

NaNMath.log on a quaternion works today and would become a MethodError; on a Taylor1 the domain check would be meaningless and oftype(x, NaN) silently truncates a 3rd-order series to 1st order. Real carries a total-order contract, Number carries none, so the domain checks belong on Real and everything else keeps forwarding to Base unchanged. Unitful is better served the way ForwardDiff, Symbolics and SymbolicUtils already handle this, by defining their own NaNMath methods.

Conditions are whitelists

Each condition is written so that only arguments inside the domain reach Base. Besides being the safer default for third-party Real types, it is slightly cheaper: sqrt folds to a bare fsqrt instruction because the hardware already returns NaN for negative input, and sin, cos and tan lose two instructions each. All other functions are unchanged instruction-for-instruction.

Behaviour changes beyond the fix

pow no longer returns a finite value for a negative base with a non-integer exponent whose conversion to a float rounds to an integer. NaNMath.pow(-1.5, (2^60 + 1)//2) returned Inf and now returns NaN, because the check looks at the original exponent rather than the promoted float.
A NaN argument's sign bit is no longer propagated by sin, cos, tan, asin, atanh and acosh. IEEE 754 assigns no meaning to it.

Tests

BigFloat (#95), 147 assertions. Runs at two non-default precisions (64 and 1024 bits) and asserts precision(y) == p and bit-exact equality with Base, so neither a Float64 detour nor a hard-coded precision can pass. Also pins that the result takes the global precision at call time, matching Base.
` Real fixed points of float , 49 assertions, against a test type that documents the full interface the domain checks rely on: float, promote_rule, <, abs, isfinite.

  • Regression tests for the pow exponent-rounding case and for Float16 sqrt.
  • Verified unchanged against ForwardDiff, Symbolics, SymbolicUtils, TaylorSeries, Quaternions, Unitful and Complex, and exhaustively over all 65536 Float16 bit patterns for sqrt`.

    🤖 Generated with Claude Code

    Diff: 511 lines (+383 / -128) across 6 files Reviewer: oscardssmith Review duration: 1091m
    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 · 511 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.373
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #112"
    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: 47.6% (MEDIUM) · Higher = larger blast radius
    47.6%
    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: 18.8% · Safe to merge
    18.8%