When to use the Decimal library over floats? 🤔
Today I was wondering why we were using the Decimal library instead of my favorite faulty friend, the IEEE 754 floating-point number. Well, it says it right in the readme.
Arbitrary precision decimal arithmetic.
But what is arbitrary precision, Dave?! 🥱
Well, http://0.30000000000000004.com/ can explain it better than I can, but let me illustrate with an example!
iex(7)> 0.1 + 0.2
0.30000000000000004
🤢🤮
No fear, the Decimal
library provides arbitrary precision floating points for us!
iex(1)> Decimal.add(Decimal.from_float(0.1), Decimal.from_float(0.2))
#Decimal<0.3>
Tweet