#ifndef BISECTION_METHOD_HH
#define BISECTION_METHOD_HH

#include <cmath>

#include "function.hh"


/**
 * Implementierung des Bisektionsalgorithmus' als Prozedur;
 * im Parameter n_count wird die benoetigte Anzahl an Iterationen zurueckgegeben.
 */
double bisection_method (const Function& f, double a, double b, double eps,
			 unsigned int& n_count)
{
  double x1 = a;
  double x2 = b;
  double m;

  // Initialisierung
  n_count = 0;

  while (std::abs(x2-x1) > eps)
  {
    m = (x1+x2) / 2;
    if (f.evaluate(m) * f.evaluate(x1) >= 0)
    {
      x1 = m;
    }
    else
    {
      x2 = m;
    }
    // Erhoehe Anzahl durchlaufener Iterationsschritte
    n_count++;
  }
  return (x1+x2) / 2;
}


#endif