I am doing python certification and I don’t understand why I am getting the wrong number in step 8 (and a few others),
I am supposed to get something between 0.03162267660168379 and 0.031622876601683794 but I am getting 0.03162384033203125, I looked for other examples of the code and the logic seems to be correct, I even copied FreeCodeCamp’s step-by-step tutorial code to see if it worked and it gets the wrong result for square_root_bisection(0.001, 1e-7, 50).
Your code so far
def square_root_bisection(number, tolerance=1e-3, max_number=100):
if number < 0:
raise ValueError('Square root of negative number is not defined in real numbers')
elif number in (0, 1):
print (f'The square root of {number} is {number}')
return number
low = 0
high = max(1, number)
root = None
for i in range(max_number):
mid = (low + high) / 2
square = mid ** 2
if abs(square - number) < tolerance:
root = mid
elif square > number:
high = mid
else:
low = mid
if root:
return root
else:
print(f'Failed to converge within {max_number} 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; rv:143.0) Gecko/20100101 Firefox/143.0
Challenge Information:
Implement the Bisection Method - Implement the Bisection Method
For example, if the tolerance is 0.01, the bisection method will keep halving the interval until the difference between the upper and lower bounds is less than or equal to 0.01
After reading that I changed the if statement inside the for loop to do abs(high - low) and it did give me a different number 0.03162279725074768 it looks closer to the result but it’s still not correct enough, is it about formatting the result?
I thought the If statement would only take results where the difference is lower than the tolerance, in a while loop it would possibly look like While abs(high - low) < tolerance, here is my code:
def square_root_bisection(number, tolerance=1e-3, max_number=100):
if number < 0:
raise ValueError('Square root of negative number is not defined in real numbers')
elif number in (0, 1):
print (f'The square root of {number} is {number}')
return number
low = 0
high = max(1, number)
root = None
for i in range(max_number):
mid = (low + high) / 2
square = mid ** 2
if abs(high - low) < tolerance:
root = mid
elif square > number:
high = mid
else:
low = mid
if root:
return f'The square root of {number} is approximately {root}'
else:
print(f'Failed to converge within {max_number} iterations')
return None
print(square_root_bisection(0.001, 1e-7, 50))
I see, the for loop found the root at loop no.24 and kept on going with the same number until the end, I tried returning right after finding it or putting a break if that condition is true but it is still incorrect:
Edit: I checked the all incorrect results and they all seem to fall between the goals: e.g. for step 11 it returns 15.000200271606445 which falls between 14.999 and 15.001
def square_root_bisection(number, tolerance=1e-3, max_number=100):
if number < 0:
raise ValueError('Square root of negative number is not defined in real numbers')
elif number in (0, 1):
print (f'The square root of {number} is {number}')
return number
low = 0
high = max(1, number)
root = None
for i in range(max_number):
mid = (low + high) / 2
square = mid ** 2
if abs(high - low) <= tolerance:
root = mid
break
elif square > number:
high = mid
else:
low = mid
if root:
return f'The square root of {number} is approximately {root}'
else:
print(f'Failed to converge within {max_number} iterations')
return None
print(square_root_bisection(0.001, 1e-7, 50))
I think I found what was wrong in my case, I returned the number and printed the sentence outside the function and it marked step 8 as correct! Thanks for the explanation!