#include #include using namespace std; int fsign(double a) { if (a>0.0) return 1; if (a<0.0) return -1; return 0; } double f(double x) { return (pow(x,2.0)-2); } main() { double a=0.0; double b=5.0; double tol=0.000001; int iter=0; while (b-a>tol) { iter++; double c=(a+b)/2; if (fsign(f(c))==0) break; if ( fsign(f(c))==fsign(f(a)) ) { a=c; } else { b=c; } cout << iter << "\t" << a << "\t" << c << "\t" << b << "\t" << f(a) << "\t" << f(c) << "\t" << f(b) << "\n"; } }