This is a discussion on Math Header? within the C++ Programming forums, part of the General Programming Boards category; Couldn't you just write a short recursive function to handle "power of" math calls. Code: long PowerOf(long Base_Number, long Power_Of) ...

1. Couldn't you just write a short recursive function to handle "power of" math calls.

Code:
```long PowerOf(long Base_Number, long Power_Of)
{
long tmp=0;
static long counter;
while(Power_Of<=counter)
{
counter++;
tmp=Base_Number*(counter);
Power_Of--;
tmp=PowerOf(tmp,Power_Of);;
};
return tmp;
}```
(I know this code has flaws)

2. >>I needed it for a think called HEK
Another .NET sellout. Did you know I couldn't even install my HP printer drivers on my older computer because it didn't have .NET installed?

**EDIT**
>>Couldn't you just write a short recursive function to handle "power of" math calls.
a) Why rewrite the standard math library?
b) How would you compute fractional powers by this method?

3. Couldn't you just do a bit of code modification and change out the "long" parameters for "float"?

Also, if you aren't interested in the bloat of including more headers files this should help. Or for the novelty, of I don't know, writing something yourself...

I wasn't suggesting it was the be all/end all. Just an alternative. An idea.

4. long PowerOf(long Base_Number, long Power_Of)
{
long tmp=0;
static long counter;
while(Power_Of<=counter)
{
counter++;
tmp=Base_Number*(counter);
Power_Of--;
tmp=PowerOf(tmp,Power_Of);;
};
return tmp;
}

don't know what that would do...

5. >>Couldn't you just do a bit of code modification and change out the "long" parameters for "float"?
No. Because the power-ing is done by whole numbers. An interesting idea though, to do it with recursion... I would have just stuck it in a for loop

6. using recursion in the place of a simple for loop is not interesting, its slow and clumsy.

7. You know I didn't even think of putting it in a loop...

8. Recursive power function:

if 'base' is zero, then 'exp' must be positive
Code:
```
double power( double base, int exp)
{
if (base == 0)  //base is zero, and exp should be positive

return 0;

else if (exp == 0)

return 1;

else if (n > 0)

return base * power(base, exp - 1);

else //base is nonzero, and exp is negative

return  1/power(base, -exp);

}```

9. so can you explain what this does and how to use it if it will help me please?

10. >>so can you explain what this does and how to use it if it will help me please?
It is a recursive function: It calls itself with changing parameters in order to accomplish the task. And it isn't useful to you at all, since <cmath> includes a pow() function

11. oh lol yes I have already used the pow function for my calculator.

12. Originally Posted by Hunter2
>>so can you explain what this does and how to use it if it will help me please?
It is a recursive function: It calls itself with changing parameters in order to accomplish the task. And it isn't useful to you at all, since <cmath> includes a pow() function

sometimes i get joy out of bludgeoning a fly with a hammer

Page 2 of 2 First 12