我有个案例想返回
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/