I have several mathematical algorithms that use iteration to search for the right answer. Here is one example:
def Bolzano(fonction, a, b, tol=0.000001):
while abs(b - a) > tol:
m = (a + b) / 2
if sign(fonction(m)) == sign(fonction(a)):
a = m
else:
b = m
return a, b
I want to count how many times the algorithm goes through the loop to get a and b. However this is not a for
function and it isn't a list, so I can't clearly indicate what objects do I want to count if I use enumerate
. Is there a way to count those loops?
Note: I am not trying to change the code itself. I am really searching for a way to count iterations in a while
loop, which I can then use for other cases.