View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# how do u take a number to the power of

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

16 replies to this topic

### #1dragonsimoto  Members

Posted 16 February 2002 - 11:32 AM

equal = (num1^num2); i get zero for the answer when i do this with any number combos why dont it work? what is the correct way to do powers of? thanks, dragon

### #2Martee  Members

Posted 16 February 2002 - 11:36 AM

Try pow(num1, num2). Make sure to include math.h.

### #3dragonsimoto  Members

Posted 16 February 2002 - 11:38 AM

oh yeaah, thanks! that is it lol i forgot i remember reading that in a tutorial somewhere any way thanks.

### #4dragonsimoto  Members

Posted 16 February 2002 - 11:43 AM

now when i take the pow of some rather large powers, it gives me the same number but smaller pows work perfect. if u know this one let me know if not no big deal.

### #5krez  Members

Posted 16 February 2002 - 11:48 AM

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?

### #6dragonsimoto  Members

Posted 16 February 2002 - 11:54 AM

i used int for num1 num2 and equal
so i should use double now for equal right?

### #7dragonsimoto  Members

Posted 16 February 2002 - 11:55 AM

ok that worked great now it says e+ some variable which is what i wanted. thanks again.

### #8LOWORBIT  Members

Posted 16 February 2002 - 12:05 PM

you could always do a while loop, or even better, bitwise operators if your just doing a square, or square root

### #9Aphelion  Members

Posted 16 February 2002 - 06:00 PM

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

### #10dragonsimoto  Members

Posted 16 February 2002 - 06:38 PM

thanks alot u guys u all are great help

### #11matrix2113  Members

Posted 17 February 2002 - 06:53 AM

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

### #12Oluseyi  Members

Posted 17 February 2002 - 10:19 AM

Just because we can:

  template double exponentiate(float number){ return exponentiate(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!

### #13matrix2113  Members

Posted 17 February 2002 - 12:07 PM

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

### #14Beer Hunter  Members

Posted 17 February 2002 - 12:17 PM

Oluseyi - That looks disturbingly similar to prolog code...

### #15SabreMan  Members

Posted 17 February 2002 - 10:44 PM

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.

### #16Useless Hacker  Members

Posted 18 February 2002 - 01:21 AM

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)

### #17Beer Hunter  Members

Posted 18 February 2002 - 10:44 AM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.