JavaScript 中的数字可以在运行时达到无穷大吗? [英] Can a number in JavaScript ever reach to Infinity in runtime?

查看:36
本文介绍了JavaScript 中的数字可以在运行时达到无穷大吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是好奇,JavaScript 中的数字是否可以达到 Infinity.

I was just curious, whether a number in JavaScript can ever reach Infinity.

JavaScript 数字的范围非常好 -- 2 的 64 次方 不同数字,大约是 18 Quintilian(18 后有 18 个零).好多啊.

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.

现在,我有几个问题:

  1. 当一个数字超出这个范围时,真正会发生什么?JavaScript 会将其称为新的 Infinity 数字吗?
  2. JavaScript 中有哪些场景可以在运行时将值 Infinity 分配给变量?
  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?

<小时>

我们来看一个代码示例,

尝试编写一个方法 incrementNumToInfinity() 将值递增一定次数,以便 a === b 可以评估为 true(另外,看看其他可能的场景,其中 JavaScript 引擎可以在运行时将值 Infinity 分配给变量).


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

推荐答案

JavaScript 中的数字能否在运行时达到无穷大?

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

有可能在运行时得到一个数字,它是计算的结果,其值为 Infinity.Nina Scholz 有展示一个这样的例子:如果你做x = 1/0x 的值为 Infinity.

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.

当一个数字增长到超出该范围(即超出 JavaScript 可以处理的范围)时,真正会发生什么?JavaScript 会将其称为新的 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?

我们可以试试.Number.MAX_VALUE 是 JavaScript 可以表示的最大浮点数.如果你运行这个:

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 

你得到一个很大的数字,但不是Infinity.那里发生什么事了?嗯,凭直觉让我们试试这个:

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

结果是 true.怎么说?问题是浮点数的精度有限,当我向 Number.MAX_VALUE 加 1 时 没有足够的精度来注册增量.

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.

如果你试试这个:

Number.MAX_VALUE * 2

然后你得到Infinity.

JavaScript 中有哪些场景可以在运行时将值 Infinity 分配给变量?

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

所有场景"……嗯……生成所有场景的枚举存在多个问题.一方面,不清楚应该用什么标准来区分一种情况.-Math.log(0) 是否与 1/0 不同.如果是这样,为什么?然后是 JavaScript 引擎在实现数学函数方面有相当多的余地的问题.例如,Math.tan当前草稿:

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

返回 x 的切线的依赖于​​实现的近似值.参数以弧度表示.

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

如果xNaN,结果为NaN.

如果x+0,则结果为+0.

如果x-0,则结果为-0.

如果x+∞-∞,结果为NaN.

它不强制Math.tan(Math.PI/2) 的值 如果你回忆一下你的三角学类,pi/2 是 90 度角度切线无穷大.v8 的各种版本都返回了 Infinity 或一个非常大的正数.(参见 这个问题.)规范不强制要求一个结果高于另一个:实现可以自由选择.

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.

所以实际上,如果你从一组你知道数学上应该产生Infinity的情况开始,你不知道它们是否真正产生,直到你尝试它们.

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.

您关于 incrementNumToInfinity 函数的那部分问题对我来说并不完全清楚.您似乎在问是否可以通过增加一个数字来达到无穷大.这取决于你的意思.如果你是这个意思:

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++;
}

这永远不会终止.x 永远不会超过 Number.MAX_SAFE_INTEGER + 1.所以它不会达到Infinity.试试这个:

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;

你会得到结果true.这又遇到了精度问题.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.

将增量更改为 2、5、10 或 10000000 并不能真正解决问题,它只是改变了在增量不再产生任何影响之前您可以走多远.

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.

这篇关于JavaScript 中的数字可以在运行时达到无穷大吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆