views:

3615

answers:

5

So I realize that #include is necessary, and that there is a pow(x,y) where x^y works...but when I attempted to use pow(2,(num-1)), it kicked back an error...

errorC2668: 'pow' : ambiguous call to overloaded function

the line of code I have for it is as follows

perfect = (pow(2,(num-1))) * (pow(2,num)-1);

Any recommendations?

Thanks in advance

EDIT:

num is indeed declared as an int.

num does have a value, starts at 1 and goes to UINT_MAX

Added an asterisk to equation

+1  A: 

Add missing multiplication (or what you want) and also there should be semicolon at the end of the line:

perfect = (pow(2,(num-1))) * (pow(2,num)-1) ;
completely forgot the needed asterisk...but have a semicolon in the actual code...didn't help though
Jeff
+4  A: 

The compiler doesn't know which pow() function to call. The overloads listed at http://www.cplusplus.com/reference/clibrary/cmath/pow.html gives the following list:

     double pow (      double base,      double exponent );
long double pow ( long double base, long double exponent );
      float pow (       float base,       float exponent );
     double pow (      double base,         int exponent );
long double pow ( long double base,         int exponent );

The compiler won't guess which one to use. Make it explicit with a casts.

perfect = (pow(2.,(double)(num-1))) < (pow(2.,(double)num)-1);

There may be some extra casts there, but they won't hurt anything.

MathStuf
I don't think it's the num since that will cause it to choose one of the last two. I think it's the 2 which should be explicitly cast to one of the datatypes in those last two.
paxdiablo
the conversion from an int to a double won't hurt anything?
Jeff
Yeah, you can get away without casting num.
lc
Gravy, thanks for the assist!
Jeff
@Jeff: maybe efficiency. It is probably faster to implement 2^5 than 2^5.0.
David Rodríguez - dribeas
Take that as x^int vs. x^double, not really 2 (which is a special number). The fact that there are the two versions seems to indicate that the implementation is different as in any other case ints would be automacally promoted to double with only the pow( X, double ) versions.
David Rodríguez - dribeas
pow(x,n) is trivial if n is integer. Basically:if (n<0) return 1/pow(x,-n); if (n==0) return 1; double root = pow(x,n/2); return (n%2) ? x*root*root : root*root;
MSalters
+2  A: 

These are the allowed pow() functions in C++. The problem is that your code has an int as the first argument and C++ doesn't know whether to promote it to a double or long double.

     double pow (      double base,      double exponent );
long double pow ( long double base, long double exponent );
      float pow (       float base,       float exponent );
     double pow (      double base,         int exponent );
long double pow ( long double base,         int exponent );

Try putting in (double)2 instead of just 2.

perfect = (pow((double)2,(num-1))) * (pow((double)2,num)-1)
paxdiablo
A: 

From error C2668: 'pow' : ambiguous call to overloaded function in VC++ 2005 only, alex.m wrote,

"pow" (any overload) takes a floating point type number (single, double or long double precision) as first argument, not an integer. This is where the error comes from, since the compiler can't guess the way you want your long integer to be converted.

Just try writing a cast expression, like this:

Code Block

c = pow((double)numberOfScansCompleted, 2);

So, if you try pow((double)2,(num-1)), it should work.


Amusing side note, as I typed the beginning of it into Google, "pow ambiguous call to overloaded function" came up as the top suggested search.

lc
or just pow(2.0, ...
derobert
yeah, that works too
lc
A: 

Remember, you can also write integer powers of 2 with a shift operator:

int perfect = (1<<(num-1)) * ((1<<num) - 1);

Or with ldexp (also included from <cmath>):

double perfect = ldexp(1.0, num-1) * (ldexp(1.0, num) - 1);
palm3D