Skip to content Skip to sidebar Skip to footer

Can A Number In Javascript Ever Reach To Infinity In Runtime?

I was just curious, whether a number in JavaScript can ever reach Infinity. The range of JavaScript numbers is pretty good enough -- 2 to the power of 64 different numbers, which

Solution 1:

Can a number in JavaScript ever reach to Infinity in runtime?

It is possible at run time to get a number which is the result of a computation and which has for value Infinity. Nina Scholz has shown one such case: if you do x = 1 / 0, x will have for value Infinity.

What would really happen when a number grows beyond that range [i.e beyond the range JavaScript can handle]? Would JavaScript refer it as a new Infinity number?

We can try it. Number.MAX_VALUE is the maximum floating point number that JavaScript can represent. If you run this:

Number.MAX_VALUE + 1

You get a big number but not Infinity. What's going on there? Hmm, on a hunch let's try this:

Number.MAX_VALUE + 1 === Number.MAX_VALUE

The result is true. Say yhat? The problem is that floating point numbers have a limited precision, when I add 1 to Number.MAX_VALUEthere isn't enough precision to register the increment.

If you try this:

Number.MAX_VALUE * 2

Then you get Infinity.

What are all the scenarios in JavaScript, where the value Infinity could be assigned to a variable in runtime?

"all the scenarios"... hmm... There are multiple issues with producing an enumeration of all the scenarios. For one thing, it is not clear what criteria should distinguish one scenario from one another. Is -Math.log(0) a different scenario from 1 / 0. If so, why? Then there's the issue that JavaScript engines have quite a bit of leeway to implement math functions. For instance, Math.tan is specified like this in the current draft:

Math.tan(x)

Returns an implementation-dependent approximation to the tangent of x. The argument is expressed in radians.

If x is NaN, the result is NaN.

If x is +0, the result is +0.

If x is -0, the result is -0.

If x is +∞ or -∞, the result is NaN.

It does not mandate a value for Math.tan(Math.PI / 2) If you recall your trigonometry classes, pi / 2 is 90 degrees and at that angle the tangent is infinite. Various versions of v8 have returned Infinity or a very large positive number. (See this question.) The specification does not mandate one result over the other: implementations are free to choose.

So practically if you start with a set of cases that you know mathematically should produce Infinity, you don't know whether they will actually produce that until you try them.


The part of your question with the incrementNumToInfinity function is not completely clear to me. You seem to be asking whether you can reach infinity simply by incrementing a number. It depends on what you mean. If you mean this:

let x = 0;
while (x !== Infinity) {
  x++;
}

This will never terminate. x won't ever reach beyond Number.MAX_SAFE_INTEGER + 1. So it won't reach Infinity. Try this:

let x = Number.MAX_SAFE_INTEGER + 1;
x === x + 1;

You'll get the result true. That's again running into precision problems. The increment of 1 is not big enough to make a difference within the precision available to you.

Changing the increment to 2, 5, 10 or 10000000 does not really fix the issue, it just changes how far you can go before your increment no longer makes any difference.

Solution 2:

Can a number in JavaScript ever reach to Infinity in runtime?

Assume your program does not have memory leak. I believe it can reach Infinity.

console.log(Number.MAX_SAFE_INTEGER)
// 9007199254740991console.log(Number.MAX_VALUE)
// 1.7976931348623157e+308var i = Number.MAX_SAFE_INTEGERwhile (i != Infinity) {
  i += Math.pow(10, 307)
  console.log(i)
}
// 1.0000000000000005e+307// 2.000000000000001e+307// 3.0000000000000013e+307// 4.000000000000002e+307// 5.000000000000002e+307// 6.000000000000003e+307// 7.000000000000003e+307// 8.000000000000004e+307// 9.000000000000004e+307// 1.0000000000000004e+308// 1.1000000000000004e+308// 1.2000000000000003e+308// 1.3000000000000003e+308// 1.4000000000000003e+308// 1.5000000000000002e+308// 1.6000000000000002e+308// 1.7000000000000001e+308// Infinity

Solution 3:

The ratio of the square root of a square multiplied by PI of the same square subtracting PI to account for infinite decay as it approaches infinity, equals infinity. Or proving Archimedes wrong and right at the same time. PI and square are equivalent because neither will ever reach 0. This phenomenon also explains the zero boundary in the Pythagorean theory where A squared + B squared = c squared while approaching infinity.

Math.sqrt(1) / (Math.PI * ((Math.sqrt(1))) - Math.PI)

This is in result to the Fox and Duck Riddle. As the duck moves 1r of the distance to the pond the fox moves 180deg or the sum equivalent of the squares of its opposing and adjacent angles, we are give the square 2^2 (the travel distance from the center of the pond) Square root PI to the given 1:4 ratio therefor the hypotonuse of the triangle over pi - pi = Infinity or a 1:1 relationship with opposing vectors at any specific point.

Solution 4:

ad 2:

What are all the scenarios in JavaScript, where the value Infinity could be assigned to a variable in runtime?

You could take a division with zero.

var x = 1 / 0;

console.log(x);

Post a Comment for "Can A Number In Javascript Ever Reach To Infinity In Runtime?"