python nan != nan

前端 未结 3 1222
夕颜
夕颜 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.

提交回复
热议问题