Python nan! = Nan

Python 2.7.3 (default, Aug 1 2012, 05:14:39) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> x = 0 * 1e3000 >>> id(x) == id(x) True >>> x == x False 

I am wondering how nan != nan in python. And just to clarify, I know that nan should behave as by definition, I ask how not about why. Where is this implemented? Is there any other object that behaves like this?

+5
python math nan
source share
3 answers

In the "where" section, see your questions on line 391 in Object / floatobject.c in the Python 2.7.3 source tree. A brief discussion is given of the behavior of NaN = NaN followed by implementation.

For other cases that exhibit similar behavior, this is certainly possible. However, I did not conduct an exhaustive search for libraries, so I can not give a definitive answer.

+3
source share

Not a number (NaN) does not match any. To detect this, use math.isnan . And such an object is pretty easy to define:

 class A(object): def __eq__(self, other): return False def __ne__(self, other): return True 

The reason for this is quite simple. CPython follows the IEEE 754 floating point math standard. NaN is a floating point value for which IEEE 754 dictates that it is not equal to any other floating point value.

+4
source share

Machine code that implements floating point operations processes the result of operations with NaN. For a series of x86 processors, this is usually achieved using the instructions of the x87 coprocessor, although for earlier x86 processors, where the x87 coprocessor was not always present, the compiler usually provided an emulation code.

+3
source share

All Articles