std::pow() is a general purpose function designed to accept any pair of floating point values. It performs costly calculations and should be considered a slow function. However, apparently, many nations abused it for squaring, so the implementation of pow() in the IBM Accurate Mathematical Library (which is used by glibc) was optimized for this particular case:
sysdeps / ieee754 / dbl-64 / e_pow.c :
double __ieee754_pow (double x, double y) { ... ... if (y == 1.0) return x; if (y == 2.0) return x * x; if (y == -1.0) return 1.0 / x; if (y == 0) return 1.0;
As you can see, the values ββof the exponent 0, 1, and -1 are also processed specially, but those are at least mathematically significant special cases, while squaring is just a statistically significant case that otherwise does not deserve special processing). EDIT : The exponent values 0 , 1 , 2 and -1 are the only ones that allow std::pow(x,n) to be expressed using (much faster) arithmetic operations without loss of precision. See this answer for more details. Thus, the exponential value of 2 is not only a statistically significant case. End edit
If you need a quick alternative to std::pow() for non-negative integer exponent values ββand don't care about a slight loss of precision, then
The boundary value of the indicator for choosing between the 1st and 2nd methods should be found using careful benchmarking.
source share