参见英文答案 >
What is the rationale for all comparisons returning false for IEEE754 NaN values?9个
我想知道为什么undefined == undefined但NaN!= NaN.
我想知道为什么undefined == undefined但NaN!= NaN.
解决方法
因为这是
Abstract Equality Comparison Algorithm和
Strict Equality Comparison Algorithm中的定义.
如果任一操作数为==或===为NaN,则返回false.
抽象
- If Type(x) is Number,then
- If x is NaN,return false.
- If y is NaN,return false.
- If x is the same Number value as y,return true.
- If x is +0 and y is −0,return true.
- If x is −0 and y is +0,return true.
- Return false.
编辑:@CMS所述的不平等比较动机符合IEEE 754标准.
…The normal comparison operations however treat NaNs as unordered and compare −0 and +0 as equal. The totalOrder predicate will order these cases,and it also distinguishes between different representations of NaNs and between the same decimal floating point number encoded in different ways.