1

为什么这两个不同?

var x = NaN; //e.g. Number("e");
alert(isNaN(x)); //true (good)
alert(x == NaN); //false (bad)
4

3 回答 3

1

等式和不等式谓词是无信号的,因此 x = x 返回 false 可用于测试 x 是否是安静的 NaN。

资源

这是 IEEE 754 中定义的规则,因此完全符合规范需要此行为。

于 2013-02-20T17:45:03.490 回答
1

没有什么是等于的NaN。任何比较永远都是false

在严格比较算法和抽象比较算法中,如果类型相同,且任一操作数为NaN,则结果为false

如果 Type(x) 是 Number,那么

  • 如果xNaN,则返回false
  • 如果yNaN,则返回false

在抽象算法中,如果类型不同,并且aNaN是其中一个操作数,那么另一个操作数最终会被强制转换为一个数字,将我们带回到上面的场景。

于 2013-02-20T17:47:42.943 回答
0

以下操作返回 NaN

The divisions 0/0, ∞/∞, ∞/−∞, −∞/∞, and −∞/−∞
The multiplications 0×∞ and 0×−∞
The power 1^∞
The additions ∞ + (−∞), (−∞) + ∞ and equivalent subtractions.
Real operations with complex results:

The square root of a negative number
The logarithm of a negative number
The tangent of an odd multiple of 90 degrees (or π/2 radians)
The inverse sine or cosine of a number which is less than −1 or greater than +1.

以下运算返回数值运算的值。因此typeof南是一个数字。NaN 在数学上是一个未定义的数字。∞ + (-∞) 不等于 ∞ + (-∞)。但是我们得到 NaN 是typeof数字,因为它是数字运算的结果。

来自维基

于 2013-02-20T17:45:43.973 回答