def find_root(a, b, tol, f): """prec: the function f is continuous and changes sign on [a,b]. postc: returns a value that is within tol of a root of f.""" while b - a > tol: mid = (a + b)/2 if f(a)*f(mid) < 0: # change sign on [a, mid] b = mid elif f(mid)*f(b) <= 0: # change signon [mid, b] a = mid return mid approximate = find_root(1, 2, .00001, lambda x: x*x - 2) gold_standard = 2**(.5) print(approximate - gold_standard) print(.00001)