-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Disallow rem(x, y::Unsigned, RoundUp)
#39318
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
base: master
Are you sure you want to change the base?
Conversation
The current (master) implementation is buggy. However, considering the promotion, I think the prohibition is too strict. julia> div(3.0, UInt(2), RoundUp)
2.0
julia> rem(3.0, UInt(2), RoundUp)
3.0 # bug (#34325)
-1.0 # expected
julia> div(3, UInt8(2), RoundUp)
2
julia> rem(3, UInt8(2), RoundUp)
0x0000000000000003 # bug (#34325)
-1 # expected Just FYI, I implemented some arithmetic operations involving saturating arithmetic in From the perspective of consistency, I would like to relax the restriction. Base.rem(x, y, r::RoundingMode{:Up}) = rem(promote(x, y)..., r)
Base.rem(x::T, y::T, ::RoundingMode{:Up}) where {T} = mod(x, -y)
Base.rem(x::T, y::T, ::RoundingMode{:Up}) where {T <: Unsigned} = y == oneunit(T) ? zero(T) : error() |
I agree, the current implementation is too strict. However, I think it’s better to always throw an error in |
However, other julia> rem(Int(5), UInt(2), RoundDown)
0x0000000000000001
julia> rem(UInt(5), Int(2), RoundDown)
1
julia> rem(Int(5), UInt(2))
1
julia> rem(UInt(5), Int(2))
0x0000000000000001 |
Ah, good reminder of complicated remainder! |
Seems like fixing this would be more appropriate than disallowing it. |
Do we want to get this merged for now? |
Fixes #34325.