fork download
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. double f(double x, double eps)
  7. {
  8. double s = 3, p1 = 1, p2 = 2, t = 3;
  9. for(; abs(t) > eps;)
  10. {
  11. p2 *= -2*x;
  12. p1 *= x;
  13. s += (t = p1+p2);
  14. }
  15. return s;
  16. }
  17.  
  18.  
  19. int main(int argc, char * argv[])
  20. {
  21. for(double x = 0; x < 0.45; x += 0.05)
  22. {
  23. cout << setw(5) << left << x << " " << setw(7) << f(x,0.0001) << " " << setw(7) << 3/(1-x)/(1+2*x) << endl;
  24. }
  25. }
  26.  
Success #stdin #stdout 0.01s 5544KB
stdin
Standard input is empty
stdout
0      3        3      
0.05   2.87081  2.87081
0.1    2.77777  2.77778
0.15   2.71492  2.71493
0.2    2.67855  2.67857
0.25   2.66665  2.66667
0.3    2.6786   2.67857
0.35   2.71497  2.71493
0.4    2.77774  2.77778
0.45   2.87086  2.87081