fork(2) download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. // Stała liczba > 1
  8. double A = 10.0;
  9.  
  10. // Przedział początkowy
  11. double a = 1.0;
  12. double b = A;
  13. double c;
  14.  
  15. // Dokładność
  16. double eps = 1e-6;
  17.  
  18. // Metoda bisekcji
  19. while ((b - a) > eps) {
  20. c = (a + b) / 2.0;
  21.  
  22. if (c * c > A)
  23. b = c;
  24. else
  25. a = c;
  26. }
  27.  
  28. cout << "Przyblizona wartosc sqrt(" << A << ") = " << c << endl;
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
Przyblizona wartosc sqrt(10) = 3.16228