[Python-ideas] Float nan equality

Nick Coghlan ncoghlan at gmail.com
Tue Oct 4 07:43:03 EDT 2016


On 4 October 2016 at 21:32, Arek Bulski <arek.bulski at gmail.com> wrote:
> I had  a bug where nan floats failed to compare equal because there seems to
> be more than one nan value and comparison seems to be binary based.

"NaN != NaN" is part of the definition of IEEE 754 floats:
https://en.wikipedia.org/wiki/NaN#Floating_point

That's why it returns False even if you compare a specific NaN
instance with itself:

    >>> x = float("nan")
    >>> x == x
    False

If you need a kinda-like-NaN value that provides reflexive equality,
then Python's None singleton is a better fit.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list