为什么算术运算符在 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 returningInfinity
while doing arithmetic operation in betweenInfinity
Value?Why
-
and/
operator returningNaN
while doing arithmetic operation in betweenInfinity
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.
关于javascript - 算术运算符如何计算 JavaScript 中的无穷大值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49817773/