fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <functional>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7. double solve(function<double (double)> f, double a, double b, double eps)
  8. {
  9. if (a > b) swap(a, b);
  10. while ( abs(a - b) > eps ) {
  11. double x = (a + b) / 2.0;
  12. if ( f(a) * f(x) < 0 )
  13. b = x;
  14. else a = x;
  15. }
  16. return (a + b) / 2.0;
  17. }
  18.  
  19. int main()
  20. {
  21. auto f1 = [](double x)->double { return 2 * pow(x, 2.0) - pow(0.5, x) - 3; };
  22. cout << fixed << setprecision(11) << solve( f1, 1, 2, 0.00001 );
  23. return 0;
  24. }
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
1.30475997925