How Python Compares Floats and Ints: When Equals Isn’t Really Equal

abhi9u@lemmy.world to Technology@lemmy.world – 133 points –
How Python Compares Floats and Ints: Why It Can Give Surprising Results
blog.codingconfessions.com
43

You are viewing a single comment

Did nobody read the manual?

IEEE 754 double precision: The 53-bit significand precision gives from 15 to 17 significant decimal digits precision.

I'm not sure where the 17 comes from. It's 15.

The "15 to 17" part is worded somewhat confusingly, but it's not wrong.

The number of bits contained in a double is equivalent to ~15.95 decimal digits. If you want to store exactly a decimal number with a fixed number of significant digits, floor(15.95) = 15 digits is the most you can hope for. However, if you want to store exactly a double by writing it out as a decimal number, you need 17 digits.