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 some corner cases of isapprox with unsigned integers #55828

Merged
merged 2 commits into from
Sep 26, 2024
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
4 changes: 3 additions & 1 deletion base/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,9 @@ function isapprox(x::Integer, y::Integer;
if norm === abs && atol < 1 && rtol == 0
return x == y
else
return norm(x - y) <= max(atol, rtol*max(norm(x), norm(y)))
# 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)))
end
end

Expand Down
29 changes: 29 additions & 0 deletions test/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,35 @@ end
end
end

@testset "isapprox and unsigned integers" begin
for T in Base.BitUnsigned_types
# Test also combinations of different integer types
W = widen(T)
# The order of the operands for difference between unsigned integers is
# very important, test both combinations.
@test isapprox(T(42), T(42); rtol=T(0), atol=0.5)
@test isapprox(T(42), W(42); rtol=T(0), atol=0.5)
@test !isapprox(T(0), T(1); rtol=T(0), atol=0.5)
@test !isapprox(T(1), T(0); rtol=T(0), atol=0.5)
@test isapprox(T(1), T(3); atol=T(2))
@test isapprox(T(4), T(2); atol=T(2))
@test isapprox(T(1), W(3); atol=T(2))
@test isapprox(T(4), W(2); atol=T(2))
@test isapprox(T(5), T(7); atol=typemax(T))
@test isapprox(T(8), T(6); atol=typemax(T))
@test isapprox(T(1), T(2); rtol=1)
@test isapprox(T(6), T(3); rtol=1)
@test isapprox(T(1), W(2); rtol=1)
@test isapprox(T(6), W(3); rtol=1)
@test !isapprox(typemin(T), typemax(T))
@test !isapprox(typemax(T), typemin(T))
@test !isapprox(typemin(T), typemax(T); atol=typemax(T)-T(1))
@test !isapprox(typemax(T), typemin(T); atol=typemax(T)-T(1))
@test isapprox(typemin(T), typemax(T); atol=typemax(T))
@test isapprox(typemax(T), typemin(T); atol=typemax(T))
end
end

@testset "Conversion from floating point to unsigned integer near extremes (#51063)" begin
@test_throws InexactError UInt32(4.2949673f9)
@test_throws InexactError UInt64(1.8446744f19)
Expand Down