hi, I am trying to understand how Clojure multiplication works with double numbers, sometimes I get incorrect fractional result, ie:
user=> (* 221 0.55)
121.55000000000001
It happens with couple of other numbers as well, but it seems that 221 hits it pretty often. Is this expected behaviour?
1 Like
Yes, this is expected. Floating point arithmetic has been standardised in IEEE 754 and so this behaviour is common to most popular hardware and languages. If you want to know more about this, see What Every Programmer Should Know About Floating-Point Arithmetic.
3 Likes
NobbZ
3
Another nice ressource I usually refer to is this:
https://0.30000000000000004.com/
2 Likes
I spot an error:
When you have a base-10 system (like ours), it can only express fractions that use a prime factor of the base.
This suggests 1 / (2×3) is representable, but it isn’t. It should say
When you have a base-10 system (like ours), it can only express fractions that use only prime factors of the base.
kotp
5
I think that is a good suggestion, though I would consider this instead:
When you have a base-10 system (like ours), it can only express fractions that use only prime factor(s) of the base.
Notice the parenthesis to denote single or more.