1 #ifndef SRC_CLIMATH_ROOT_HPP_
2 #define SRC_CLIMATH_ROOT_HPP_
12 #define UNLIKELY_VAL -1.11111e+30
14 template <
typename FLOAT,
typename FUNC>
15 int root(FLOAT
x1, FLOAT
x2, FLOAT xacc, FLOAT *x_root, FUNC func) {
18 fh,
fl, fm, fnew, s, xh, xl, xm, xnew;
24 static char dbmsname[] =
"_FindRoot";
28 if ((
fl > 0. && fh < 0.) || (fl < 0. && fh > 0.)) {
34 for (iter = 0; iter <
MAX_IT; iter++) {
37 s = sqrt(fm * fm -
fl * fh);
41 xnew = xm + (xm - xl) * ((
fl > fh ? 1. : -1.) * fm / s);
43 if (fabs(xnew - *x_root) <= xacc) {
53 if ((fnew > 0. ? fabs(fm) : -fabs(fm)) != fm) {
58 }
else if ((fnew > 0. ? fabs(
fl) : -fabs(
fl)) !=
fl) {
61 }
else if ((fnew > 0. ? fabs(fh) : -fabs(fh)) != fh) {
65 fprintf(stderr,
"**error:%s, should never get here\n", dbmsname);
68 if (fabs(xh - xl) <= xacc) {
73 "**error:%s, exceeded MAX_IT = %d, current root calc = %e\n",
74 dbmsname,
MAX_IT, *x_root);
86 compare = fabs(
fl) >= fabs(fh) ? 1 : -1;
95 fprintf(stderr,
"**error:%s, should never reach here\n", dbmsname);
int root(FLOAT x1, FLOAT x2, FLOAT xacc, FLOAT *x_root, FUNC func)