def bisection(func, low, high, eps):
    """
    Find the root on the interval [low, high] to the tolerance eps
    ( eps apples to both function value and separation of the bracketing values )
    """

    # if bracket is backwards, correct this
    if low > high:
        low, high = high, low  # standard way to swap two variables
        # other way:
        #tmp = low
        #low = high
        #high = tmp

    flow = func(low)
    fhigh = func(high)
    if flow*fhigh > 0:
        print("root is not bracketd by [", low, ", ", high,"]")
        exit(1)

    mid = 0.5*(high+low)
    fmid = func(mid)

    while (high-low) > eps and abs(fmid)>eps:
        if fmid*flow < 0:
            high = mid
            fhigh = fmid
        else:
            low = mid
            flow = fmid

        mid = 0.5*(high+low)
        fmid = func(mid)

    return mid