为什么NaN大于JavaScript中的任何数字? [英] Why NaN is greater than any number in JavaScript?

查看:82
本文介绍了为什么NaN大于JavaScript中的任何数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一个示例:

parseInt(50) > parseInt('a');

在控制台上执行此操作时,它将返回 false 。我的原始代码看起来有点像这样:

When executing this on a console, it will return false. My original code looks somewhat like this:

variableB = parseInt(jQuery('some-element').html());
if(parseInt(variableA) > variableB)
     // do something
else
     // do something else

有时 some-element 将不会被填充,因此返回 NaN 。发生这种情况时,我确实希望执行 else 块。我实际上得到了我的期望,但我只是想确保它确实打算以这种方式工作。

Sometimes the some-element will not be filled and thus return NaN. When this happens, I do want the else block to be executed. I am actually getting what I expect, but I just want to make sure that it indeed is intended to work this way.

推荐答案

在IEEE 754 arithmethic中, JavaScript使用的浮点模型<根据定义,/ a>, NaN (或非数字)不小于,等于或大于任何其他数字。

In IEEE 754 arithmethic, the floating-point model used by JavaScript, NaN (or not-a-number) is, by definition, not less than, equal to, or greater than any other number.

请注意,这甚至适用于两个 NaN :如果 x = NaN y = NaN ,比较 x === y 将返回false。

Notice that this even applies to two NaNs: if x = NaN and y = NaN, the comparison x === y will return false.

下面我引用 IEEE 754标准(第5.11节)指定了此行为:

Below I quote the portion of the IEEE 754 Standard (Section 5.11) where this behavior is specified:


可能存在四种相互排斥的关系:比,等于,
大于和无序。当至少一个
操作数是 NaN 时,会出现最后一种情况。每个 NaN 都应将无序与
的所有内容进行比较,包括其自身。

Four mutually exclusive relations are possible: less than, equal, greater than, and unordered. The last case arises when at least one operand is NaN. Every NaN shall compare unordered with everything, including itself.

这篇关于为什么NaN大于JavaScript中的任何数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆