Tell us what’s happening:
I think that the check software is not allowing for the characters to get small enough, because if I chance the parameters to 1e-8 for decimals, it returns a number within the range. I don’t know what is wrong.
Your code so far
def square_root_bisection(input_value, tolerance = .01, max_iter = 10):
if input_value < 0:
raise ValueError("Square root of negative number is not defined in real numbers")
if input_value == 0 or input_value == 1:
print(f"The square root of {input_value} is {input_value}")
return input_value
i = 0
low = 0
high = input_value
mid = (low + input_value) / 2
if input_value > 1:
while i <= max_iter:
print(f"Mid: {mid}")
mid = (low + high) / 2
if mid*mid >= (input_value - tolerance) and (mid*mid <= input_value + tolerance):
print(f"The square root of {input_value} is approximately {mid}")
return mid
break
elif mid*mid > input_value:
print(f"{mid}")
high = mid
else:
print(f"{mid}")
low = mid
i += 1
print(f"Failed to converge within {max_iter} iterations")
return None
if input_value < 1:
low = input_value
high = 1
mid = (low + high) / 2
while i <= max_iter:
mid = (low + high) / 2
if mid*mid > (input_value - tolerance) and (mid*mid < input_value + tolerance):
print(f"The square root of {input_value} is approximately {mid}")
return mid
break
elif mid*mid > input_value:
high = mid
else:
print(f"{mid}")
low = mid
i += 1
print(f"Failed to converge within {max_iter} iterations")
return None
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/143.0.0.0 Safari/537.36
Challenge Information:
Implement the Bisection Method - Implement the Bisection Method