Implement the Bisection Method - Step 8

Tell us what’s happening:

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

You are close, there’s though one detail making precision smaller than desired. Take a look at the beginning of instructions, related to the interval.

1 Like

Is it this?

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?

You’ve got the correct paragraph of instruction. However, have you applied it?

if abs(square - number) < tolerance:

This doesn’t seem to implement the above sentence?

Try to translate the sentence about tolerance into a WHILE loop.

Forget what you think about a tolerance and try to implement only what’s in the instructions.

This does sound closer, can you share your full updated code?

Does your loop end when the tolerance is reached?

1 Like

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))

Correct, but if you keep halving the interval, it will get smaller and smaller, so it will be less than the tolerance for many iterations.

When does your for loop stop updating root ? What’s the condition? It should be this:

1 Like

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!

1 Like