関数 f(x) について区間 [a,b] で次のような条件が分かっていたとします。

f(c)>0
f(c)<0
区間 [a,b] で f(a) と f(b) が同符号である場合、永久ループとなってしまうことに気をつけてください。
// ver.041107
#ifndef __BISECTION_H
#define __BISECTION_H
// Function: y=0 を求めたい関数
// left: 計算開始の左端
// right: 計算開始の右端
// e: 終了基準 left right の間隔
double Bisection(double (*Function)(double), double left, double right, double e)
{
bool rev = false;
if(Function(left) > 0) rev = true;
double c;
for(int i = 0; ; i++){
c = (left + right) / 2;
double f = Function(c);
if(rev) f = -f;
if(f < 0)
left = c;
else
right = c;
if(right - left < e) break;
}
c = (left + right) / 2;
return c;
}
#endif
#include <iostream>
#include <cmath>
using namespace std;
#include "Bisection.h"
double F(double x);
main()
{
cout << Bisection(F, 0, 3.00, pow(10.0, -10.0)) << endl;
return 0;
}
double F(double x)
{
return cos(x);
}