代码之家  ›  专栏  ›  技术社区  ›  Dr Deo

不明确的pow()函数

c++
  •  7
  • Dr Deo  · 技术社区  · 15 年前

    我想打个简单的电话给 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;
    }
    

    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) .

    6 回复  |  直到 11 年前
        1
  •  20
  •   Cogwheel    15 年前

    在队列中:

    v=pow(w,0.5);
    

    w 是一个浮子和 0.5 double . 你可以用 0.5f 相反。

        2
  •  5
  •   Andrew Selle    15 年前

    数学函数(如PUE)、SN()等在更现代的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
    
        3
  •  2
  •   NG.    15 年前

    v=pow(w,0.5f);

        4
  •  2
  •   esseff    15 年前

    0.5 是双重的。尝试

    v=pow(w,0.5f);
    
        5
  •  1
  •   Edward Strange    15 年前

    嘿,你试过0.5f吗?

        6
  •  0
  •   Kirill V. Lyadvinsky    15 年前

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