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

时间:2015-08-03 12:41:27

标签: javascript

我有一个案例,我想要返回

y = 1/2^x

基本上

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

这是我提出的最好的

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

是否有更简单易行的方法?

1 个答案:

答案 0 :(得分:3)

来自a Wikibooks article on mathematics

  

负指数只是意味着先取基数的倒数(一个数),然后应用指数。

因此,您可以将倒数移到Math.pow来电:

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