// bisection method revisted

public class bisection {
    
    public static void main(String args[]) {
	double tol,s,e,m;

	tol = 0.01;
	s = 0;
	e = 10;

  	do {
	    m = (s+e)/2;

	    if(f(m)*f(s) < 0)
		e = m;
	    else if(f(m)*f(e) < 0)
		s = m;
	    else {
		System.err.println("Error");
		System.exit(0);
	    }
	    
	} while(Math.abs(f(m)) > tol);

	System.out.println("Root: " + m);
	
	System.out.println("Test of root: " + f(m));
	
    } // method main

    public static double f(double x) {
	
	double a0 = -2;
	double a1 = -1;
	double a2 =  1;
	return a0 + a1*x + a2*x*x ;
    } // method f
	
} // class bisection

/* Output:

Root: 2.001953125
Test of root: 0.005863189697265625

*/
