My implementation is more general and yet simpler than other solutions: (and a public domain)
def solve(func, x = 0.0, step = 1e3, prec = 1e-10): """Find a root of func(x) using the bisection method. The function may be rising or falling, or a boolean expression, as long as the end points have differing signs or boolean values. Examples: solve(lambda x: x**3 > 1000) to calculate the cubic root of 1000. solve(math.sin, x=6, step=1) to solve sin(x)=0 with x=[6,7). """ test = lambda x: func(x) > 0
source share