Archived

This topic is now archived and is closed to further replies.

how do u take a number to the power of

This topic is 5775 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

perhaps the resulting big-ass number doesn''t fit in your data type? i think pow() returns a double, but are you putting it in an int or something?

Share this post


Link to post
Share on other sites
You could create your own function for a little coding practice...
Example:

float ComputeExponent(float base, float exponent)
{
int index;
float endresult = base;
for(index = 0; index < exponent - 1; index++)
endresult *= base;

return endresult;
}

Cheers,
-Jesse

| The Hitchhiker''''s Guide to Programming |
"That is not dead which can eternal lie,
And with strange aeons even death may die."
-H.P. Lovecraft

Share this post


Link to post
Share on other sites
Just for practice, I think I'll write a little recursiev power function here:
      
double power(float number, float exponent) {
if (exponent == 0)
return 1;//Power of 0 is always 1


if (exponent == 1)
return number;//Power of 1 is the number itself


if (exponent < 0)
return (1 / power(number,(-1 * exponent)));//power less than 0 makes it go to bottom of fraction (reciprecal))


if (exponent > 0 && exponent < 1)
/*Um....fractional exponents...fractional exponents.....
12 ^ .25 is the same as the 4th root of 12....I don't know of any
to the xth root function...if you know (lets say for this example we'll
call it roots(float number, int toTheXthRoot), take the exponent, turn
it into a fraction (you'll have to find/make a function for that,
too), the top part of the fraction being the divisor, the bottom
the denominator, you'll have to return roots(power(number, devisor),
denominator);*/


return (number * power(number, exponent - 1);//12^3 == 12 * 12^2 == 12 * 12 * 12

}


[Edit: It's no fun having to scroll several pages to the right to read a post]

[Edit: Let me have my fun..... ]

"I've learned something today: It doesn't matter if you're white, or if you're black...the only color that REALLY matters is green"
-Peter Griffin

Edited by - Oluseyi on February 17, 2002 5:09:50 PM

Edited by - matrix2113 on February 18, 2002 10:39:41 PM

Share this post


Link to post
Share on other sites
Just because we can:

  
template <int power> double exponentiate(float number)
{
return exponentiate<power - 1>(number);
}
.
template double <2> exponentiate(float number)
{
return (number * number);
}
.
template double <1> exponentiate(float number)
{
return number;
}


The beauty of template metaprogramming! In this case the compiler generates the recursive code and end cases based on the explicit instantiation for parameters 1 and 2.

I wanna work for Microsoft!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
That only works for Positive whole numbers, mine works for all (well, if I finished it it would have... )

"I''ve learned something today: It doesn''t matter if you''re white, or if you''re black...the only color that REALLY matters is green"
-Peter Griffin

Share this post


Link to post
Share on other sites
quote:
Original post by Beer Hunter
Oluseyi - That looks disturbingly similar to prolog code...



It looks exactly like template metaprogramming to me. If you haven''t seen this style before, then get used to it, as you''ll be seeing lots more of it in the future!



--
Very simple ideas lie within the reach only of complex minds.

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
It looks exactly like template metaprogramming to me.



template double <1> exponentiate(float number) {
return number;
}
template double <2> exponentiate(float number) {
return (number * number);
}
template double exponentiate(float number) {
return exponentiate(number);
}


Power(Number, 1, Result):-
Result = Number.

Power(Number, 2, Result):-
Result = Number * Number.

Power(Number, X, Result):-
Power(Number, X-1, T),
Result = T * Number.

That''s more or less how most prolog programmers would implement it. Do you see the similarities?

quote:
Original post by SabreMan
If you haven''t seen this style before, then get used to it, as you''ll be seeing lots more of it in the future!


I made a point of how that style is very common in prolog, but you seem to believe that I''ve never seen that style before.

Share this post


Link to post
Share on other sites