gpt4 book ai didi

javascript - 无穷大 - 无穷大 = NaN?

转载 作者:数据小太阳 更新时间:2023-10-29 04:22:43 29 4
gpt4 key购买 nike

任何数字减去它本身应该是 0,对吗?

3 - 3 === 0

那为什么

Infinity - Infinity === NaN

因为 typeof Infinity'number':

最佳答案

我们知道,两个数字之间的差可以这样计算

a - b = a + (-b)

JavaScript 使用它来查找两个值之间的差异。引自 Applying the Additive Operators to Numbers section from ECMA 5.1 Specification ,

The - operator performs subtraction when applied to two operands of numeric type, producing the difference of its operands; the left operand is the minuend and the right operand is the subtrahend. Given numeric operands a and b, it is always the case that a–b produces the same result as a +(–b).

所以,当你这样做的时候

Infinity - Infinity

它被评价为

Infinity + (-Infinity)

在 JavaScript 中,它们都是不同的对象。引自 The Number Type section of ECMA 5.1 Specification ,

There are two other special values, called positive Infinity and negative Infinity. For brevity, these values are also referred to for expository purposes by the symbols +∞ and −∞, respectively. (Note that these two infinite Number values are produced by the program expressions +Infinity (or simply Infinity) and -Infinity.)

再次引用 Applying the Additive Operators to Numbers section from ECMA 5.1 Specification

  • If either operand is NaN, the result is NaN.
  • The sum of two infinities of opposite sign is NaN.
  • The sum of two infinities of the same sign is the infinity of that sign.
  • ...

这就是结果为 NaN 的原因。

关于javascript - 无穷大 - 无穷大 = NaN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27074158/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com