# Thread: What do i use to exponetiate something?

1. ## What do i use to exponetiate something?

I'm working on a program that I need to use this equation:
1/2^n where n is a for loop number going up to 8. what can i use instead of carrot (^) to square or exponetiate somthing?

Plz help,
Artanis

2. std::pow(double, double);

from <cmath>

i.e.

double result = std::pow(.5, n);

3. ## thanks

thanks, how could i have been so stupid, heh heh

4. well, you could also create you're own recursive function, but that isn't necessary... but i feel like it and i'm bored, so...

Code:
#include <iostream>
using namespace std;

double power(double,double);

int main()
{
double number;
double exp;

...

return 0;
}

double power(double number, double exp)
{
if(exp>1)
return number*power(number, exp-1);
else
return number;
}
there it is... although it only works for positive exponents...

Code:
else if(exp==0)
return 1;

6. that's why i said positive ;-)

7. oops i didnt see that sorry

8. ## Use the force... uh... I mean the library.

Although writing your own functions is the best exercise, and there's nothing more fun than creating a recursive function The built-in library functions are almost always better (bug-free, fool-proof, flexable, etc.)

And, a "simple" recursive function can't do this: pow(0.5, 2.4)... In fact, I don't know how to do that! Hmmm, I'd have to dig out my math books... must have something to do with summing exponents...

9. ## Re: Use the force... uh... I mean the library.

Originally posted by DougDbug
And, a "simple" recursive function can't do this: pow(0.5, 2.4)... In fact, I don't know how to do that! Hmmm, I'd have to dig out my math books... must have something to do with summing exponents...
Raise 0.5 to the 24th power, and take the 10th root (or the 12th power and take the 5th root, etc.). Use your home-built recursive function for the raising, but alas the only way I ever figured out how to get roots (other than the square root) is std::pow...

10. well recursive functions are fun to write... kinda like linked lists... or am i just strange?

11. I wonder how difficult it would be to make our own exp function with inline assembly...Arn't their ASM commands to do logarithms? (I don't have any x86 reference manuals, otherwise I would look in those)

12. Wow, you all went so far off from my question... Though these inquires are intriguing. I say thanks to Zach L. I didn't have time to make a recursive function, though it almost came to that!