Javascript 如何检查数字是否计算为无穷大?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4724555/
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
How do I check if a number evaluates to infinity?
提问by Homer_J
I have a series of Javascript calculations that (only under IE) show Infinity depending on user choices.
我有一系列 Javascript 计算(仅在 IE 下)根据用户选择显示 Infinity。
How does one stop the word Infinity
appearing and for example, show 0.0
instead?
如何停止Infinity
出现这个词,例如,显示出来0.0
?
回答by LukeH
if (result == Number.POSITIVE_INFINITY || result == Number.NEGATIVE_INFINITY)
{
// ...
}
You could possibly use the isFinite
function instead, depending on how you want to treat NaN
. isFinite
returns false
if your number is POSITIVE_INFINITY
, NEGATIVE_INFINITY
or NaN
.
您可以改用该isFinite
函数,具体取决于您想如何对待NaN
. 如果您的号码是,或,则isFinite
返回。false
POSITIVE_INFINITY
NEGATIVE_INFINITY
NaN
if (isFinite(result))
{
// ...
}
回答by ryanve
A simple n === n+1
or n === n/0
works:
一个简单的n === n+1
或n === n/0
有效的:
function isInfinite(n) {
return n === n/0;
}
Be aware that the native isFinite()
coerces inputs to numbers. isFinite([])
and isFinite(null)
are both true
for example.
请注意,本机将isFinite()
输入强制转换为数字。isFinite([])
并且isFinite(null)
都是true
例如。
回答by zangw
In ES6
, The Number.isFinite()
method determines whether the passed value is a finite number.
中ES6
,Number.isFinite()
方法判断传入的值是否为有限数。
Number.isFinite(Infinity); // false
Number.isFinite(NaN); // false
Number.isFinite(-Infinity); // false
Number.isFinite(0); // true
Number.isFinite(2e64); // true
回答by Yuri
Actually n === n + 1 will work for numbers bigger than 51 bit, e.g.
实际上 n === n + 1 将适用于大于 51 位的数字,例如
1e16 + 1 === 1e16; // true
1e16 === Infinity; // false
回答by random_user_name
I like to use Lodashfor a variety of defensive codingreasons as well as readability. ES6 Number.isFinite
is great and does not have issues with non-numeric values, but if ES6 isn't possible, you already have lodash, or want briefer code: _.isFinite
我喜欢将Lodash用于各种防御性编码原因以及可读性。ES6Number.isFinite
很棒并且没有非数字值的问题,但是如果 ES6 不可能,你已经有了 lodash,或者想要更简短的代码:_.isFinite
_.isFinite(Infinity); // false
_.isFinite(NaN); // false
_.isFinite(-Infinity); // false
_.isFinite(null); // false
_.isFinite(3); // true
_.isFinite('3'); // true
回答by dmitrizzle
I've ran into a scenario that required me to check if the value is of the NaN
or Infinity
type but pass strings as valid results. Because many text strings will produce false-positive NaN
, I've made a simple solution to circumvent that:
我遇到了一个场景,需要我检查值是否为NaN
orInfinity
类型,但将字符串作为有效结果传递。因为许多文本字符串会产生 false-positive NaN
,我做了一个简单的解决方案来规避:
const testInput = input => input + "" === "NaN" || input + "" === "Infinity";
The above code converts values to strings and checks whether they are strictly equal to NaN or Infinity (you'll need to add another case for negative infinity).
上面的代码将值转换为字符串并检查它们是否严格等于 NaN 或 Infinity(您需要为负无穷大添加另一种情况)。
So:
所以:
testInput(1/0); // true
testInput(parseInt("String")); // true
testInput("String"); // false
回答by Alireza
You can use isFinitein window, isFinite(123)
:
您可以使用isFinite的窗口,isFinite(123)
:
You can write a function like:
您可以编写如下函数:
function isInfinite(num) {
return !isFinite(num);
}
And use like:
并使用如下:
isInfinite(null); //false
isInfinite(1); //false
isInfinite(0); //false
isInfinite(0.00); //false
isInfinite(NaN); //true
isInfinite(-1.797693134862316E+308); //true
isInfinite(Infinity); //true
isInfinite(-Infinity); //true
isInfinite(+Infinity); //true
isInfinite(undefined); //true
You can also Number.isFinit
e which also check if the value is Number too and is more accurate for checking undefined
and null
etc...
您也可以Number.isFinit
è也检查值数过,是检查更精确undefined
和null
等...
Or you can polyfillit like this:
或者你可以像这样polyfill它:
Number.isFinite = Number.isFinite || function(value) {
return typeof value === 'number' && isFinite(value);
}