为什么JavaScript无法正确计算9 ** 1/2?输出为4.5而不是3

发布于 2025-02-06 10:16:15 字数 183 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双手揣兜 2025-02-13 10:16:15

这是因为运营商的优先级。 **的优先级高于/,就像*具有更高优先级的方式相同。

您不会期望3 * 3 + 2等于15吗?

因此,您的9 ** 1/2 < / code>被解释为(9 ** 1) / 2 < / code>。如果您的意思是将9提高到0.5的功能,则需要将其澄清为:

9 ** (1/2)

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 raise 9 to the power of 0.5 you need to clarify it as:

9 ** (1/2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文