当我的Math.log
值变为零时,它的值为-Infinity
。为什么给出负无穷大值?为什么只给出-Infinity而不是给出+ Infinity。为什么只有0值才能给出此结果。
console.log(Math.log(0))
答案 0 :(得分:3)
从根本上讲,因为that's how it's specified:
20.2.2.20
Math.log ( x )
返回x的自然对数的依赖于实现的近似值。
- 如果x为NaN,则结果为NaN。
- 如果x小于0,则结果为NaN。
- 如果x为+0或-0,则结果为-∞。
- 如果x为1,则结果为+0。
- 如果x为+∞,则结果为+∞。
(我的重点)
更重要的是,log(x)
接近x
decreases without bound时0
。因此-Infinity似乎是规范制定的合理选择。
答案 1 :(得分:-2)
因为未定义日志0,所以他们已采用这种方式实现。
console.log(Infinity); / *无限* /
console.log(Infinity + 1); / *无限* /
console.log(Math.pow(10,1000)); / *无穷大* /
console.log(Math.log(0)); / *-无穷大* /
console.log(1 /无穷大); / * 0 * /
规格:https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Infinity#Beispiele