python nan != nan

前端 未结 3 1180
夕颜
夕颜 2021-01-03 02:08
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 =          


        
相关标签:
3条回答
  • 2021-01-03 02:10

    Not A Number (NaN) is unequal with anything. To detect it, use math.isnan. And an object like this is quite easy to define:

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

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

    0 讨论(0)
  • 2021-01-03 02:32

    For the "where" part of your questions, look starting at line 391 in Objects/floatobject.c in the Python 2.7.3 source tree. A brief discussion is given about the behavior of NaN == NaN with the implementation following.

    With respect to other cases that exhibit similar behavior, it is certainly possible. I have not done an exhaustive search of the libraries however, so I can't give a definitive answer.

    0 讨论(0)
  • 2021-01-03 02:32

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

    0 讨论(0)
提交回复
热议问题