### pow function error

Hi everyone,

I am practice and I face one error about pow() this my code. Can you please explain why this happen?

 ``12345678910111213141516171819`` ``````#include #include #include using namespace std; int main() { float area; area = pow (3,2); cout << area ; system ("pause"); return 0; }``````

And it gives me this error.
 Error 1 error C2668: 'pow' : ambiguous call to overloaded function
closed account (Dy7SLyTq)
try int area
There is an error due to the base you entered (which in here is 3). To use pow function, the base of the power has to be double or float.

To solve the problem, change to:
`area = pow(3.0,2) ;`

You can refer to: http://www.cplusplus.com/reference/cmath/pow/
closed account (Dy7SLyTq)
ah did not no that. but makes sense with the error. if you would like to fix that:

 ``12345678910`` ``````template a pow(a base, int power) { auto value = base; for(int counter = 0; counter < power; counter++) value *= base; return value; }``````
Thank you all for your help.
DTSCode I tried it with int but did not work. But it works with the suggestion from
 osgwsy
Thank you so much.
closed account (Dy7SLyTq)
yeah i know i didnt realize that pow wont take or return an int. however my code above will take any int float or double and return int float or double
@DTSCode

I don't see the point in writing your own template function to do this, that is what `std::pow` does - no need to reinvent the wheel.

So osgwsy's solution is the best one.

Personally, I explicitly specify the zero after the decimal point for FP numbers - as in 3.0 but not 3 or 3. (no decimal point, or nothing after decimal point) Doing means the number will be a double by default and is easier to read IMO.

There is also `3.0f` to explicitly specify a float.

HTH
closed account (Dy7SLyTq)
i just did it because it doesnt do ints. it didnt take that long to write and the only issue it could have is making it auto and/or giving it that initial value of base instead of one
But version 4, in the link below, will take arguments of any combination of int or FP, casting the ints to double.

 http://en.cppreference.com/w/cpp/numeric/math/pow

Admittedly it is a C++11 thing - people are often not aware of some of the new things. I am only aware of a small fraction of them myself.

If the OP had C++11 enabled & called std::pow instead of pow, the code would have worked as is. No need to write any more code. But osgwsy's solution is by far the simplest.

I guess it is a good lesson to the OP to be aware of types in general & especially when calling functions.
closed account (Dy7SLyTq)
yes i was not aware of that. the link osgwy gave didnt have that
Other than as an exercise, it's not usually a good idea to re-invent the wheel. I don't remember the context, but I saw some other example recently where a library function had been re-written by someone.

Doing that is fine, as a study task. It can be a good way to learn. But the larger issue is that the library functions are thoroughly tested and well-documented. Their properties, including any limitations, are known.

Once one starts writing one's own versions of any function, then all the testing, debugging, optimising and documenting has to be done all over again. Although I'd do that with my own code, if the code came from some other source I'd go for the library version every time.
Topic archived. No new replies allowed.