how do u take a number to the power of

Started by
15 comments, last by dragonsimoto 22 years, 1 month ago
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
"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
Advertisement
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!
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
"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
Oluseyi - That looks disturbingly similar to prolog code...
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.
quote:Original post by matrix2113 / Oluseyi
[Edit: It''s no fun having to scroll several pages to the right to read a post]


I still do (800x600)
[ PGD - The Home of Pascal Game Development! ] [ Help GameDev.net fight cancer ]
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.

This topic is closed to new replies.

Advertisement