fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. double pierwiastek(double x, double eps) {
  7. double a = 0.0;
  8. double b = x;
  9. double c;
  10. while (fabs(b - a) > eps) {
  11. c = (a + b) / 2;
  12. if (c * c > x)
  13. b = c;
  14. else
  15. a = c;
  16. }
  17. return c;
  18. }
  19. int main() {
  20. cout << setprecision(2) << pierwiastek(2.0, 0.1)<< endl;
  21. cout << setprecision(3) << pierwiastek(2.0, 0.01)<< endl;
  22. cout << setprecision(5) << pierwiastek(2.0, 0.00001)<< endl;
  23. return 0;
  24. }
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
1.4
1.41
1.4142