fork(1) download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. // Definicja funkcji
  7. double f(double x)
  8. {
  9. return x * x - 0.5;
  10. }
  11.  
  12. int main()
  13. {
  14. double a = 0.0;
  15. double b = 1.0;
  16. double eps = 1e-6; // dokładność
  17. double c;
  18.  
  19. if (f(a) * f(b) >= 0)
  20. {
  21. cout << "Brak zmiany znaku na przedziale!" << endl;
  22. return 1;
  23. }
  24.  
  25. while ((b - a) > eps)
  26. {
  27. c = (a + b) / 2.0;
  28.  
  29. if (f(c) == 0.0)
  30. break;
  31. else if (f(a) * f(c) < 0)
  32. b = c;
  33. else
  34. a = c;
  35. }
  36.  
  37. cout << "Przyblizone miejsce zerowe: " << (a + b) / 2.0 << endl;
  38.  
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Przyblizone miejsce zerowe: 0.707107