#include "bisection.h"

double bisec(func_ptr f, double left, double right,
        double epsilon, unsigned int *steps, unsigned int max_steps) {
    double mid = left, fmid;

    for( ; (fabs(right - left) > 2 * epsilon)
            && (!max_steps || (*steps < max_steps)); (*steps)++ ) {
        mid = (right + left) / 2;

        if( f(left) * (fmid = f(mid)) < 0 )
            right = mid;
        else if( f(right) * fmid < 0 )
            left = mid;
        else {
            (*steps)++;
            break;
        }
    }

    return mid;
}
