isapprox: restore the `x == y` guard in the `Integer` method #62975
This comment reads weirdly (and non-human) in my opinion.
# note: check x == y to avoid NaN comparisons for e.g. rtol=Inf and x==y==0
See my suggested edit below
Approved these changes.
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!
Couldn't this just be
_x, _y = minmax(x, y)
?
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 |
Approved these changes.
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)
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)))
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.
Conflicts
Superseded by #63022.
KristofferC
PatrickHaecker
This is a can of worms. I tried to limit the following PR to the (hopefully) non-controversial part.
:robot:
The
Integermethod ofisapproxexists for one reason:x - yoverflows 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
Numbermethod itspecializes. It deviates anyway, because the rewrite dropped that method's
x == y ||guard:For two zeros with
rtol=Infthe bound isInf0, which isNaN, so the comparison isfalse. The same happens for
rtol=NaN, foratol=NaNwith any arguments at all(
isapprox(5, 5; atol=NaN)isfalse), 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
Numbermethod viainvokeover every combination of 11 integer types, threemagnitudes including
typemax, and seven values in bothatolandrtol.This does not settle what
isapproxshould mean for an indeterminate tolerance. Thedocstring's prose ("relative distance or absolute distance is within tolerance bounds") and
its formula (
norm(x-y) <= max(atol, rtolmax(norm(x), norm(y)))) disagree precisely there,since
d <= max(t1, t2)andd <= t1 || d <= t2are equivalent over the reals but not whenmaxpropagates aNaN. Neither method implements the prose. That question should bediscussed separately.
Performance
The fast path (
norm === abs && atol < 1 && rtol == 0, taken by every default call) isuntouched: the LLVM IR of a loop over
isapprox(v[i], w[i])is byte-identical before and after.The
elsebranch is reached only with explicit non-default keywords. There the added shortcircuit trades vectorisation for an early exit — 100k
Intpairs withatol=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, butalways evaluates the user's
normand the subtraction, which costs an allocation forBigInt.The short-circuiting form matches what the
Numbermethod does, which is the point of thechange.
Assisted-by: Claude Code (Opus 5)