模糊的pow()函数

Dr *_*Deo 7 c++

我试图简单地调用pow()math.h中的函数,类似于..

#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;
}
Run Code Online (Sandbox Code Playgroud)

但是视觉工作室说这是一个错误

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)'
Run Code Online (Sandbox Code Playgroud)

我以为我有格式float pow(float, float).

Cog*_*eel 19

在线:

v=pow(w,0.5);
Run Code Online (Sandbox Code Playgroud)

w是一个浮动,0.5是一个double.你可以0.5f改用.


小智 5

像 pow()、sin() 等数学函数在更现代的 C++ 实现中被模板化。之所以模棱两可,是因为不清楚你想做什么。如果您发送的两个参数相同,您可能希望以该特定精度完成计算。如果它们不同,那么您是要以较高的精度计算并向上转换较低精度的操作数,还是要将较高的精度向下转换为较低的精度,然后以较低的精度进行计算。IE

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
Run Code Online (Sandbox Code Playgroud)