How do you use an exponent in c++ with a variable?

32,131

Solution 1

The compiler doesn't know which pow() function to call. The overloads listed here gives the following list:

      float pow (       float base,       float exponent );
     double pow (      double base,      double exponent );
long double pow ( long double base, long double exponent );
      float pow (       float base,         int 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.

Solution 2

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)

Solution 3

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) ;

Solution 4

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.

Share:
32,131
Soully
Author by

Soully

Am majoring in Computer Science, emphasizing on programming. I really enjoy coding in visual basic, or what little of it I've used, but as I only have ever programmed in that and C++, I don't quite have a favorite yet. I just finished my general ed requirements, and have just recently started working on my major. I asked my first question on this site and within minutes did I have not one reply, but five! And each answer was very helpful, I instantly added this site to my bookmarks, as finding one as helpful as this is near impossible...save this site and use it! It's great for not only beginners, but veteran programmers alike!

Updated on August 28, 2020

Comments

  • Soully
    Soully almost 4 years

    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

    • paxdiablo
      paxdiablo about 15 years
      Strangely enough, this works as-is with g++. Maybe it's an extension.
    • Soully
      Soully about 15 years
      I have a friend I'm talking with at the moment who uses g++ and linux, works fine for him as well...yes, yes! I know! Linux is better, but I can't game as heavily on linux, so I'm stuck with windows.
  • Soully
    Soully about 15 years
    completely forgot the needed asterisk...but have a semicolon in the actual code...didn't help though
  • paxdiablo
    paxdiablo about 15 years
    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.
  • Soully
    Soully about 15 years
    the conversion from an int to a double won't hurt anything?
  • lc.
    lc. about 15 years
    Yeah, you can get away without casting num.
  • David Rodríguez - dribeas
    David Rodríguez - dribeas about 15 years
    @Jeff: maybe efficiency. It is probably faster to implement 2^5 than 2^5.0.
  • David Rodríguez - dribeas
    David Rodríguez - dribeas about 15 years
    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.
  • MSalters
    MSalters about 15 years
    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) ? xrootroot : root*root;