zbrac.c

来自「Numerical Recipes in C的源代码」· C语言 代码 · 共 26 行

C
26
字号
#include <math.h>
#define FACTOR 1.6
#define NTRY 50

int zbrac(func,x1,x2)
float (*func)(),*x1,*x2;
{
	void nrerror();
	int j;
	float f1,f2;

	if (*x1 == *x2) nrerror("Bad initial range in zbrac");
	f1=(*func)(*x1);
	f2=(*func)(*x2);
	for (j=1;j<=NTRY;j++) {
		if (f1*f2 < 0.0) return 1;
		if (fabs(f1) < fabs(f2))
			f1=(*func)(*x1 += FACTOR*(*x1-*x2));
		else
			f2=(*func)(*x2 += FACTOR*(*x2-*x1));
	}
	return 0;
}
#undef FACTOR
#undef NTRY

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?