C++ standard doesn't have int pow(int, int)
(It has double pow(double, int)
, float ...
). Microsoft's cmath uses C math.h that has not ipow. Some cmath headers define template version of pow
.
$ cat main.cpp
#include <cmath>
int main() {
std::pow(2,2);
}
$ gcc main.cpp # this cmath has template pow
...snip... std::pow<int, int>(int, int)]+0x16): undefined reference to `pow'
collect2: ld returned 1 exit status
1 ;( user@host:
$ gcc main.cpp -lm
Search for function:ipow lang:c++ on Google Code .
Here's example from the first link:
template <typename Type1, typename Type2>
Type1 ipow(Type1 a, Type2 ex)
// Return a**ex
{
if ( 0==ex ) return 1;
else
{
Type1 z = a;
Type1 y = 1;
while ( 1 )
{
if ( ex & 1 ) y *= z;
ex /= 2;
if ( 0==ex ) break;
z *= z;
}
return y;
}
}
See calculating integer powers (squares, cubes, etc.) in C++ code.