Sign in to follow this  

pow()

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

This topic is 4336 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this