12345678910111213141516171819202122
#include <iostream> #include <cmath> using namespace std; double fac(int n) { double f=n; while (--n)f*=n; return f; } int main(int argc, char *argv[]) { const double x=2.3145; double cos_approx=1; for (int i=0;i<20;i++)cos_approx+=(i%2? 1 : -1)*pow(x,i*2+2)/fac(i*2+2); cout << "cos(x): " << cos(x) << endl << "cos approx.: " << cos_approx << endl; double sin_approx=x; for (int i=0;i<20;i++)sin_approx+=(i%2? 1 : -1)*pow(x,i*2+3)/fac(i*2+3); cout << "sin(x): " << sin(x) << endl << "sin approx.: " << sin_approx << endl; }