Skip to main content
GameDev.net gamedev.net
🔒 Locked

int pow not float?

Started by frogtag Oct 3, 2010 at 6:39 AM 7 replies 2k views
Original Post
frogtag
frogtag
can anyone point me in the right direction for a power of function that uses ints not floats/doubles?

eg

2 ^ 3

not

2.0 ^ 3.0

Thanks
implicit
implicit
Any particular reason why the regular floating-point power function with arguments converted to/from integers won't do?

At any rate an integer implementation usually looks something like this:
int ipow(int base, unsigned int exp) {	int result = 1;	do {		if(exp & 1)			result *= base;		base *= base;	} while(exp >>= 1);	return result;}
frogtag
frogtag
honestly I think I have OCD or something like it. I don't like floats because the calculations I'm using are ints and I don't like having to use the .0

But anyhow, cheers for the function.
Anntor
Anntor
int power(int base, unsigned int exponent) {   int result = 1;   while (exponent--) result *= base;   return result;}


edit: oops, opened the tab and waited with my answer for to long (was reading another tab first). Now it was answered already.
frogtag
frogtag
Cheers. Although the while loop was destructive to the div value, with a little alteration I removed the function and achieved the same result keeping div intact.

for(int x=div; x > 0; x--)  array_max *= 2;
Ezbez
Ezbez
Do note that in many programming languages (specifically C-like ones such as C, C++, and Java), x ^ y doesn't do exponentiation, instead it does a bit-wise exclusive or.
rip-off
rip-off
Quote:
Original post by frogtag
Cheers. Although the while loop was destructive to the div value, with a little alteration I removed the function and achieved the same result keeping div intact.

*** Source Snippet Removed ***


It was implemented as a function for a reason. Putting the same code inline is a poor decision:

  • A named function is clearer in intent. If I am reading your code I'm not immediately aware of what the code is trying to do.

  • The code will be duplicated anywhere you need integer powers. This makes it harder to fix bugs and also makes your project larger.

  • The compiler will trivially inline such a function, so that resulting assembly will be equally efficient.


Finally, for powers of two you might choose to skip the loop and just use the shift operators.
Steve132
Steve132
Implicit's solution is the best. It runs ins O(lg e) time where e is the exponent. The other solutions in this thread are O(e). However, if your base integer is a power of two with p as the power, then you can compute it like this:

I^e = (2^p)^e = 2^(p*e) = (1 << p*e)

so, if you want to raise 8 to the 5th power, then 2^p=8, so p=3, e=5, then 8^5 = (1 << p*e) == (1 << (3)(5)) == (1 << 15) = 2^(15) = 32768.
iMalc
iMalc
Quote:
Original post by frogtag
Cheers. Although the while loop was destructive to the div value, with a little alteration I removed the function and achieved the same result keeping div intact.

*** Source Snippet Removed ***
You've hard-coded that to powers of two. I.e simply calculates array_max = array_max * 2^div (where ^ denotes to-the-power-of)
You don't even need a loop for that. Using bit-shifting, all you need to calculate the same thing is just:
array_max = array_max << div;

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.