How do I check if a number evaluates to infinity?

JavascriptInfinity

Javascript Problem Overview


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

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

Javascript Solutions


Solution 1 - Javascript

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.

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

Solution 2 - Javascript

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

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

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

Solution 3 - Javascript

A simple n === n+1 or n === n/0 works:

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.

Solution 4 - Javascript

Actually n === n + 1 will work for numbers bigger than 51 bit, e.g.

1e16 + 1 === 1e16; // true
1e16 === Infinity; // false

Solution 5 - Javascript

Perform the plain ol’ comparison:

(number === Infinity || number === -Infinity)

or to save several characters:

Math.abs(number) === Infinity

Why to use this

  1. !(Number.isFinite(number)) breaks on NaN inputs.
  2. Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY can be redefined; they are configurable.
  3. Infinity and -Infinity are read-only in the strict mode.
  4. It is the shortest solution.

Solution 6 - Javascript

I like to use Lodash for a variety of defensive coding reasons 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

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

_.isFinite(null); // false
_.isFinite(3); // true
_.isFinite('3'); // true

Solution 7 - Javascript

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:

  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).

So:

testInput(1/0); // true
testInput(parseInt("String")); // true
testInput("String"); // false

Solution 8 - Javascript

You can use isFinite in window, 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.isFinite which also check if the value is Number too and is more accurate for checking undefined and null etc...

Or you can polyfill it like this:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHomer_JView Question on Stackoverflow
Solution 1 - JavascriptLukeHView Answer on Stackoverflow
Solution 2 - JavascriptzangwView Answer on Stackoverflow
Solution 3 - JavascriptryanveView Answer on Stackoverflow
Solution 4 - JavascriptYuriView Answer on Stackoverflow
Solution 5 - JavascriptКонстантин ВанView Answer on Stackoverflow
Solution 6 - Javascriptrandom_user_nameView Answer on Stackoverflow
Solution 7 - JavascriptdmitrizzleView Answer on Stackoverflow
Solution 8 - JavascriptAlirezaView Answer on Stackoverflow