Ambiguous pow () function

I am trying to make a simple call to the pow() function from math.h someihing, like ..

 #include<math.h> int main() { float v,w; w=3.0; v=pow(w,0.5);//i think this is 'float pow(float,float)' return 0; } 

but visual studio says an error

 1>c:\users\user\documents\visual studio 2008\projects\deo\deo\main.cpp(7) : error C2666: 'pow' : 6 overloads have similar conversions 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\math.h(575): could be 'long double pow(long double,int)' 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\math.h(573): or 'long double pow(long double,long double)' 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\math.h(527): or 'float pow(float,int)' 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\math.h(525): or 'float pow(float,float)' 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\math.h(489): or 'double pow(double,int)' 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\math.h(123): or 'double pow(double,double)' 1> while trying to match the argument list '(float, double)' 

I thought I had the format float pow(float, float) .

+7
c ++
source share
6 answers

In line:

 v=pow(w,0.5); 

w is a float, and 0.5 is a double . You can use 0.5f .

+18
source share

Mathematical functions like pow (), sin (), etc. are templatized in more modern C ++ implementations. The reason for the ambiguity is that it is not clear what you want to do. If you send both arguments the same, you apparently want the calculations to be done with some precision. If they differ from each other, then you want to calculate with higher accuracy and increase the efficiency of the operand with lower accuracy, or you want to lower the higher accuracy to less accuracy, and then perform the calculations with less accuracy. i.e.

 float a,b; double c,d; pow(a,b); // not ambiguous, done at float precision pow(c,d); // not ambiguous, done at double precision pow(a,c); // ambiguous, gives error pow((double)a,c); // not ambiguous, performs computation at double precision pow(a,(float)c); // not ambiguous, gives computation at float precision, but c might lose precision in the down cast 
+4
source share

Try v=pow(w,0.5f);

+2
source share

0.5 is of type double. Try

 v=pow(w,0.5f); 
+2
source share

Hey, have you tried 0.5f?

+1
source share

In addition to all the other methods that have already been indicated in other answers, you can always explicitly specify a template argument:

 float w = 3.0f; double v = 1.5; v = pow<float>(w, v); 
0
source share

All Articles