javascript 为什么在 isFinite() 之后检查 !isNaN()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5690071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:02:05  来源:igfitidea点击:

Why check for !isNaN() after isFinite()?

javascriptnangoogle-closure-library

提问by pimvdb

I came across the goog.math.isFiniteNumberfunction in the Google Closure Library. What it does is checking whether a given number is both finite and not NaN.

goog.math.isFiniteNumberGoogle Closure Library 中遇到了这个函数。它所做的是检查给定的数字是否既是有限的又不是NaN

The underlying code is:

底层代码是:

goog.math.isFiniteNumber = function(num) {
  return isFinite(num) && !isNaN(num);
};

So, first it checks whether the number is finite using the native isFinitefunction, and then does an additional check to make sure the number isn't NaNusing isNaN.

因此,它首先使用本机isFinite函数检查数字是否有限,然后进行额外检查以确保数字未NaN使用isNaN.

However, isFinitealready returns false in case the argument is NaN. So, what advantages does the check for isNaNprovide?

但是,isFinite如果参数为,则已经返回 false NaN。那么,支票有什么好处isNaN呢?

采纳答案by Zach Rattner

If isFiniteworked the way isFiniteNumberdid, then there would be no reason to write isFiniteNumber. There's probably some browser out there somewhere that treats NaN as finite.

如果isFinite按照isFiniteNumber那样工作,那么就没有理由写 isFiniteNumber。可能有一些浏览器将 NaN 视为有限的。

回答by kumarharsh

The only difference is this:

唯一的区别是:

!isNan(1/0) // --> true
isFinite(1/0) // --> false

isNaN checks whether the argument is a number or not. The Infinities (+/-) are also numerical, thus they pass the isNaN check, but don't pass the isFinite check.

isNaN 检查参数是否为数字。Infinities (+/-) 也是数字,因此它们通过 isNaN 检查,但不通过 isFinite 检查。

** Note that any string which can be parsed as a number ("2", "3.14") will cause isNaN to return false.

** 请注意,任何可以解析为数字(“2”、“3.14”)的字符串都会导致 isNaN 返回 false。

Hope this helps.

希望这可以帮助。

PS: The answer given by user1170379 was very nearly perfect.

PS:user1170379 给出的答案非常接近完美。

回答by kumarharsh

you might reason out [Why?] after reading this:

阅读本文后,您可能会推理出[为什么?]:

NaN doesn't check if the passed value is infinite or not - it checks if the input val evaluates into a "Type: Number" end-result. Because isNaN(string) is accepted, so the: isNaN("3.14") //false (which means true, the given token is duck converted into a type Number successfully )

NaN 不检查传递的值是否是无限的 - 它检查输入 val 的计算结果是否为“类型:数字”最终结果。因为isNaN(string)被接受了,所以:isNaN("3.14") //false(意思是true,给定的token被duck转换成Number类型成功)

You may understand that the input value may happen to be an unresolved brute number, even a math operation as simple as: (x/y); which in turn might yield a (+/-infinity) number.

您可能会理解,输入值可能恰好是一个无法解析的蛮数,甚至是一个简单的数学运算:(x/y); 这反过来可能会产生一个(+/-无穷大)数。

Here x=1, y=0; meaning (1/0).Then isNaN(x/y) will first evaluate to isNaN(1/0); then to isNaN(infinity) //false. Since (1/0)=infinity is of type: "number" ie typeof(1/0) //"number" isNaN should and will return false.

这里x=1,y=0;意思是 (1/0)。然后 isNaN(x/y) 将首先评估为 isNaN(1/0); 然后到 isNaN(infinity) //false。由于 (1/0)=infinity 的类型是:"number" 即 typeof(1/0) //"number" isNaN 应该并且将返回 false。

You don't want to put "infinity" where an end result number is expected.

您不想将“无穷大”放在预期最终结果编号的位置。

回答by Rupert

Probably for the same reason that I have implemented (isfinite(num) && isfinite(-num))- I was getting errors from mysql complaining about putting "-nan" into the database even though I had a check for isfinite(field)...

可能与我实施的原因相同(isfinite(num) && isfinite(-num))- 我从 mysql 收到错误,抱怨将“-nan”放入数据库,即使我检查了isfinite(field)......

A useful article on this subject is http://Hymansondunstan.com/articles/983which provides an optimization ((d*0.0)==0.0)

关于这个主题的一篇有用的文章是http://Hymansondunstan.com/articles/983,它提供了一个优化((d*0.0)==0.0)

回答by Sri Dasari

isNaN() returns true if the argument is not a number or if the argument is a non-numeric value such as a string or an object.Otherwise, It returns false. Example: isNaN(0/0) =>true;isNaN(2-1) =>false;isFinite() returns true if the argument is a number other than NaN,Infinity or -Infinity.Otherwise, It returns false. Example: isFinite("2000") =>false;isFinite(200/2) =>true;`

如果参数不是数字或者参数是非数字值(例如字符串或对象),则 isNaN() 返回 true。否则,它返回 false。示例:isNaN(0/0) =>true;isNaN(2-1) =>false;如果参数是 NaN、Infinity 或 -Infinity 以外的数字,则 isFinite() 返回 true。否则,它返回 false。示例:isFinite("2000") =>false;isFinite(200/2) =>true;`