import math
TOL = 1.e-6

def bisect(g, z1, z2, tol):
    while abs(z2 - z1) > tol:
        zm = (z1 + z2)/2
        if g(z1)*g(zm) > 0:
            z1 = zm
        else:
            z2 = zm
    return (z1+z2)/2

def gg(z):
    return math.cos(z) - z**2

z = bisect(gg, 0., 4., TOL)
print('z =', z, ' gg =', gg(z))
