Advertisement
karbaev

binsearch-func

Feb 29th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.43 KB | None | 0 0
  1. #define EPS 1E-9
  2.  
  3. double f(double x)
  4. {
  5.    //монотонная функция на отрезке [a, b], например f(x) = x^3:
  6.    return x*x*x;
  7. }
  8.  
  9. double binarySearch(double C, double a, double b)
  10. {
  11.    double low = a, high = b;
  12.    while(abs(low-high) < EPS)
  13.    {
  14.       double mid = low + (high - low) / 2;
  15.       if f(mid) < C
  16.           low = mid;
  17.       else
  18.           high = mid;
  19.    }
  20.    return (low + high)/2;
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement