只是出于好奇。

typeof NaN是number似乎不太符合逻辑。就像NaN === NaN或NaN == NaN返回false。这是JavaScript的特性之一吗,或者有什么原因吗?

编辑:谢谢你的回答。然而,让人明白这不是一件容易的事情。阅读答案和维基我明白了更多,但仍然,一句话像

与NaN的比较总是返回无序结果,即使是与NaN本身比较也是如此。比较谓词是信号或非信号,信号版本表示此类比较的无效异常。等式和不等式谓词是非信号,因此x = x返回false可用于测试x是否为静态NaN。

让我头晕目眩。如果有人能把它翻译成人类(而不是数学家)可读的语言,我会很感激。


当前回答

因为NaN是数值型数据类型。

其他回答

考虑NAN的最好方法是它不是一个已知的数字。这就是为什么NAN != NAN,因为每个NAN值代表一个唯一的未知数字。nan是必要的,因为浮点数的值范围有限。在某些情况下,舍去小数位会丢失,导致出现像1.0/11*11 != 1.0这样的无意义的结果。真正大的值是nan,无穷大就是一个很好的例子。

鉴于我们只有十个手指,任何显示大于10的值的尝试都是不可能的,这意味着这些值必须是nan,因为我们已经丢失了这个大于10的值的真实值。浮点值也是如此,其值超过了浮点数所能容纳的限制。

这只是因为NaN是JS中Number对象的一个属性,它与它是否是一个数字无关。

好吧,这似乎有点奇怪,所谓的“不是一个数字”被认为是一个数字,但NaN仍然是一个数字类型,尽管事实如此:-)

NaN仅仅意味着特定的值不能在数字类型的限制范围内表示(尽管这可以说是所有必须四舍五入以适应的数字,但NaN是一个特殊情况)。

一个特定的NaN不等同于另一个NaN,因为它们可能是不同的值。然而,NaN仍然是一种数字类型,就像2718或31415一样。


至于你更新的问题,用外行的话解释一下:

与NaN的比较总是返回无序结果,即使是与NaN本身比较也是如此。比较谓词是信号或非信号,信号版本表示此类比较的无效异常。等式和不等式谓词是无信号的,因此x = x返回false可用于测试x是否为静态NaN。

所有这一切的意思是(分解为几个部分):

与NaN的比较总是返回无序结果,即使是与NaN本身比较也是如此。

基本上,一个NaN不等于任何其他数字,包括另一个NaN,甚至包括它自己。

比较谓词是信号或非信号,信号版本表示此类比较的无效异常。

试图在一个NaN和另一个数字之间进行比较(小于、大于等)操作,可能导致抛出异常(发出信号),也可能导致结果为false(非发出信号或为quiet)。

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

相等(等于,不等于)的测试永远不会发出信号,因此使用它们不会导致异常。如果有一个正则数x,那么x == x总是成立的。如果x是一个NaN,那么x == x将永远是假的。它为您提供了一种轻松(悄悄)检测NaN的方法。

它的意思是不是一个数字。这不是javascript的特性,而是常见的计算机科学原理。

从http://en.wikipedia.org/wiki/NaN:

There are three kinds of operation which return NaN: Operations with a NaN as at least one operand Indeterminate forms 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.

所有这些值可能都不相同。NaN的一个简单测试是测试value == value为false。

Javascript uses NaN to represent anything it encounters that can't be represented any other way by its specifications. It does not mean it is not a number. It's just the easiest way to describe the encounter. NaN means that it or an object that refers to it could not be represented in any other way by javascript. For all practical purposes, it is 'unknown'. Being 'unknown' it cannot tell you what it is nor even if it is itself. It is not even the object it is assigned to. It can only tell you what it is not, and not-ness or nothingness can only be described mathematically in a programming language. Since mathematics is about numbers, javascript represents nothingness as NaN. That doesn't mean it's not a number. It means we can't read it any other way that makes sense. That's why it can't even equal itself. Because it doesn't.