Bisect Method Output Error:
I tried to solve the problem via online, forum, etc. But I can’t generate the desired output.
Step 8 - square_root_bisection(0.001, 1e-7, 50) should return a number between 0.03162267660168379 and 0.031622876601683794.
This step got the error. I change the code which the square root is found, but the error is persistent.
Your code so far
def square_root_bisection(num,tolerance=1e-6,max_iteration=100):
if num < 0:
raise ValueError("Square root of negative number is not defined in real numbers")
elif num == 0 or num == 1:
print(f"The square root of {num} is {num}")
return num
#User Editable
else:
low = 0
high = 1 if num < 1 else num
iteration = 0
while (high - low) > (2 * tolerance) and iteration < max_iteration:
iteration += 1
mid = (low + high) / 2
mid_square = mid ** 2
if abs(mid_square - num) <= tolerance:
return f"The square root of {num} is approximately {mid}"
if mid_square > num:
high = mid
else:
low = mid
#User Editable
print(f"Failed to converge within {max_iteration} iterations")
return None
print(square_root_bisection(0.001, 1e-7, 50))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36
Challenge Information:
Implement the Bisection Method - Implement the Bisection Method