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

标签 javascript typescript types ecmascript-6 operators

为什么算术运算符在 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.

关于javascript - 算术运算符如何计算 JavaScript 中的无穷大值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49817773/

相关文章:

vba - 跨各种模块管理用户定义的类型

python - 更改类型(pandas dataframe)时如何处理 'invalid literal for int() with base 10'?

c++ - 在 C++ 中使用 size_t 是一种好习惯吗?

javascript - 强制 Google Chrome 'kill page' 弹出窗口的方法?

Angular Material Datepicker - nextClicked,previousClicked 不发射

Angular 2 无法在 XMLHttpRequest : Invalid URL 上执行打开

http - Angular 2 - 可观察和异步 http 加载

javascript 函数未定义,但正在加载我的脚本

javascript - 使用 JavaScript 禁用 HTML 所需输入 onchange 单选按钮

javascript - 如何在不覆盖值的情况下合并javascript中的对象