Question: (Python)Modify the following code so that it accurately (i.e. within ) takes the square root of any non-negative number: In [ ]: def sqrt(x): epsilon
(Python)Modify the following code so that it accurately (i.e. within ) takes the square root of any non-negative number:
In [ ]:
def sqrt(x):
epsilon = 0.001
left = 0
right = x
numGuesses = 0
guess = (right+left)/2.0
while abs(guess**2 - x) > epsilon:
numGuesses += 1
if guess**2 < x:
left = guess
else:
right = guess
guess = (right+left)/2.0
return guess
#sqrt(3) ## Uncomment this line and try.
sqrt(.25) ## Uncomment this line and try. Why does this loop forever?
In [ ]:
# YOUR CODE HERE
raise NotImplementedError()
(python)Write a function that takes the cube root of any real number within =.001=.001 of the true answer.
In [ ]:
# YOUR CODE HERE
raise NotImplementedError()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
