← Back to Pull requests

isapprox: restore the `x == y` guard in the `Integer` method #62975

Closed
PatrickHaecker closed this pull request without merging on Oct 03, 2026 02:07 UTC
View on GitHub
PatrickHaecker PatrickHaecker commented on Sep 02, 2026 09:40 UTC
Author

This is a can of worms. I tried to limit the following PR to the (hopefully) non-controversial part.

:robot:
The Integer method of isapprox exists for one reason: x - y overflows for integers,
which is what #50730 fixed. Equal arguments have no difference to compute and therefore
cannot overflow, so on them the method has no reason to deviate from the Number method it
specializes. It deviates anyway, because the rewrite dropped that method's x == y || guard:

julia> isapprox(0, 0; rtol=Inf)
false

julia> isapprox(0, 0.0; rtol=Inf)
true

For two zeros with rtol=Inf the bound is Inf0, which is NaN, so the comparison is
false. The same happens for rtol=NaN, for atol=NaN with any arguments at all
(isapprox(5, 5; atol=NaN) is false), and for negative tolerances.

This is a regression:

| version | isapprox(0,0;rtol=Inf) | isapprox(5,5;atol=NaN) | isapprox(0.0,0.0;rtol=Inf) |
| --- | --- | --- | --- |
| 1.6.7, 1.7.3, 1.8.5 | true | true | true |
| 1.10.12, 1.11.9, 1.12.7, 1.13.0-rc3, master | false | false | true |

Scope

The change is deliberately confined to restoring the dropped clause. Its entire effect is on
equal arguments; no result changes for unequal ones. The test asserts that directly, comparing
against the Number method via invoke over every combination of 11 integer types, three
magnitudes including typemax, and seven values in both atol and rtol.

This does not settle what isapprox should mean for an indeterminate tolerance. The
docstring's prose ("relative distance
or absolute distance is within tolerance bounds") and
its formula (norm(x-y) <= max(atol, rtol
max(norm(x), norm(y)))) disagree precisely there,
since d <= max(t1, t2) and d <= t1 || d <= t2 are equivalent over the reals but not when
max propagates a NaN. Neither method implements the prose. That question should be
discussed separately.

Performance

The fast path (norm === abs && atol < 1 && rtol == 0, taken by every default call) is
untouched: the LLVM IR of a loop over isapprox(v[i], w[i]) is byte-identical before and after.

The else branch is reached only with explicit non-default keywords. There the added short
circuit trades vectorisation for an early exit — 100k Int pairs with atol=0.0, rtol=1e-8,
interleaved best-of-500, ns per element:

| data | before | after |
| --- | --- | --- |
| all pairs equal | 5.00 | 0.77 (0.15x) |
| no pairs equal | 3.60 | 5.17 (1.44x) |
| half equal | 3.75 | 2.96 (0.79x) |

A branch-free (x == y) | (...) keeps vectorization and is neutral across all three, but
always evaluates the user's norm and the subtraction, which costs an allocation for BigInt.
The short-circuiting form matches what the Number method does, which is the point of the
change.

Assisted-by: Claude Code (Opus 5)

Diff: 20 lines (+18 / -2) across 2 files Reviewer: KristofferC Review duration: 44186m
All Vouch review confidence checks passed
Residual risk 0.04 = Change Risk (0.23) × (1 − Review Confidence (0.83)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.23 · 20 lines diff across 2 files
0.23
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.71 · 5 substantive comment(s)
0.71
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
82%
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.182
Real PR Conversation Timeline
14 items
KristofferC KristofferC commented on code on Sep 02, 2026 19:53 UTC
Code Review
base/floatfuncs.jl:1
@@ -236,7 +236,9 @@ function isapprox(x::Integer, y::Integer;
     else
         # We need to take the difference `max` - `min` when comparing unsigned integers.
         _x, _y = x < y ? (x, y) : (y, x)
-        return norm(_y - _x) <= max(atol, rtol*max(norm(_x), norm(_y)))
+        # Equal arguments cannot overflow, so keep the `Number` method's guard: without it a

This comment reads weirdly (and non-human) in my opinion.

stevengj stevengj commented on code on Sep 02, 2026 20:02 UTC
Code Review
base/floatfuncs.jl:1
@@ -236,7 +236,9 @@ function isapprox(x::Integer, y::Integer;
     else
         # We need to take the difference `max` - `min` when comparing unsigned integers.
         _x, _y = x < y ? (x, y) : (y, x)
-        return norm(_y - _x) <= max(atol, rtol*max(norm(_x), norm(_y)))
+        # Equal arguments cannot overflow, so keep the `Number` method's guard: without it a
+        # `NaN` tolerance (e.g. `rtol*0 == Inf*0`) would reject them.

        # note: check x == y to avoid NaN comparisons for e.g. rtol=Inf and x==y==0

stevengj stevengj commented on code on Sep 02, 2026 20:03 UTC
Code Review
base/floatfuncs.jl:1
@@ -236,7 +236,9 @@ function isapprox(x::Integer, y::Integer;
     else
         # We need to take the difference `max` - `min` when comparing unsigned integers.
         _x, _y = x < y ? (x, y) : (y, x)
-        return norm(_y - _x) <= max(atol, rtol*max(norm(_x), norm(_y)))
+        # Equal arguments cannot overflow, so keep the `Number` method's guard: without it a

See my suggested edit below

KristofferC KristofferC approved these changes on Sep 03, 2026 06:59 UTC
Approved Reviewer

Approved these changes.

PatrickHaecker PatrickHaecker commented on code on Sep 03, 2026 07:00 UTC
Code Review
base/floatfuncs.jl:1
@@ -236,7 +236,9 @@ function isapprox(x::Integer, y::Integer;
     else
         # We need to take the difference `max` - `min` when comparing unsigned integers.
         _x, _y = x < y ? (x, y) : (y, x)
-        return norm(_y - _x) <= max(atol, rtol*max(norm(_x), norm(_y)))
+        # Equal arguments cannot overflow, so keep the `Number` method's guard: without it a

Yes, thanks, it was non-human. I recognized that it did not read especially well, but it was at the level of my threshold of rewording and I didn't have an immediate idea how to improve it. But your suggestion, @stevengj, reads much better. Thanks for that!

stevengj stevengj commented on code on Sep 03, 2026 14:05 UTC
Code Review
base/floatfuncs.jl:1
@@ -236,7 +236,8 @@ function isapprox(x::Integer, y::Integer;
     else
         # We need to take the difference `max` - `min` when comparing unsigned integers.
         _x, _y = x < y ? (x, y) : (y, x)

Couldn't this just be

_x, _y = minmax(x, y)

?

PatrickHaecker PatrickHaecker commented on code on Sep 03, 2026 14:52 UTC
Code Review
base/floatfuncs.jl:1
@@ -236,7 +236,8 @@ function isapprox(x::Integer, y::Integer;
     else
         # We need to take the difference `max` - `min` when comparing unsigned integers.
         _x, _y = x < y ? (x, y) : (y, x)

return x == y || norm(_y - _x) <= max(atol, rtol*max(norm(_x), norm(_y)))

It should be identical for identical types, but differ for mixed types, because minmax does the promote, so _x and _y would be the promoted types.
This should be neutral for the subtraction inside the first norm, but would change the norm calculation inside of max. It could make the norm calculation a bit less efficient.
However, due to the promote, it gets rid of the type instability of the terniary which should be more efficient.

So I think this needs some measurements. Seems like a clear win (ratios < 1 means minmax needs less time). I pushed the change. Thanks, @stevengj!

:robot: from here on:

| x \ y | Int8 | UInt8 | Int16 | UInt16 | Int32 | UInt32 | Int64 | UInt64 | Int128 | UInt128 |
|---|---|---|---|---|---|---|---|---|---|---|
| Int8 | 1.01 | 0.30 | 0.75 | 0.17 | 0.83 | 0.17 | 0.89 | 0.29 | 0.78 | 0.40 |
| UInt8 | | 1.04 | 0.70 | 0.13 | 0.69 | 0.14 | 0.93 | 0.29 | 0.54 | 0.52 |
| Int16 | | | 1.01 | 0.28 | 0.77 | 0.17 | 0.93 | 0.34 | 0.78 | 0.44 |
| UInt16 | | | | 1.04 | 0.68 | 0.11 | 0.97 | 0.27 | 0.56 | 0.59 |
| Int32 | | | | | 1.09 | 0.26 | 0.93 | 0.34 | 0.94 | 0.36 |
| UInt32 | | | | | | 0.96 | 0.82 | 0.25 | 0.54 | 0.57 |
| Int64 | | | | | | | 1.02 | 0.33 | 0.76 | 0.46 |
| UInt64 | | | | | | | | 1.03 | 0.61 | 0.60 |
| Int128 | | | | | | | | | 0.98 | 0.46 |
| UInt128 | | | | | | | | | | 1.05 |

  • Diagonal 0.96–1.09: no difference, as expected — same-type minmax is the ternary.

  • Off-diagonal, same signedness: 0.54–0.97 — minmax faster.

  • Off-diagonal, mixed signedness: 0.11–0.61 — minmax up to ~9× faster; the ternary's Union{Tuple{S,T},Tuple{T,S}} union-split dominates.
  • stevengj stevengj approved these changes on Sep 03, 2026 19:32 UTC
    Approved Reviewer

    Approved these changes.

    stevengj stevengj commented on Sep 03, 2026 19:33 UTC
    Contributor

    Test failure:

    Error in testset floatfuncs:
    Error During Test at /cache/build/tester-amdci4-3/julialang/julia-pr/julia-ca891f468e/share/julia/test/floatfuncs.jl:254
      Test threw exception
      Expression: !(isapprox(typemin(T) + T(10), (unsigned(T))(10), atol = 1))
      InexactError: convert(UInt128, -170141183460469231731687303715884105718)
    

    stevengj stevengj commented on code on Sep 03, 2026 19:39 UTC
    Code Review
    base/floatfuncs.jl:1
    @@ -235,8 +235,9 @@ function isapprox(x::Integer, y::Integer;
             return x == y
         else
             # We need to take the difference `max` - `min` when comparing unsigned integers.
    -        _x, _y = x < y ? (x, y) : (y, x)
    -        return norm(_y - _x) <= max(atol, rtol*max(norm(_x), norm(_y)))
    +        _x, _y = minmax(x, y)
    +        # note: check x == y to avoid NaN comparisons for e.g. rtol=Inf and x==y==0
    +        return x == y || norm(_y - _x) <= max(atol, rtol*max(norm(_x), norm(_y)))

    The promotion to a common type of minmax seems to be a problem here for mixed signed/unsigned types (causing the test failure). Without re-introducing the type instability of the old _x, _y = x < y ? (x, y) : (y, x), I think you may need to do something like:

            # note: check x == y to avoid NaN comparisons for e.g. rtol=Inf and x==y==0
            return x == y || norm(x < y ? y - x : x - y) <= max(atol, rtol*max(norm(x), norm(y)))
    

    PatrickHaecker PatrickHaecker commented on code on Sep 04, 2026 06:53 UTC
    Code Review
    base/floatfuncs.jl:1
    @@ -235,8 +235,9 @@ function isapprox(x::Integer, y::Integer;
             return x == y
         else
             # We need to take the difference `max` - `min` when comparing unsigned integers.
    -        _x, _y = x < y ? (x, y) : (y, x)
    -        return norm(_y - _x) <= max(atol, rtol*max(norm(_x), norm(_y)))
    +        _x, _y = minmax(x, y)
    +        # note: check x == y to avoid NaN comparisons for e.g. rtol=Inf and x==y==0
    +        return x == y || norm(_y - _x) <= max(atol, rtol*max(norm(_x), norm(_y)))

    Thanks a lot, @stevengj, for the guidance in this PR!

    I think we should probably split this into the different signedness cases which would make it more readable while probably being faster. But this is stuff for another PR.

    So if testing succeeds, your suggestion seems to be the local optimum.

    adienes adienes commented on Sep 30, 2026 12:51 UTC
    Contributor

    Conflicts

    PatrickHaecker PatrickHaecker commented on Oct 03, 2026 02:07 UTC
    Author

    Superseded by #63022.

    Reviewers
    Assignees
    PatrickHaecker PatrickHaecker
    Labels
    risk: low confidence: 82.6% 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: 22.7% (LOW) · Higher = larger blast radius
    22.7%
    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: 82.6% · Lower = rushed or shallow review
    82.6%
    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: 3.9% · Safe to merge
    3.9%