fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. double sin_cos(double x, unsigned int n)
  8. {
  9. double term = 1.0;
  10. double sum = term;
  11. for(unsigned int k = 1; k < n; ++k)
  12. sum += (term *= x/k)*(k/2%2 ? -1 : 1);
  13. return sum;
  14. }
  15.  
  16. int main()
  17. {
  18. for(double x = 0; x < 1; x+= 0.1)
  19. cout << setw(10) << x << setw(10) << sin_cos(x,6)
  20. << setw(10) << sin(x)+cos(x) << endl;
  21. }
  22.  
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
         0         1         1
       0.1   1.09484   1.09484
       0.2   1.17874   1.17874
       0.3   1.25086   1.25086
       0.4   1.31049   1.31048
       0.5   1.35703   1.35701
       0.6   1.39005   1.38998
       0.7   1.40924   1.40906
       0.8   1.41446   1.41406
       0.9   1.40576   1.40494
         1   1.38333   1.38177