为什么undefined == undefined但是NaN!= NaN? [英] Why is undefined == undefined but NaN != NaN?
问题描述
我想知道为什么 undefined == undefined
但是 NaN!= NaN
。
推荐答案
Because that's how it is defined in both the Abstract Equality Comparison Algorithm, and the Strict Equality Comparison Algorithm.
如果任一操作数为 ==
或 ===
是 NaN
,它返回 false
。
If either operand to ==
or ===
is NaN
, it returns false
.
摘要
- 如果Type(x)是Number,那么
- 如果x为NaN,则返回false。
- 如果y为NaN,则返回false。
- 如果x与y的数值相同,则返回true。
- 如果x为+ 0且y为-0,则返回true。
- 如果x是-0且y是+0,则返回true。
- 返回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 motivation for the unequal comparison as noted by @CMS is compliance with the IEEE 754 standard.
从以下评论中提供的维基百科链接:
From the Wikipedia link provided in the comment below:
...然而,正常的比较操作将NaN视为无序,并将-0和+ 0相等。 totalOrder谓词将对这些情况进行排序,它还可以区分NaN的不同表示以及以不同方式编码的相同十进制浮点数。
...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.
这篇关于为什么undefined == undefined但是NaN!= NaN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!