简体   繁体   English

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

[英]Simplify 1/(Math.pow(2, y) in javascript?

I have a case where I want to return 我有一个案子我想回来

y = 1/2^x

basically 基本上

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

This is the best I've come up with 这是我提出的最好的

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

Is there a simpler and easier to follow way of doing this? 这样做有更简单,更容易的方法吗?

From a Wikibooks article on mathematics : 来自Wikibooks的数学文章

A negative exponent simply means you take the reciprocal (one over the number) of the base first, then apply the exponent. 负指数只是意味着先取基数的倒数(一个数),然后应用指数。

So you can move the reciprocal into the Math.pow call: 因此,您可以将倒数移动到Math.pow调用中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM