我试图简单地调用math.h中的pow()
函数,类似于..
#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;
}
但是visual studio表示这是一个错误
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)'
我以为我的格式为float pow(float, float)
。
答案 0 :(得分:19)
在行中:
v=pow(w,0.5);
w
是一个浮点数,0.5
是double
。您可以改为使用0.5f
。
答案 1 :(得分:4)
像pow(),sin()等数学函数在更现代的C ++实现中被模板化。它含糊不清的原因是它不清楚你想做什么。如果您发送的两个参数都相同,那么您可能希望计算以该特定精度完成。如果它们不同,那么您希望以更高的精度计算并向上转换较低精度的操作数,还是要将较高的精度向下转换为较低的精度,然后以较低的精度进行计算。即。
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
答案 2 :(得分:2)
尝试
v=pow(w,0.5f);
答案 3 :(得分:2)
0.5
的类型为double。尝试
v=pow(w,0.5f);
答案 4 :(得分:1)
答案 5 :(得分:0)
除了在其他答案中已经给出的所有其他方法之外,您始终可以明确指定模板参数:
float w = 3.0f;
double v = 1.5;
v = pow<float>(w, v);