只是出于好奇。

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

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

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

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


当前回答

NaN != NaN,因为它们不是相同的非数字。因此……很有道理。 浮点数+0.00和-0.00不一样的原因。四舍五入可以做到它们实际上不为零。

至于typeof,这取决于语言。大多数语言会说NaN是浮点数、双精度数或数字,这取决于他们如何对它进行分类……据我所知,没有一种语言会说这是未知类型或null。

其他回答

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

你必须热爱Javascript。它有一些有趣的小怪癖。

http://wtfjs.com/page/13

如果你停下来从逻辑上算出这些怪癖,或者如果你知道一点数论,大多数怪癖都可以解释清楚,但如果你不知道它们,它们仍然会让你出错。

顺便说一下,我建议你阅读http://wtfjs.com/的其余部分——还有很多比这个更有趣的怪癖!

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.

NaN != NaN,因为它们不是相同的非数字。因此……很有道理。 浮点数+0.00和-0.00不一样的原因。四舍五入可以做到它们实际上不为零。

至于typeof,这取决于语言。大多数语言会说NaN是浮点数、双精度数或数字,这取决于他们如何对它进行分类……据我所知,没有一种语言会说这是未知类型或null。

从类型的角度来看,NaN是一个数字,但不是像1、2或329131这样的普通数字。“不是一个数字”的名字指的是表示的值是特殊的,是关于IEEE格式规范领域,而不是javascript语言领域。