# pow()

This topic is 4622 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
Cast the parameters to double / float?

##### Share on other sites
What do you mean?
Thanks.

##### Share on other sites
pow(2.0f, 10); //like thispow(static_cast <double> (2), 10); //or maybe this

##### 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 on other sites
Quote:
 Original post by b2b3If 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 on other sites
Yes I think for just a few calls for pow it's a bit too much to introduce a new function to do it.

##### Share on other sites
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 on other sites
Quote:
 Original post by SpoonbenderBut 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.

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 10
• 11
• 14
• 10
• 25
• ### Forum Statistics

• Total Topics
632649
• Total Posts
3007644
• ### Who's Online (See full list)

There are no registered users currently online

×