I feel like I must just be unable to find it. Is there any reason that the C++ pow function does not implement the \"power\" function for anything except
Here's a really simple O(log(n)) implementation of pow() that works for any numeric types, including integers:
template
static constexpr inline T pown(T x, unsigned p) {
T result = 1;
while (p) {
if (p & 0x1) {
result *= x;
}
x *= x;
p >>= 1;
}
return result;
}
It's better than enigmaticPhysicist's O(log(n)) implementation because it doesn't use recursion.
It's also almost always faster than his linear implementation (as long as p > ~3) because: