[Numpy-discussion] np.nan and ``is``

Christian Heimes lists@cheimes...
Fri Sep 19 15:04:19 CDT 2008


Andrew Dalke wrote:
> There are a few things that Python-the-language guarantees are singleton
> objects which can be compared correctly with "is".  Those are:
> 
>    True, False, None

The empty tuple () and all interned strings are also guaranteed to be 
singletons. String interning is used to optimize code on C level. It's 
much faster to compare memory addresses than objects. All strings can be 
interned through the builtin function intern like s = intern(s). For 
Python 3.x the function was moved in the the sys module and changed to 
support str which are PyUnicode objects.


> So, back to NaN.  There's no guarantee NaN is a singleton
> object, so testing with "is" almost certainly is wrong.
> In fact, at the bit-level there are multiple NaNs.  A
> NaN (according to Wikipedia) fits the following bit pattern.
> 
>    NaN: x11111111axxxxxxxxxxxxxxxxxxxxxx. x = undefined. If a = 1,
> 
>    it is a quiet NaN, otherwise it is a signalling NaN.

The definition is correct for all doubles on IEEE 754 aware platforms. 
Python's float type uses the double C type. Almost all modern computers 
have either hardware IEEE 754 support or software support for embedded 
devices (some mobile phones and PDAs). 
http://en.wikipedia.org/wiki/IEEE_754-1985

The Python core makes no difference between quiet NaNs and signaling 
NaNs. Only errno, input and output values are checked to raise an 
exception. We were discussion the possibility of a NaN singleton during 
our revamp of Python's IEEE 754 and math support for Python 2.6 and 3.0. 
But we decided against it because the extra code and cost wasn't worth 
the risks. Instead I added isnan() and isinf() to the math module.

All checks for NaN, inf and the sign bit of a float must be made through 
the appropriate APIs - either the NumPy API or the new APIs for floats.

Hope to shed some light on things
Christian



More information about the Numpy-discussion mailing list