Why are some floating point values not represented exactly in Python?
The short answer: Because computers operate in base 2 (binary) while natural floating point numbers are in base 10 (decimal), most floating point numbers cannot be represented exactly in the computer. This is not a problem with Python but with all binary computers.
The section "Floating Point Arithmetic: Issues and Limitations" in the Python Tutorial provides a really clear description of why this is the case. In The Perils of Floating Point Bruce Bush provides ways on working around this inherent limitation.
There are packages in Python that provide ready-made ways
of getting around this problem.
For example, the
performs a safe-comparison between two
If you want to write your own safe-comparisons, however,
there isn't a ready-made way of finding out floating-point
precision in Python (Martelli 2003, p. 34).