5

我写了代码:

const double PI = 3.141592653589793;

namespace 
{
    const int N = 8;
    const double points[8] = {-0.9602898564975363, -0.7966664774136267, -0.5255324099163290, -0.1834346424956498,
        0.1834346424956498, 0.5255324099163290, 0.7966664774136267, 0.9602898564975363};
    const double weights[8] = {0.1012285362903706, 0.2223810344533744, 0.3137066458778874, 0.3626837833783621, 0.3626837833783621,
        0.3137066458778874, 0.2223810344533744, 0.1012285362903706};
    const double error = 1e-10;

    template <class TFunction, class TNumber>
    class ChangeOfVariables
    {
        TFunction f;
    public:
        ChangeOfVariables(TFunction f) : f(f){}
        TNumber operator() (TNumber x) { TNumber c=std::cos(x); return f(std::tan(x))/(c*c); } 
    };
}


class Quadrature
{
public:
    Quadrature(void) {};
    ~Quadrature(void) {};

    template <class TFunction, class TNumber>
    TNumber integrate(TFunction f, TNumber a, TNumber b) 
    {
        TNumber result = 0.0;
        for(int i=0; i<N; i++) 
        {
            result+= weights[i]*f(0.5*((b-a)*points[i]+(a+b)));
        }
        return 0.5*(b-a)*result;
    }

    template <class TFunction, class TNumber>
    TNumber integrateToInfty(TFunction f, TNumber a)
    {
        using std::atan; 
        return integrate<ChangeOfVariables<TFunction,TNumber>, TNumber>(ChangeOfVariables<TFunction,TNumber>(f), atan(a), PI/2); 
    }
};

在这里使用:

double dampendedExp(double x) {return std::exp(-2.0*x);}
int main()
{
    Quadrature quadrature;
    cout<<"e^(-2x)="<<quadrature.integrateToInfty(dampendedExp, 0)<<endl;
}

但编译器抱怨:

Error   1   error C2668: 'atan' : ambiguous call to overloaded function c:\users\ga1009\documents\dev\fouriertransform\fouriertransform\quadrature.h    48

这个想法是让它适用于不同的数字类型,例如doublecomplex<double>,其中定义了 atan 。我该如何解决?

4

2 回答 2

3

Visual Studio 将解释0为整数并在调用 时失败,std::atan因为它不知道整数应转换为哪个浮点类型 ( float, double, ):long double

~\documents\visual studio 2010\projects\so-atan\so-atan\quadrature.h(44): 错误 C2668: 'atan': 对重载函数的模糊调用
          c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(553): 可以是'long double atan(long double)'
          c:\program 文件 (x86)\microsoft visual studio 10.0\vc\include\math.h(505): 或“float atan(float)”
          c:\program 文件 (x86)\microsoft visual studio 10.0\vc\include\math.h(108): 或 "double atan(double)"          
          ~\documents\visual studio 2010\projects\so-atan\so-atan\main.cpp(12):
          在 [...] “TNumber Quadrature::integrateToInfty(TFunction,TNumber)”中。
          和
          [
              TNumber=int,
              TFunction=double (__cdecl *)(double)
          ]

您可以非常轻松地自己重新创建此行为:

#include <iostream>

float f(float x){return x;}
double f(double x){return x;}
long double f(long double x){return x;}

int main()
{
    std::cout << f(0) << std::endl;
}

这将产生同样的错误。要摆脱这种情况,您应该使用特定版本的std::atanstd::cos并在求积中std::tan使用static_cast<double>or static_cast<long double>

template <class TFunction, class TNumber>
    TNumber integrateToInfty(TFunction f, TNumber a)
    {
        using std::atan; 
        return integrate<ChangeOfVariables<TFunction,TNumber>, TNumber>
         (ChangeOfVariables<TFunction,TNumber>(f),
              atan(static_cast<long double>(a)), PI/2); 
              /*   ^^^^^^^^^^^^^^^^^^^^^^^^^^^       */          
    }

或在调用中使用浮点数而不是整数(这更容易):

cout<<"e^(-2x)="<<quadrature.integrateToInfty(dampendedExp, 0.0)<<endl;
// cout<<"e^(-2x)="<<quadrature.integrateToInfty(dampendedExp, static_cast<double>(x))<<endl;
于 2012-06-07T15:48:13.013 回答
-1

规范的解决方案是using std::atan; return integrate(atan(a), PI/2);。这为您提供了TFunction.

于 2012-06-07T14:42:47.820 回答