Implement the Bisection Method - Implement the Bisection Method

Tell us what’s happening:

square_root_bisection(0.001, 1e-7, 50) should return a number between 0.03162267660168379 and 0.031622876601683794.

Your code so far

def square_root_bisection(number, tolerance=0.01, max_iteration=100):
       if number < 0:
           raise ValueError("Square root of negative number is not defined in real numbers")
       if number == 0 or number == 1:
           print(f"The square root of {number} is {number}")
           return number
       
       low = 0.0
       high = max(1.0, number)
       
       for _ in range(max_iteration):
           mid = (low + high) / 2.0
           print(f"{_} {mid}")
           square_target = mid * mid
           if abs(square_target - number) <= tolerance:
               print(f"square_target {square_target}")
               print(f"The square root of {number} is approximately {mid}")
               return mid
           if square_target > number:
               high = mid
           else:
               low = mid
       
       print(f"Failed to converge within {max_iteration} iterations")
       return None

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:148.0) Gecko/20100101 Firefox/148.0

Challenge Information:

Implement the Bisection Method - Implement the Bisection Method

hi, you posted some code but didn’t ask any specific question about it or the exercise.
What is your question?