Divising a DateTime by 1 or 10 yields different floating point precision

I have come to an inexplicable (at least for me at the moment) behavior in Julia

julia> using Dates
julia> Dates.value(Minute(60)) / 1 * 38.849
2330.9399999999996
julia> Dates.value(Dates.value(Minute(60)) * 38.849
2330.9399999999996
julia> Dates.value(Minute(60)) / 10 * 38.849
233.094
julia> Dates.value(Minute(60)) * 3.8849
233.094

Can you explain why the first two don’t yield the same precision as the two latest?

This isn’t about Dates, but rather that floating point arithmetic doesn’t work quite how one may expect. See:

3.8849 == 38.849/10 # false

This isn’t even about Julia, since Julia implements the industry standard IEEE 754 that most languages do.

Essentially, if you’re using floats you can’t usually use == as the order of operations can matter and slight differences can occur.

2 Likes

That question is common enough that there’s a PSA:

5 Likes

Thanks for your replies :slight_smile:
Just in case for future readers, what I actually wanted to ask has it answers here :smiley: