Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 21 Online
OpenStudy (anonymous):

I would be really glad if someone could explain me this code line by line. I have literally looking at the code for hours and I cant understand anything. Especially the while part why are we subtracting from x and comparing with epsilon and also why are we comparing ans with x. I would be really grateful if someone helped #Finding the square root of a number. x = 25 epsilon = 0.01 numGuesses = 0 ans = 0.0 while abs(ans**2 - x) >= epsilon and ans <= x: ans += 0.00001 numGuesses += 1 print 'numGuesses =', numGuessesif abs(ans**2 - x) >= epsilon:

OpenStudy (anonymous):

http://en.literateprograms.org/Newton-Raphson's_method_for_root_finding_(C) Take a while to read that and it should help clear up what's happening. Also, it's hard to understand the code without having any formatting or grouping braces. What language are you using?

OpenStudy (anonymous):

I'm sorry, it seems that class is using Python?

OpenStudy (rsmith6559):

Make a copy of the file that this code is in, and then you can edit the copy. Add spacing for what YOU feel are logical parts of the code, or even where you start having trouble understanding the code. Feel free to rename the variables and add comments to make it more understandable. It's your copy, you always have the original to go back to, so go for it! Refer to web for things like epsilon, or http://docs.python.org for things like the abs() function (absolute value). After you understand what's going on with this code, take some time to study/admire your work. What you've done is make the code more of a natural read with your new variable names, made it easier to read with your spacing changes and probably ideally commented the code. Remember this in your future programs, this is how good code should look!

OpenStudy (anonymous):

x = 25 epsilon = 0.01 numGuesses = 0 ans = 0.0 while abs(ans**2 - x) >= epsilon and ans <= x: ans += 0.00001 numGuesses += 1 print 'numGuesses =', numGuesses if abs(ans**2 - x) >= epsilon: print 'Failed on square root of', x else: print ans, 'is close to square root of', x Here formatted the code in blocks. That helped but I am still confused by the third line of the code. Why are we comparing ans with x Thanks alot for helping

OpenStudy (anonymous):

On line 4 you are changing the value of ans (incrementing by .00001). Essentially you are testing the values between 0 through 25 at that interval in order to obtain an approximation. Since no number larger than 25 can be a square-root approximation of 25, the loop terminates when that case is tested.

OpenStudy (anonymous):

ans is the variable that holds your guess, and x is the number that you are trying to find the square root of. You compare the square of ans to x, and when they are equal (or within a certain margin defined by epsilon) you have found your root. We want our post condition for the loop to be 'ans**2 = x' If you subtract x from both sides, you get 'ans**2-x=0' Since we are dealing with flating points, we won't get exactly 0 and would thus never exit the loop, so instead we set epsilon to whatever margin of error we allow. So we want our post condition to be 'ans**2-x<epsilon and ans**2-x > -epsilon', or simplified to 'abs(ans**2-x)<epsilon', meaning the absolute value of the difference between the square of ans and x is less than the margin of error allowed. Our loop condition will then be the negation of that. As long as the above statement is not true, we want to stay in the loop. The negation would be 'abs(ans**2-x)>=epsilon', and that is where we get it from. The 'ans<=x' is there to make sure we don't run an infinite loop, should ans skip over our target answer, which can happen with certain values of epsilon,x,and the ans increment amount.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!