fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. double Func(double x, double eps, double term = 0, int n = 0)
  8. {
  9. if (n == 0) term = x*x*x;
  10. else
  11. {
  12. if (abs(term) < eps) return term;
  13. term *= -x*x/(2*n*(2*n-1));
  14. }
  15. return term + Func(x,eps,term,n+1);
  16. }
  17.  
  18. int main(int argc, char * argv[])
  19. {
  20. for(double x = 0; x < 1; x += 0.1)
  21. cout << setw(5) << x << setw(15) << Func(x,1e-8) << setw(15) << x*x*x*cos(x) << endl;
  22. }
  23.  
Success #stdin #stdout 0.01s 5532KB
stdin
Standard input is empty
stdout
    0              0              0
  0.1    0.000995008    0.000995004
  0.2     0.00784053     0.00784053
  0.3      0.0257941      0.0257941
  0.4      0.0589479      0.0589479
  0.5       0.109698       0.109698
  0.6       0.178272       0.178272
  0.7       0.262341       0.262341
  0.8       0.356714       0.356714
  0.9       0.453154       0.453154
    1       0.540302       0.540302