Back to Freecodecamp

Step 13

curriculum/challenges/english/blocks/learn-the-bisection-method-by-finding-the-square-root-of-a-number/65ef1c34a541afc12dbb849d.md

latest1.5 KB
Original Source

--description--

The abs() function returns the absolute value of a number, which is always positive, regardless of the number sign. You will use it to check that the estimated square root is close enough to the actual value.

Now, create an if statement to check if the absolute value of the difference between square_mid and square_target is within the specified tolerance.

--hints--

You should have an if statement with the condition abs(square_mid - square_target) < tolerance: inside the body of the for loop.

js
({
    test: () =>
    {
        assert(runPython(`_Node(_code).find_function("square_root_bisection").find_ifs()[1].find_bodies()[2].find_for_loops()[0].find_ifs()[0].find_conditions()[0].is_equivalent("abs(square_mid - square_target) < tolerance")`))
    } 
})

--seed--

--seed-contents--

py
def square_root_bisection(square_target, tolerance=1e-7, max_iterations=100):
    if square_target < 0:
        raise ValueError('Square root of negative number is not defined in real numbers')
    if square_target == 1:
        root = 1
        print(f'The square root of {square_target} is 1')
    elif square_target == 0:
        root = 0
        print(f'The square root of {square_target} is 0')

    else:
        low = 0
        high = max(1, square_target)
        root = None
        
--fcc-editable-region--
        for _ in range(max_iterations):
            mid = (low + high) / 2
            square_mid = mid**2
            
--fcc-editable-region--