如何简化以下函数和对Math.pow()
的重复调用
function x(a, b, c) {
rv = Math.floor(a * Math.pow(2, b));
for (i = 1; i < c; i++) {
rv += Math.floor(a * Math.pow(1.2, b + i));
}
return rv;
}
发布于 2018-06-05 03:59:09
如果您担心在大范围内计算全部1.2^(b+i),请考虑底层可能会在编译器优化中从以前的结果中推断出结果。但是,如果你想明确地帮助他,你可以这样做
function x (a, b, c) {
var rv = Math.floor(a * Math.pow(2, b))
var multiplier = Math.pow(1.2, b + 1)
for (i = 1; i < c; i++) {
rv += Math.floor(a * multiplier);
multiplier *= 1.2
}
return rv;
}
只有数学。
https://stackoverflow.com/questions/50670772
复制相似问题