Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix integer overflow in isapprox #50730

Merged
merged 4 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion base/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,20 @@ true
function isapprox(x::Number, y::Number;
atol::Real=0, rtol::Real=rtoldefault(x,y,atol),
nans::Bool=false, norm::Function=abs)
x == y || (isfinite(x) && isfinite(y) && norm(x-y) <= max(atol, rtol*max(norm(x), norm(y)))) || (nans && isnan(x) && isnan(y))
x′, y′ = promote(x, y) # to avoid integer overflow
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have the other method, why do we need to widen here?

Copy link
Member Author

@nalimilan nalimilan Jul 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the other method is called only when both values are integer, but here norm(x) can overflow if x is an integer and y is a float. See tests added by the same commit.

EDIT: Note that the goal is not really to widen, but to switch to a floating point type so that instead of overflowing for values like typemax(Int8), norm gives a reasonable value to compute the tolerance.

x == y ||
(isfinite(x) && isfinite(y) && norm(x-y) <= max(atol, rtol*max(norm(x′), norm(y′)))) ||
Comment on lines +307 to +309
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically this can give slightly different results than before even in the absence of overflow, since promotion may round values larger than maxintfloat, giving a slightly different tolerance. But this should be OK.

(nans && isnan(x) && isnan(y))
end

function isapprox(x::Integer, y::Integer;
atol::Real=0, rtol::Real=rtoldefault(x,y,atol),
nans::Bool=false, norm::Function=abs)
if norm === abs && atol < 1 && rtol == 0
return x == y
else
return norm(x - y) <= max(atol, rtol*max(norm(x), norm(y)))
end
end

"""
Expand Down
44 changes: 44 additions & 0 deletions test/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,47 @@ end
struct CustomNumber <: Number end
@test !isnan(CustomNumber())
end

@testset "isapprox and integer overflow" begin
for T in (Int8, Int16, Int32)
T === Int && continue
@test !isapprox(typemin(T), T(0))
@test !isapprox(typemin(T), unsigned(T)(0))
@test !isapprox(typemin(T), 0)
@test !isapprox(typemin(T), T(0), atol=0.99)
@test !isapprox(typemin(T), unsigned(T)(0), atol=0.99)
@test !isapprox(typemin(T), 0, atol=0.99)
@test_broken !isapprox(typemin(T), T(0), atol=1)
@test_broken !isapprox(typemin(T), unsigned(T)(0), atol=1)
@test !isapprox(typemin(T), 0, atol=1)

@test !isapprox(typemin(T)+T(10), T(10))
@test !isapprox(typemin(T)+T(10), unsigned(T)(10))
@test !isapprox(typemin(T)+T(10), 10)
@test !isapprox(typemin(T)+T(10), T(10), atol=0.99)
@test !isapprox(typemin(T)+T(10), unsigned(T)(10), atol=0.99)
@test !isapprox(typemin(T)+T(10), 10, atol=0.99)
@test_broken !isapprox(typemin(T)+T(10), T(10), atol=1)
@test !isapprox(typemin(T)+T(10), unsigned(T)(10), atol=1)
@test !isapprox(typemin(T)+T(10), 10, atol=1)

@test isapprox(typemin(T), 0.0, rtol=1)
end
for T in (Int, Int64, Int128)
@test !isapprox(typemin(T), T(0))
@test !isapprox(typemin(T), unsigned(T)(0))
@test !isapprox(typemin(T), T(0), atol=0.99)
@test !isapprox(typemin(T), unsigned(T)(0), atol=0.99)
@test_broken !isapprox(typemin(T), T(0), atol=1)
@test_broken !isapprox(typemin(T), unsigned(T)(0), atol=1)

@test !isapprox(typemin(T)+T(10), T(10))
@test !isapprox(typemin(T)+T(10), unsigned(T)(10))
@test !isapprox(typemin(T)+T(10), T(10), atol=0.99)
@test !isapprox(typemin(T)+T(10), unsigned(T)(10), atol=0.99)
@test_broken !isapprox(typemin(T)+T(10), T(10), atol=1)
@test !isapprox(typemin(T)+T(10), unsigned(T)(10), atol=1)

@test isapprox(typemin(T), 0.0, rtol=1)
end
end