pow()

Started by
8 comments, last by b2b3 18 years, 2 months ago
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!
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Advertisement
Cast the parameters to double / float?
What do you mean?
Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
pow(2.0f, 10); //like thispow(static_cast <double> (2), 10); //or maybe this
Bah forgot to log in, anyway that should work ^.
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.
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]
Yes I think for just a few calls for pow it's a bit too much to introduce a new function to do it.
You could just overload the function so that it takes two ints. This way, you'd just need to perform casts inside the function.
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.

This topic is closed to new replies.

Advertisement