Calculation of remainder, floating point issues

In particular, 0.00625 is not exactly representable in binary floating point, so it is rounded to a slightly larger value:

julia> big(0.00625)
0.00625000000000000034694469519536141888238489627838134765625

(In contrast, 5.0 is exactly represented.) So, you are actually computing the remainder of 5 \div 0.00625000000000000034694469519536141888238489627838134765625, which is not zero. The correct quotient is therefore 799, not 800 (=5/0.00625), and the correct remainder computed in arbitrary precision arithmetic to about 300 decimal digits (setprecision(BigFloat, 1024)) is:

julia> 5 - 799 * big(0.00625)
0.00624999999999972279118853890622631297446787357330322265625

If we round this to the nearest Float64 value, we get:

julia> Float64(5 - 799 * big(0.00625))
0.006249999999999723

julia> rem(5, 0.00625)
0.006249999999999723

So, in fact, rem is giving exactly the correct answer to the question you asked (i.e., the exact answer rounded to Float64).

It’s just that you are not asking the question you thought you were asking, because 0.00625 in floating point is not the number you think it is.

5 Likes