-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Feature request: round-to-nearest integer division #30099
Comments
What is the problem of the code you wrote? |
Rather than add a new function, we can just overload |
@chethega The code I wrote is an experiment with fixed point arithmetic, quite similar to @simonbyrne Yes, overloading (This is the function I wrote: # Correctly rounded division (breaking ties towards even)
export rdd
function rdd(x::T, y::Signed)::T where {T <: Signed}
d, m = fldmod(x, y)::Tuple{T, T}
a2m = abs(m << 1)
ay = abs(y)
if a2m > ay
# Round up
d += T(1)
elseif a2m == ay
# Break tie towards even (round up if the result is odd)
d += d & T(1)
end
d
end ) |
For the benefit of those who stumble on this issue, Julia It would have saved me some floundering if someone had noted this and closed this issue. |
Integer division currently offers 3 rounding modes:
fld
,cld
, anddiv
, corresponding to the rounding modes offered byfloor
,ceil
, andtrunc
(round towards minus infinity, plus infinity, and zero).I suggest a fourth integer division function, maybe called
rdd
, that rounds towards the nearest integer, breaking ties towards even, corresponding to the rounding performed byround
:... except without converting to a floating-point number.
The text was updated successfully, but these errors were encountered: