A recent project euler problem made me realize when precision matters with floating point numbers. Consider
It’s important to understand when the precision matters in your program. Almost all platforms map Python floats to IEEE-754 “double precision”, which contain 53 bits of binary precision (think a number like
10011010010100010010010101011101111111111011101110101). In base-10 or decimal number system, this is roughly 17 digits (think since 10 is roughly
2**3, so each decimal digit is roughly the same as 3 binary digits, so 53 binary digits is roughly 17 decimal digits).
How “much” precision is 17 decimal digits? Well, let’s take the Great Wall of China (roughly 10,000 km). 17 digits of precision gives us about 10 atoms width of precision!
As proof, we can check a given number
x by adding a very small number
epsilon, to see how big
epsilon can be before
x + epsilon are “different”. I’ve included some code below and its output(requires the python library humanize).