anonymous one year ago 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**3<x: low = ans elif ans**3>x: 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

1. e.mccormick

What part do you need explained?

2. 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

3. 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. 

4. 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.

5. anonymous

Got it now thanks a lot.

6. e.mccormick

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

8. 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.

9. anonymous

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

10. 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.

11. anonymous

its really all making sense now