osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

FW: Why float('Nan') == float('Nan') is False


On Fri, Feb 15, 2019 at 2:37 PM Avi Gross <avigross at verizon.net> wrote:
> But here is a curiosity. The numpy add-on package has a nan that is UNIQUE
> so two copies are the same. Read this transcript and see if it might
> sometimes even be useful while perhaps confusing the heck out of people who
> assume all nans are the same, or is it all nans are different?
>
> >>> floata = float('nan')
> >>> floatb = float('nan')
> >>> floata, floatb
> (nan, nan)
> >>> floata == floatb
> False
> >>> floata is floatb
> False
>
> >>> numpya = numpy.nan
> >>> numpyb = numpy.nan
> >>> numpya, numpyb
> (nan, nan)
> >>> numpya == numpyb
> False
> >>> numpya is numpyb
> True
>

You shouldn't be testing floats for identity.

>>> x = 2.0
>>> y, z = x+x, x*x
>>> y == z
True
>>> y is z
False

If nan identity is confusing people, other float identity should be
just as confusing. Or, just don't test value types for identity unless
you're actually trying to see if they're the same object.

ChrisA