anonymous
  • 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
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
schrodinger
  • schrodinger
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
e.mccormick
  • e.mccormick
What part do you need explained?
anonymous
  • 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
e.mccormick
  • 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**3x: # 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. ```

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

e.mccormick
  • 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.
anonymous
  • anonymous
Got it now thanks a lot.
e.mccormick
  • e.mccormick
Glad to help.
e.mccormick
  • 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.
e.mccormick
  • 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.
anonymous
  • anonymous
hey you don't know how much problem you just solved for me thank again
e.mccormick
  • 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.
anonymous
  • anonymous
its really all making sense now

Looking for something else?

Not the answer you are looking for? Search for more explanations.