javascript - 在 javascript 中简化 1/(Math.pow(2, y)?

标签 javascript

我有个案例想返回

y = 1/2^x

基本上

1
1/2
1/4
1/8
...

这是我想出的最好的

function halfing (depth){
    return 1/(Math.pow(2, (depth) ))
}

是否有更简单易懂的方法来做到这一点?

最佳答案

来自 a Wikibooks article on mathematics :

A negative exponent simply means you take the reciprocal (one over the number) of the base first, then apply the exponent.

因此您可以将倒数移动到 Math.pow 调用中:

function halfing(depth) { 
   Math.pow(2,-depth);
}

关于javascript - 在 javascript 中简化 1/(Math.pow(2, y)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31787564/

相关文章:

javascript - Rally javascript 不显示任何内容,查询存在问题

javascript - 用于 HTML 模板的 Angular JS

javascript - 我正在尝试使用 useEffect react Hook 从服务器获取数据

javascript - 隐藏浏览器的地址栏(弹出)

javascript - 检查复选框是否被选中时出现问题

javascript - 将逗号分隔的 json 对象列表转换为数组

javascript - 从 onChange 处理程序调用时使用错误的字段名称提交 HTML 表单

javascript - 使用 useState 清除字段

javascript - 如何重构这个 for 循环?检查标签 id 是否与数组中的标签匹配,然后更新其边框

javascript - 使用主干时什么类型的功能在哪里?