Skip to main content

bisect

Function bisect 

Source
pub fn bisect(lo: f64, hi: f64, below: impl FnMut(f64) -> bool) -> f64
Expand description

The boundary in (lo, hi) of a predicate that holds below a point and fails above it, by bisection to full precision: the loop stops when the midpoint no longer lies strictly between the ends.

For f(x) = target with f increasing, pass |x| f(x) < target. If below is not monotone the result is some point where it changes.

use prospicio_math::roots::bisect;

let r = bisect(0.0, 2.0, |x| x * x < 2.0);
assert!((r - 2f64.sqrt()).abs() < 1e-15);