#include <math.h>
#define JMAX 40

double rtbis(double (*func)(int, double, int), double x1, double x2, double xacc, int site, int size)
{
	void nrerror(char error_text[]);
	int j;
	double dx,f,fmid,xmid,rtb;

	f=(*func)(site,x1,size);
	fmid=(*func)(site,x2,size);
	if (f*fmid >= 0.0) nrerror("Root must be bracketed for bisection in rtbis");
	rtb = f < 0.0 ? (dx=x2-x1,x1) : (dx=x1-x2,x2);
	for (j=1;j<=JMAX;j++) {
		fmid=(*func)(site,xmid=rtb+(dx *= 0.5),size);
		if (fmid <= 0.0) rtb=xmid;
		if (fabs(dx) < xacc || fmid == 0.0) return rtb;
	}
	nrerror("Too many bisections in rtbis");
	return 0.0;
}
#undef JMAX
/* (C) Copr. 1986-92 Numerical Recipes Software 21].,t45316. */
