2 #define UNLIKELY_VAL -1.11111e+30
13 double fh,
fl, fm, fnew, s, xh, xl, xm, xnew;
14 static char name[] =
"_root";
18 if ((
fl > 0. && fh < 0.) || (fl < 0. && fh > 0.)) {
24 for (iter = 0; iter <
MAX_IT; iter++) {
27 s = sqrt(fm * fm -
fl * fh);
31 xnew = xm + (xm - xl) * ((
fl > fh ? 1. : -1.) * fm / s);
33 if (fabs(xnew - *x_root) <= xacc) {
38 fnew = func(*x_root, aux);
43 if ((fnew > 0. ? fabs(fm) : -fabs(fm)) != fm) {
48 }
else if ((fnew > 0. ? fabs(
fl) : -fabs(
fl)) !=
fl) {
51 }
else if ((fnew > 0. ? fabs(fh) : -fabs(fh)) != fh) {
56 "### FATAL ERROR in _root function: should never get here\n");
59 if (fabs(xh - xl) <= xacc) {
63 fprintf(stderr,
"### FATAL ERROR in _root function: exceeded MAX_IT = ");
64 fprintf(stderr,
"%d\n",
MAX_IT);
65 fprintf(stderr,
"current root calc = ");
66 fprintf(stderr,
"%12.4g\n", *x_root);
78 compare = fabs(
fl) >= fabs(fh) ? 1 : -1;
87 fprintf(stderr,
"### FATAL ERROR in _root function: should never get here\n");
int root(double x1, double x2, double xacc, double *x_root, RootFunction_t func, void *aux)
double(* RootFunction_t)(double, void *)