-
-
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
div/fld floating point issue #4156
Comments
Is this something we need to address by 0.2? |
It's at least rather embarrassing. Here's another somewhat simpler and slightly worse example: julia> 0.3/0.01
30.0
julia> fld(0.3,0.01)
28.0 What happens is that div{T<:Real}(x::T, y::T) = convert(T,trunc((x-rem(x,y))/y)) expands to
The first two lines cast some doubt about the validity of the fix of #3127, but that's nothing to try to solve with quick fixes. From the last two lines, however, I agree with Simon that trunc should be round. In retrospect it looks obvious that the fix of #3321 should have used round in the first place. |
Hello! It seems, your implementation is not correct for: Thanks |
@Yaffle The accuracy of Comparing your examples with integer division (which is exact), using Julia 0.3, julia> int(div(18014398509481992., 7.))
2573485501354571
julia> int(div(18014398509481992, 7))
2573485501354570
julia> int(div(18014398509481988., 3.))
6004799503160661
julia> int(div(18014398509481988, 3))
6004799503160662 which are both wrong. In Julia 0.4, the first one is actually correct, but this is just due to the change in |
@simonbyrne Thanks for your quick reply! I am not an expert in floating point, but I have my own candidate (the code is in JavaScript):
What do you think? |
Hmm, something like that might work, I'll have to think about it more. If you only care about integers, there is this neat result by Vincent Lefèvre (who knows much more about this stuff than I do): Annoyingly, the actual CPU instructions (FPREM/FPREM1) also compute the last three bits of the quotient, which would be enough to determine which of the correct values we want, but there doesn't seem to be a good way to get to it: this is exposed by a libm |
Interesting trivia: from what I can tell, computing remainders seems to be the only floating point functionality that is still handled by x87 instructions, instead of SSE. |
in C++, possilbly, |
I think you're right. The only potential problem I can think of with your proposed solution is x/y is in the range ( 2^52, 2^53 ) (when As you suggest, using the C But I'm also not sure this situation can actually happen: can you think of any such x, y for which this would occur? |
No, I cannot, but I cannot garantee that there are no. |
Here's another, in the spirit of #3127 and #3321:
I would think that changing the
trunc
andfloor
toround
would fix this?The text was updated successfully, but these errors were encountered: