Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous pow() function

Tags:

c++

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

#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 it's 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).

like image 746
Dr Deo Avatar asked Jun 30 '10 18:06

Dr Deo


3 Answers

In the line:

v=pow(w,0.5);

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

like image 152
Cogwheel Avatar answered Oct 17 '22 04:10

Cogwheel


Math functions like pow(), sin() etc are templatized in more modern C++ implementations. The reason it is ambiguous is that it is unclear what you want to do. If you send in both arguments being the same, you presumably want the computation to be done at that specific precision. If they are different, then do you want to compute at the higher precision and upcast the lower precision operand, or do you want to downcast the higher precision to lower precision and then do the computation at lower precision. 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
like image 23
Andrew Selle Avatar answered Oct 17 '22 05:10

Andrew Selle


Try v=pow(w,0.5f);

like image 2
NG. Avatar answered Oct 17 '22 05:10

NG.