为什么JavaScript无法正确计算9 ** 1/2?输出为4.5而不是3
console.log(Math.pow(9, 1 / 2) === 9 ** 1 / 2) // false
console.log(9 ** 1 / 2) // 4.5
console.log(Math.pow(9, 1 / 2) === 9 ** 1 / 2) // false
console.log(9 ** 1 / 2) // 4.5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为运营商的优先级。
**
的优先级高于/
,就像*
具有更高优先级的方式相同。您不会期望
3 * 3 + 2
等于15吗?因此,您的
9 ** 1/2 < / code>被解释为
(9 ** 1) / 2 < / code>。如果您的意思是将
9
提高到0.5
的功能,则需要将其澄清为:It's because of operator precedence.
**
has a higher precedence than/
in the same way that*
has higher precedence thatn+
.You wouldn't expect
3 * 3 + 2
to equal 15 do you?So similarly your
9**1 / 2
is interpreted as(9**1) / 2
. If you mean to raise9
to the power of0.5
you need to clarify it as: