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 Python float
. Inputs with more than 16 decimal digits are going to get approximated, with 5.0000000000000001
getting approximated to 5.0
:
>>> 5.0000000000000001
5.0
As a result the comparison 5 == 5.0000000000000001
is going to succeed (5
is going to get transformed to a Python float equal to 5.0
in order for the comparison to take place).
For digits less than the aforementioned, the result (can be represented) and speaks for itself:
>>> 5.000000000000001
5.000000000000001
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.