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