How to determine if a number is infinite using Javascript?

Asked

Viewed 215 times

7

There is the possibility to check if a certain value is a infinite number using Javascript?

  • The question does not match the title. What exactly is the doubt?

  • Voce could check the type with variable typeof if it is Infinity then you discovered

2 answers

8

You can use the function isFinite():

console.log(!isFinite(Number.MAX_SAFE_INTEGER)); // false
console.log(!isFinite(Infinity));                // true

Another way is to compare the value with Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY:

if (numero == Number.POSITIVE_INFINITY || numero == Number.NEGATIVE_INFINITY) {
    alert('Número infinito!');
} else {
    alert('Número finito!')
}
  • 1

    Only one thing, the way you described it seems that isFinite checks if it’s infinite, but it’s the other way around, then it would have to do something like !isFinite(number), agrees?

  • 2

    @Guillhermenascimento isFinite returns false to NaN, if you use !isFinite as a matter of fact for isInfinite, the value NaN will be considered infinite. The best choice is to perform the tests of POSITIVE_INFINITY and NEGATIVE_INFINITY at hand. I also do not use advice isFinite because he enforces coercion, so he accepts null as a finite value. Best to use Number.isInfinite.

  • @Gabrielkatakura as far as I know NaN is not really a number, the correct would be to check both var isInfinite = (!isNaN(n) && !isFinite(n));, agrees? ;)

  • @Guilhermenascimento yes, that’s what I was talking about to be done ;)

6


Apart from the example of the other answer that got very good, you can also do so:

var n = ...; //Valor infinito ou não
var isInfinite = (!isNaN(n) && !isFinite(n));

console.log(isInfinite);

The isNaN is to prevent chance !isFinite(n); return true chance to receive a number NaN

Could move to a function to facilitate:

function isInfinite(num) {
    return !isNaN(num) && !isFinite(num);
}

Or

function isInfinite(num) {
     return num == Number.POSITIVE_INFINITY ||
            num == Number.NEGATIVE_INFINITY;
}

The use would look like this:

alert(isInfinite(numero));

Yet I read in the first issue that you used a number like this: 100000000000000000000000000000100000000000000000000000000000, actually this is not infinite, this would be a number that exceeds the limit

A simple test:

function checkNumberSizeValide(num) {
    return num <= Number.MAX_SAFE_INTEGER;
}

function isInfinite2(num) {
     return num == Number.POSITIVE_INFINITY ||
            num == Number.NEGATIVE_INFINITY;
}

var valor = window.prompt("Digite um numero, pode ser inteiro ou quebrado, exemplo 1.0000002");

if (/^(\d+|\d+[.]\d+)$/.test(valor)) {
    valor = Number(valor);

    console.log("valor", valor);

    console.log("O numero é valido dentro do limite máximo:", checkNumberSizeValide(valor));
} else {
    alert("Digite um numero");
}

  • 1

    +1. I had also seen the number in the first issue... until I was formulating it in the reply, what I would suggest was the Number.isSafeInteger() but your solution is also good. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.