FW: Why float('Nan') == float('Nan') is False
Chris Angelico
rosuav at gmail.com
Thu Feb 14 23:11:08 EST 2019
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
More information about the Python-list
mailing list