我如何检查一个数字是否等于无穷?

时间:2021-05-29 07:32:17

I have a series of Javascript calculations that (only under IE) show Infinity depending on user choices.

我有一系列Javascript计算(仅在IE下),根据用户选择显示无穷大。

How does one stop the word Infinity appearing and for example, show 0.0 instead?

如何阻止无穷大这个词的出现,例如,显示0 ?

5 个解决方案

#1


151  

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.

你可以用islimited函数来代替,这取决于你如何处理NaN。如果你的数字是正无穷、负无穷或NaN,那么它就是假的。

if (isFinite(result))
{
    // ...
}

#2


9  

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.

请注意,本机islimited()将输入强制为数字。islimited([])和islimited (null)都是正确的。

#3


4  

In ES6, The Number.isFinite() method determines whether the passed value is a finite number.

在ES6中,number. islimited()方法确定传递的值是否是一个有限数。

Number.isFinite(Infinity);  // false
Number.isFinite(NaN);       // false
Number.isFinite(-Infinity); // false

Number.isFinite(0);         // true
Number.isFinite(2e64);      // true

#4


2  

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

#5


0  

You can use isFinite in window, isFinite(123):

你可以用islimited在窗口,islimited (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.isFinite which also check if the value is Number too and is more accurate for checking undefined and null etc...

你也可以数。它还检查值是否为Number,更准确地检查未定义和null等。

Or you can polyfill it like this:

或者你可以这样填充:

Number.isFinite = Number.isFinite || function(value) {
  return typeof value === 'number' && isFinite(value);
}

#1


151  

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.

你可以用islimited函数来代替,这取决于你如何处理NaN。如果你的数字是正无穷、负无穷或NaN,那么它就是假的。

if (isFinite(result))
{
    // ...
}

#2


9  

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.

请注意,本机islimited()将输入强制为数字。islimited([])和islimited (null)都是正确的。

#3


4  

In ES6, The Number.isFinite() method determines whether the passed value is a finite number.

在ES6中,number. islimited()方法确定传递的值是否是一个有限数。

Number.isFinite(Infinity);  // false
Number.isFinite(NaN);       // false
Number.isFinite(-Infinity); // false

Number.isFinite(0);         // true
Number.isFinite(2e64);      // true

#4


2  

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

#5


0  

You can use isFinite in window, isFinite(123):

你可以用islimited在窗口,islimited (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.isFinite which also check if the value is Number too and is more accurate for checking undefined and null etc...

你也可以数。它还检查值是否为Number,更准确地检查未定义和null等。

Or you can polyfill it like this:

或者你可以这样填充:

Number.isFinite = Number.isFinite || function(value) {
  return typeof value === 'number' && isFinite(value);
}