只是出于好奇。

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

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

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

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


当前回答

我们可以认为NaN是一个特殊情况对象。在本例中,NaN的对象表示一个没有数学意义的数字。数学中还有一些特殊情况的对象,比如INFINITE等等。

你仍然可以用它做一些计算,但这会产生奇怪的行为。

更多信息在这里:http://www.concentric.net/~ttwang/tech/javafloat.htm(基于java,不是javascript)

其他回答

NaN是一个有效的浮点值(http://en.wikipedia.org/wiki/NaN)

NaN === NaN为假,因为它们不一定是相同的非数字

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.

它的意思是不是一个数字。这不是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。

它是Number类型的特殊值,如POSITIVE_INFINITY

为什么?通过设计

一个例子

假设我们正在将一个字符串转换为一个数字:

Number("string"); // returns NaN

我们将数据类型更改为数字,但它的值不是数字!