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

孤街浪徒 提交于 2019-12-11 17:34:26

问题


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 is about 18 Quintilian (an 18 with 18 zeros after it). That’s a lot.

Now, I've few questions here:

  1. What would really happen when a number grows beyond that range? Would JavaScript refer it as a new Infinity number?
  2. What are all the scenarios in JavaScript, where the value Infinity could be assigned to a variable in runtime?

Let's look at a code example,

Attempting to write a method incrementNumToInfinity() to increment value of a certain number of times, so that a === b can evaluate to be true (also, to look at other possible scenarios, where the JavaScript Engine could assign the value Infinity to a variable in runtime).

var a = 1000; // a positive number
var b = Infinity;
console.log(a === b); // It returns false, that's expected

function incrementNumToInfinity(num) {
    // Logic to convert our variable num into Infinity
    return num;
};

a = incrementNumToInfinity(a); // Input: 1000, Expected output: Infinity
console.log(a === b); // Should return true

回答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_VALUE there 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.




回答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)
// 9007199254740991
console.log(Number.MAX_VALUE)
// 1.7976931348623157e+308

var i = Number.MAX_SAFE_INTEGER
while (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



回答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.




回答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);


来源:https://stackoverflow.com/questions/55949608/can-a-number-in-javascript-ever-reach-to-infinity-in-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!