Problem with pow function
Jul 12, 2010 at 11:40am UTC
Hey guys,
I've created my own version to use setprecision.
However, when I try to compile my code, it gives me this error message:
In function `float setprecision(float, int)':
error: call of overloaded `pow(int, int&)' is ambiguous
C:/Dev-Cpp/include/math.h:150: note: candidates are: double pow(double, double)
C:/Dev-Cpp/include/c++/3.4.2/cmath:361: note: long double std::pow(long double, int)
C:/Dev-Cpp/include/c++/3.4.2/cmath:357: note: float std::pow(float, int)
C:/Dev-Cpp/include/c++/3.4.2/cmath:353: note: double std::pow(double, int)
C:/Dev-Cpp/include/c++/3.4.2/cmath:349: note: long double std::pow(long double, long double)
C:/Dev-Cpp/include/c++/3.4.2/cmath:345: note: float std::pow(float, float)
error: call of overloaded `pow(int, int&)' is ambiguous
C:/Dev-Cpp/include/math.h:150: note: candidates are: double pow(double, double)
C:/Dev-Cpp/include/c++/3.4.2/cmath:361: note: long double std::pow(long double, int)
C:/Dev-Cpp/include/c++/3.4.2/cmath:357: note: float std::pow(float, int)
C:/Dev-Cpp/include/c++/3.4.2/cmath:353: note: double std::pow(double, int)
C:/Dev-Cpp/include/c++/3.4.2/cmath:349: note: long double std::pow(long double, long double)
C:/Dev-Cpp/include/c++/3.4.2/cmath:345: note: float std::pow(float, float)
Execution terminated
The function is:
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
#include <cmath>
#include <math.h>
float setprecision(float result, int round)
{
result=result*pow(10,round);
int y=(int ) result;
result=y/(pow(10,round));
return result;
}
I don't realy get this error, because I think this pow function is correct..
Could someone please explain whats wrong?
Thanks :)
Last edited on Jul 12, 2010 at 11:40am UTC
Jul 12, 2010 at 12:06pm UTC
Just read the error message... as you can see, there is no pow(int,int), so you need to choose one of the other ones.
Jul 13, 2010 at 10:10am UTC
Ok, so I changed the 10 into a double (double power = 10), but what would be the point of that?
Why can't you have a pow(int,int)?
Thanks!
Jul 13, 2010 at 10:35am UTC
It is because the pow function used here has no matching definition in std namespace. However you are not even using std so its strange that you get an error.
Because I could compile it without using namespace std, but get an error upon using it.
The program compiles fine with g++ 3.4.6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <cmath>
float setprecision(float result, int round)
{
result=result*pow(10,round);
int y=(int ) result;
result=y/(pow(10,round));
return result;
}
int main()
{
float x = setprecision(10.0, 2);
std::cout<<x<<std::endl;
return 0;
}
A good explanation is still needed.
Topic archived. No new replies allowed.