# Thread: How do I do this without the pow() function?

1. ## How do I do this without the pow() function?

First off let say thank you for even taking the time to read this post.

What I want to do is write a program that evalutes the powers given the base and the exponent without using the pow() function.

What I decided to do have the base multiplied to it's self x amount of times, x being exponent. It seems easy to do but I can't seem to get it right. Any help appreciated!

2. have a function take the appropriate parameters, set the return value equal to the base, and loop multiply it the number of exponent times...

3. I figured it out right before you posted. Thanks anyway!

4. If you do like float^integer then you can use my method for faster code (between 32 and 64 multiplies for a 32-bit exponent). Here it is:

Code:
```float powfl(float b, unsigned long x)
{
unsigned long bit = 1;
float total = 1.f;
float mult = b;
for(long c = 0; c < 32; c++)
{
if(x & bit)
{
total *= mult;
}
mult *= mult;
bit <<= 1;
}
}```
Did I do it right?

5. Hi
if the power factor is not an integer you may use exponents and logarithms to do powers

1. a = y^x
2. log(a) = log(y^x)
3. log(a) = x*log(y)
4. exp(log(a)) = exp(x*log(y))

or finaly:

5. a = exp(x*log(y))

where log is natural logarithm (with base 'e')

damyan