def binary_search(target,bounds,fn,eps=1e-2):
    """ Perform binary search to find the input corresponding to the target output
        of a given monotonic function fn up to the eps precision. Requires initial bounds
        (lower,upper) on the values of x."""
    lb,ub = bounds
    i = 0
    while ub-lb>eps:
        guess = (ub+lb)/2
        y = fn(guess)
        if y<target:
            lb = guess
        elif y>=target:
            ub = guess
        i+=1
        if i>500: assert False
    return (ub+lb)/2