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

please help explain this x = int(raw_input('Enter integer :')) epsilon = 0.001 low = 0.0 high = x numguesses = 0 ans = (low+high)/2.0 while abs(ans**3-x)>=epsilon: numguesses+=1 print 'numguesses=',numguesses print low,high,ans ans =(low+high)/2.0 if ans**3x: high = ans else: if x<0: ans = (-low+high)/2.0 print ans, 'is thecube root of', x how is while abs(ans**3-x)>=epsilon: loop when the guesses of x are Enter integer :27 numguesses= 1 0.0 27 13.5 numguesses= 2 0.0 13.5 13.5

OpenStudy (e.mccormick):

What part do you need explained?

OpenStudy (anonymous):

the ans**3-x>=epsilon because when i analyzed it,it seemed like only ans=low+high/2.0-x>=epsilon was in the loop instead of ans**3-x or (low+high/2.0)**3-x>=epsilon. Can you pls explain why its like that

OpenStudy (e.mccormick):

ans = (low+high)/2.0 changes ans every time it is run. This makes the loop basically: while abs(((low+high)/2.0)**3-x)>=epsilon But it does it more cleanly and alows for some other reuse. This is wht while loop: ``` while abs(ans**3-x)>=epsilon: # start the while loop numguesses+=1 # increment the number of guesses print 'numguesses=',numguesses print low,high,ans ans =(low+high)/2.0 # re-evaluate ans. NOTE: On the first run this will not change. if ans**3<x: # If the guess, when cubed, is less than the input low = ans # then change the low so that the next guess will be higher. elif ans**3>x: # BUT, if it is higher high = ans # then change the high to make the next guess lower. else: if x<0: # and if the number is negative, ans = (-low+high)/2.0 # adjust the ans to compensate. ```

OpenStudy (e.mccormick):

One of the key things there is the while loop condition: `while abs(ans**3-x)>=epsilon` See, what it is doing there is taking the guess (ans) and cubing it, then subtracing out the number you want the cube root of. If the absolute value of that is less than epsilon, then it is the actual answer and the loop ends. However, if it is greater or equal to epsilon, then the loop runs again. This comes from the concept of epsilon-delta or \(\epsilon\rightarrow \delta\). The epsilon-delta definition of a limit relates to margins of error. It has to do with curve fitting and how close is close enough. https://www.math.ucdavis.edu/~kouba/CalcOneDIRECTORY/preciselimdirectory/PreciseLimit.html In this, the epsilon is the "close enough" on the y value or output. So really, every time you do `ans = (low+high)/2.0` you are changing delta. Then you see if the answer is within epsilon of the real answer. If it is, you call it done.

OpenStudy (anonymous):

Got it now thanks a lot.

OpenStudy (e.mccormick):

Glad to help.

OpenStudy (e.mccormick):

One of the things that may make it look like only part of the code is running is the conditional statments. The if statments cause parts of things not to run.

OpenStudy (e.mccormick):

Here is an arbitrary example to show some differences with if and elif (else if). I like green. I also like yellow. If I like green, give me a green apple. If I like yellow, give me a banana. In this example I get BOTH a green apple and a banana because I like both and I have if statments for both. But what about: I like green. I also like yellow. If I like green, give me a green apple. Else, if I like yellow, give me a banana. In this case I only get a green apple. Why? Because I used else if (elif.) Because green happened, it never got to the yellow. So what would make it get to yelow? Well... I like red. I also like yellow. If I like green, give me a green apple. Else, if I like yellow, give me a banana. In this case I would get a banana but not a green apple.

OpenStudy (anonymous):

hey you don't know how much problem you just solved for me thank again

OpenStudy (e.mccormick):

Well, it is one of these concepts that is odd... but once you get it computer programming makes more sense. Computers are very, very literal. They are the perfect idiot savant. As a savant the computer does millions of calculations in a second, all the time following a programmer's rules. The idiot part is that if the programmer makes a mistake, the computer still tries to do it. So you need to be careful, as a programmer, to be sure to tell the computer to do things the right way! Or it will just happily do them the wrong way. That is basically all a bug or logic error really is.

OpenStudy (anonymous):

its really all making sense now

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!