# Thread: integral counterpart to pow()?

1. ## integral counterpart to pow()?

Hi,
I was wondering, in the standard libraries, is there an integral counterpart to the "double pow(double, double)" function? (for an integer raised to the power of a positive integer)

I am asking this because I want to use the pow function without having to worry about everything associated with floating points (precision etc).

Thank you very much.

2. I was wondering, in the standard libraries, is there an integral counterpart to the "double pow(double, double)" function? (for an integer raised to the power of a positive integer)
Unfortunately no, only versions for float, double and long double.

I am asking this because I want to use the pow function without having to worry about everything associated with floating points (precision etc).
Thankfully, it is much easier to write a version for an int raised to an int, so you could always write it yourself.

Thankfully, it is much easier to write a version for an int raised to an int, so you could always write it yourself.
Can you please enlighten me a bit on that? It doesn't sound too easy to me. Unless the most efficient way is to just loop through the exponent?

Thank you very much

4. Unless the most efficient way is to just loop through the exponent?
An efficient yet easy way (not sure if it is the most efficient) is to note that:
For a integers x, n and letting ^ denote exponentiation:
x^(2n) = x^n * x^n
x^(2n+1) = x^n * x^n * x

Consequently, one could write a recursive function that performs this calculation.

5. An efficient yet easy way (not sure if it is the most efficient) is to note that:
For a integers x, n and letting ^ denote exponentiation:
x^(2n) = x^n * x^n
x^(2n+1) = x^n * x^n * x
hmm I see... I will try to implement that.

6. It doesn't need to be recursive:
Code:
```	unsigned int pow(unsigned int a, unsigned int b) {
unsigned int result = 1;
while (b) {
if (b & 1)
result *= a;
b >>= 1;
a *= a;
}
return result;
}```