以下两个代码都给出了相同的结果.但我不知道我应该在哪里提出加注声明.
def bisection(f, start, stop, eps = 1e-5, max_iteration = 100):
for __ in range(max_iteration):
half = start + (stop - start)/2
if abs(f(half)) < eps or half - start < eps:
return half
if f(half) * f(start) > 0:
start = half
else:
stop = half
else:
raise ValueError('Cannot find root in the given range')
要么
def bisection(f, start, stop, eps = 1e-5, max_iteration = 100):
for __ in range(max_iteration):
half = start + (stop - start)/2
if abs(f(half)) < eps or half - start < eps:
return half
if f(half) * f(start) > 0:
start = half
else:
stop = half
raise ValueError('Cannot find root in the given range')