fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. double ch(double x, double eps = 1e-7)
  7. {
  8. double s = 1, t = 1;
  9. x *= x;
  10. for(int k = 1; abs(t) > eps; k += 2)
  11. s += t *= x/k/(k+1);
  12. return s;
  13. }
  14.  
  15. int main(int argc, char * argv[])
  16. {
  17. for(double x = 0; x <= 1; x += 0.1)
  18. cout << x << " " << ch(x) << " " << (exp(x)+exp(-x))/2 << endl;
  19.  
  20. }
  21.  
Success #stdin #stdout 0.01s 5492KB
stdin
Standard input is empty
stdout
0  1  1
0.1  1.005  1.005
0.2  1.02007  1.02007
0.3  1.04534  1.04534
0.4  1.08107  1.08107
0.5  1.12763  1.12763
0.6  1.18547  1.18547
0.7  1.25517  1.25517
0.8  1.33743  1.33743
0.9  1.43309  1.43309
1  1.54308  1.54308