Jump to content
  • Advertisement
Sign in to follow this  
daniel_i_l

pow()

This topic is 4702 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

In my progar I'm using std and when I try to use pow(int, int_variable) with two ints I get an error: 131 C:\Dev-Cpp\Examples\tic\CMineSweeper.cpp call of overloaded `pow(int, int&)' is ambiguous why? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster

pow(2.0f, 10); //like this
pow(static_cast <double> (2), 10); //or maybe this

Share this post


Link to post
Share on other sites
If you use only integers and don't need very big numbers, you can write power much faster than the pow function.
For example try this:

long long int_pow(int x, unsigned int exponent)
{
long long temp = x;
long long res = exponent & 1 ? x : 1;

exponent >>= 1;

while (exponent != 0)
{
temp *= temp;

if (exponent & 1)
{
res *= temp;
}

exponent >>= 1;
}

return res;
}

This is 64-bit version which is relatively slow. If you know that you won't be needing numbers bigger than 231, you should change return type and types of temporaries in the function to the long. It should speed it up on any 32 bit machine since it will not call 64-bit multiplication which is emulated on 32 bit machines.

Share this post


Link to post
Share on other sites
Quote:
Original post by b2b3
If you use only integers and don't need very big numbers, you can write power much faster than the pow function.
For example try this:

long long int_pow(int x, unsigned int exponent)
{
long long temp = x;
long long res = exponent & 1 ? x : 1;

exponent >>= 1;

while (exponent != 0)
{
temp *= temp;

if (exponent & 1)
{
res *= temp;
}

exponent >>= 1;
}

return res;
}

This is 64-bit version which is relatively slow. If you know that you won't be needing numbers bigger than 231, you should change return type and types of temporaries in the function to the long. It should speed it up on any 32 bit machine since it will not call 64-bit multiplication which is emulated on 32 bit machines.


But how useful is it to someone who's simply asking how to use pow()?
I must have missed the part about "help, I've profiled my program, and it turns out the pow() function is my main bottleneck. Help me optimize it" [wink]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You could just overload the function so that it takes two ints. This way, you'd just need to perform casts inside the function.

Share this post


Link to post
Share on other sites
Quote:
Original post by Spoonbender
But how useful is it to someone who's simply asking how to use pow()?
I must have missed the part about "help, I've profiled my program, and it turns out the pow() function is my main bottleneck. Help me optimize it" [wink]


I didn't mean to push him into optimizing his program (alhough from my post it may sound like that [smile]). That would be really unnecessary in most cases.
For me it just looks little weird to use floating-point pow when working with integers [wink]. You may get incorrect results in some cases. With doubles it is not that big problem, since you have 52 bits for mantissa, but for floats it may be problem if he needs bigger numbers.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!