算术运算符如何计算 JavaScript 中的无穷大值?

How arithmetic operators works for infinity value in JavaScript?

为什么算术运算符对 JavaScript 中的无穷值有不同的作用?

console.log(1.7976931348623157E+10308 + 1.7976931348623157E+10308)//Infinity
console.log(1.7976931348623157E+10308 * 1.7976931348623157E+10308)//Infinity
console.log(1.7976931348623157E+10308 - 1.7976931348623157E+10308)/NaN
console.log(1.7976931348623157E+10308 / 1.7976931348623157E+10308)/NaN

实际上1.7976931348623157E+10308是一个Infinity值。您可以在下面查看算术运算符如何返回无穷大值的结果。

console.log(Infinity + Infinity)//Infinity
console.log(Infinity * Infinity)//Infinity
console.log(Infinity - Infinity)/NaN
console.log(Infinity / Infinity)/NaN

所以我的问题是..

  • Why + and * operator returning Infinity while doing arithmetic operation in between Infinity Value?

  • Why - and / operator returning NaN while doing arithmetic operation in between Infinity Value?

因为规范明确表示应该这样做。

+:

The sum of two infinities of the same sign is the infinity of that sign.

*:

Multiplication of an infinity by an infinity results in an infinity. The sign is determined by the rule already stated above.

-:

The sum of two infinities of opposite sign is NaN.

/:

Division of an infinity by an infinity results in NaN.