#include #include #include template auto midpoint(Domain a, Domain b) { return (a + b) / 2; } template auto distance(Domain a, Domain b) { using std::abs; return abs(a - b); } template std::pair root_bisection(Function f, Domain a, Domain b, Distance epsilon, int steps = 50) { assert(f(a) * f(b) < 0); auto ya = f(a); while (distance(a, b) > epsilon && steps-- > 0) { auto m = midpoint(a, b); auto ym = f(m); if (ya * ym < 0) { b = m; } else { a = m; ya = ym; } } return {a, b}; }