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 edge cases where inexact conversions to UInt don't throw #51095

Merged
merged 5 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ for Ti in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UIn
end
end
function (::Type{$Ti})(x::$Tf)
if ($(Tf(typemin(Ti))) <= x <= $(Tf(typemax(Ti)))) && isinteger(x)
if ($(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti))+one(Tf))) && isinteger(x)
Copy link
Member

Choose a reason for hiding this comment

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

I think this deserves a source comment about why this isn't equivalent to <=.

return unsafe_trunc($Ti,x)
else
throw(InexactError($(Expr(:quote,Ti.name.name)), $Ti, x))
Expand Down
9 changes: 9 additions & 0 deletions test/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,12 @@ end
@test isapprox(typemin(T), 0.0, rtol=1)
end
end

@testset "Conversion to UInt near extremes (#51063)" begin
@test UInt64(4.2949673f9) === typemax(UInt32) + UInt64(1)
@test_throws InexactError UInt32(4.2949673f9)
@test UInt32(prevfloat(4.2949673f9)) === 0xffffff00
@test UInt128(1.8446744073709552e19) === typemax(UInt64) + UInt128(1)
@test_throws InexactError UInt64(1.8446744073709552e19)
@test UInt64(prevfloat(1.8446744073709552e19)) === 0xfffffffffffff800
end