gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:18:18 25 4
gpt4 key购买 nike

我有个案例想返回

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com