Using standard math functions with template arguments in C ++

I wrote the code:

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*((ba)*points[i]+(a+b))); } return 0.5*(ba)*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); } }; 

which is used here:

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

but the compiler complains:

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

The idea was to make it work for different types of numbers, for example. double and complex<double> , where atan is defined. How can i fix this?

+4
source share
2 answers

Visual Studio will interpret 0 as an integer and does not call std::atan , because it does not know what type of float double long double integer should be cast to

  ~ \ documents \ visual studio 2010 \ projects \ so-atan \ so-atan \ quadrature.h (44): error C2668: 'atan': ambiguous call to overloaded function 
           c: \ program files (x86) \ microsoft visual studio 10.0 \ vc \ include \ math.h (553): can be 'long double atan (long double)'
           c: \ program files (x86) \ microsoft visual studio 10.0 \ vc \ include \ math.h (505): or "float atan (float)"
           c: \ program files (x86) \ microsoft visual studio 10.0 \ vc \ include \ math.h (108): or "double atan (double)"          
           ~ \ documents \ visual studio 2010 \ projects \ so-atan \ so-atan \ main.cpp (12):
           in [...] "TNumber Quadrature :: integrateToInfty (TFunction, TNumber)".
           with
           [
               TNumber = int,
               TFunction = double (__cdecl *) (double)
           ] 

You can very easily recreate this behavior:

 #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; } 

This will create the same error. To get rid of this, you should either use a specific version of std::atan , std::cos and std::tan , using static_cast<double> or static_cast<long double> in your quadrature,

 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); /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^ */ } 

or use a floating number instead of an integer in your call (which is much simpler):

 cout<<"e^(-2x)="<<quadrature.integrateToInfty(dampendedExp, 0.0)<<endl; // cout<<"e^(-2x)="<<quadrature.integrateToInfty(dampendedExp, static_cast<double>(x))<<endl; 
+2
source

Canonical solution using std::atan; return integrate(atan(a), PI/2); using std::atan; return integrate(atan(a), PI/2); . This gives an argument-dependent lookup in the TFunction namespace.

-1
source

Source: https://habr.com/ru/post/1416592/


All Articles