Ambiguous pow() function

In the line:

v=pow(w,0.5);

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


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

Try v=pow(w,0.5f);

Tags:

C++