[英]GNU Scientific Library, Power function efficiency
I was going through GSL library. 我正在浏览GSL库。 I am pasting the function they used for finding the power of a double number.
我正在粘贴他们用于查找双数幂的函数。
double gsl_pow_int(double x, int n)
{
double value = 1.0;
if(n < 0) {
x = 1.0/x;
n = -n;
}
/* repeated squaring method
* returns 0.0^0 = 1.0, so continuous in x
*/
do {
if(n & 1) value *= x; /* for n odd */
n >>= 1;
x *= x;
} while (n);
return value;
}
But wouldn't it be more efficient if they use? 但是,如果使用它们,会不会更有效率?
double gsl_pow_int(double x, int n)
{
double value = 1.0;
if(n < 0) {
x = 1.0/x;
n = -n;
}
/* repeated squaring method
* returns 0.0^0 = 1.0, so continuous in x
*/
do{
if(--n)value*=x;
}while(n);
return value;
}
Your code doesn't even properly handle negative powers! 您的代码甚至无法正确处理负功率! How can you claim that your code is optimised.
您如何声称您的代码已优化。
Also,next,just decreasing space from your program doesn't make your code more-optimised.Their code has got more readability and more proper indentation than yours!!!
此外,接下来,仅减少程序空间就不会使您的代码更优化。与您的代码相比,它们的代码具有更高的可读性和更合适的缩进!!! Their code is proper for negative powers too and much more optimised!
他们的代码也适用于负功率,并且更加优化!
Also,next, bitwise logical operations like &
and right shifting >>
is considered more efficient than multiplying as what you have done. 另外,接下来,按
&
以及右移>>
类的按位逻辑运算被认为比您做的乘法要高效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.