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

BUG: clamp(typemax(UInt64), Int64) errors #54022

Closed
KlausC opened this issue Apr 10, 2024 · 4 comments · Fixed by #54038
Closed

BUG: clamp(typemax(UInt64), Int64) errors #54022

KlausC opened this issue Apr 10, 2024 · 4 comments · Fixed by #54038
Labels
bug Indicates an unexpected problem or unintended behavior maths Mathematical functions

Comments

@KlausC
Copy link
Contributor

KlausC commented Apr 10, 2024

The call clamp(typemax(UInt64), Int64) should return typemax(Int64) but throws InexactError.

julia> clamp(typemax(UInt64), Int64)
ERROR: InexactError: check_top_bit(UInt64, -9223372036854775808)
Stacktrace:
 [1] throw_inexacterror(f::Symbol, ::Type{UInt64}, val::Int64)
   @ Core ./boot.jl:634
 [2] check_top_bit
   @ ./boot.jl:648 [inlined]
 [3] toUInt64
   @ ./boot.jl:759 [inlined]
 [4] UInt64
   @ ./boot.jl:789 [inlined]
 [5] convert
   @ ./number.jl:7 [inlined]
 [6] clamp
   @ ./math.jl:98 [inlined]
 [7] clamp(x::UInt64, ::Type{Int64})
   @ Base.Math ./math.jl:123
 [8] top-level scope
   @ REPL[1]:1
@KlausC
Copy link
Contributor Author

KlausC commented Apr 10, 2024

The same happens for bit sizes 8, 16, 32, 64, 128.

@barucden
Copy link
Contributor

barucden commented Apr 10, 2024

It's due to ifelse being used in

julia/base/math.jl

Lines 99 to 102 in e9a24d4

ifelse(x > hi, convert(promote_type(X,L,H), hi),
ifelse(x < lo,
convert(promote_type(X,L,H), lo),
convert(promote_type(X,L,H), x)))

Since ifelse is a function, all arguments are evaluated. That is

convert(UInt64, typemax(Int64))
convert(UInt64, typemin(Int64))
convert(UInt64, typemax(UInt64))

with the second one throwing the reported error since typemin(Int64) is negative. We might replace ifelse with the ternary conditional operator (or normal conditional statements), but I am not sure whether it has some performance consequences.

@vtjnash
Copy link
Member

vtjnash commented Apr 10, 2024

Ternary is nearly always faster and usually generates better code

barucden added a commit to barucden/julia that referenced this issue Apr 11, 2024
@KlausC
Copy link
Contributor Author

KlausC commented Apr 11, 2024

See my comment in #54038.
Additionally something like clamp(x::Int64, ::Type{UInt64}) = UInt64(max(x, 0)) for all bit sizes would solve the issue, probably.

barucden added a commit to barucden/julia that referenced this issue Apr 12, 2024
Fixes JuliaLang#54022

Co-authored-by: Klaus Crusius <KlausC@users.noreply.github.com>
barucden added a commit to barucden/julia that referenced this issue Apr 12, 2024
Fixes JuliaLang#54022

Co-authored-by: Klaus Crusius <KlausC@users.noreply.github.com>
barucden added a commit to barucden/julia that referenced this issue Apr 12, 2024
Fixes JuliaLang#54022

Co-authored-by: Klaus Crusius <KlausC@users.noreply.github.com>
barucden added a commit to barucden/julia that referenced this issue Apr 12, 2024
Fixes JuliaLang#54022

Co-authored-by: Klaus Crusius <KlausC@users.noreply.github.com>
@oscardssmith oscardssmith added bug Indicates an unexpected problem or unintended behavior maths Mathematical functions labels Apr 12, 2024
barucden added a commit to barucden/julia that referenced this issue Apr 12, 2024
Fixes JuliaLang#54022

Co-authored-by: Klaus Crusius <KlausC@users.noreply.github.com>
oscardssmith pushed a commit that referenced this issue Apr 13, 2024
Fixes #54022

Co-authored-by: Klaus Crusius <KlausC@users.noreply.github.com>
KristofferC pushed a commit that referenced this issue May 28, 2024
Fixes #54022

Co-authored-by: Klaus Crusius <KlausC@users.noreply.github.com>
(cherry picked from commit a721658)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior maths Mathematical functions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants