In python 3.5, I was just playing around with comparison operators and came across this (seeming) oddity.
Is there a threshold of number of zeros after the decimal point past which the interpreter decides to not consider values relevant because of the inherent inaccuracy of floating point values?
In short, during the phase were parsing of input is done, Python needs to transform your input to a
C double
which can then be transformed to a Pythonfloat
. Inputs with more than 16 decimal digits are going to get approximated, with5.0000000000000001
getting approximated to5.0
:As a result the comparison
5 == 5.0000000000000001
is going to succeed (5
is going to get transformed to a Python float equal to5.0
in order for the comparison to take place).For digits less than the aforementioned, the result (can be represented) and speaks for itself:
Yes,
float_richcompare
has --unfortunately-- nothing to do with this behavior as I thought in my original comment on the question. It all happens before it gets invoked.