javascript - 是否有 Math.pow(x, 0) 不为 1 的 `x` 值?

标签 javascript node.js

我在 previous question 中找到了Math.pow(0, 0) === 1 返回 true

the documentation我们发现 x^y 的以下规则:

  • If y is NaN, the result is NaN.
  • If y is +0, the result is 1, even if x is NaN.
  • If y is −0, the result is 1, even if x is NaN.
  • If x is NaN and y is nonzero, the result is NaN.
  • If abs(x)>1 and y is +∞, the result is +∞.
  • If abs(x)>1 and y is −∞, the result is +0.
  • If abs(x)==1 and y is +∞, the result is NaN.
  • If abs(x)==1 and y is −∞, the result is NaN.
  • If abs(x)<1 and y is +∞, the result is +0.
  • If abs(x)<1 and y is −∞, the result is +∞.
  • If x is +∞ and y>0, the result is +∞.
  • If x is +∞ and y<0, the result is +0.
  • If x is −∞ and y>0 and y is an odd integer, the result is −∞.
  • If x is −∞ and y>0 and y is not an odd integer, the result is +∞.
  • If x is −∞ and y<0 and y is an odd integer, the result is −0.
  • If x is −∞ and y<0 and y is not an odd integer, the result is +0.
  • If x is +0 and y>0, the result is +0.
  • If x is +0 and y<0, the result is +∞.
  • If x is −0 and y>0 and y is an odd integer, the result is −0.
  • If x is −0 and y>0 and y is not an odd integer, the result is +0.
  • If x is −0 and y<0 and y is an odd integer, the result is −∞.
  • If x is −0 and y<0 and y is not an odd integer, the result is +∞.
  • If x<0 and x is finite and y is finite and y is not an integer, the result is NaN.

有趣的是,对于 x 的任何值,返回的值为 1。我们能否找到 x 的任何值,因为 Math.pow(x, 0) 返回的值不是 1

我在 NodeJS shell 中尝试了以下操作,但我猜它在浏览器控制台中的结果是一样的:

> Math.pow(undefined, 0)
1
> Math.pow(Date(), 0)
1
> Math.pow("asd", 0)
1
> Math.pow(function () {}, 0)
1
> Math.pow(function () { return 3}, 0)
1
> Math.pow([], 0)
1
> Math.pow(null, 0)
1

也许我们找到了执行此操作的 JS 技巧,例如 x === x // false (where isNaN(x) === false)案例。


澄清一下:y 将始终为 0。只有 x 发生了变化。

最佳答案

您从文档中复制/粘贴的内容包括以下要求:

  • If y is +0, the result is 1, even if x is NaN

所以你的问题的答案似乎是“否”

关于javascript - 是否有 Math.pow(x, 0) 不为 1 的 `x` 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19976978/

相关文章:

javascript - 使用 stopPropagation 单击特定元素以外的任何地方

javascript - Jest 测试失败,出现意外 token ,预期为 ";"

javascript - 使用 expressjs 3 强制 SSL

javascript - Node Express Mongoose 数组未填充引用模型

javascript - HTML5 中的异步调用?

javascript - react 状态改变状态使代码从可见变为隐藏

javascript - 使用铯的最轻巧的方法是什么?

node.js - 尝试从 Nodejs 连接到 SQL Server Express

node.js - 连接mongodb时出错

Javascript:将链接文本更改为新文本填充或收缩文本以保持其与原始文本的大小相同