A community for students.
Here's the question you clicked on:
 0 viewing
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**3x)>=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**3x)>=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
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**3x)>=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**3x)>=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

This Question is Closed

e.mccormick
 one year ago
Best ResponseYou've already chosen the best response.3What part do you need explained?

anonymous
 one year ago
Best ResponseYou've already chosen the best response.0the ans**3x>=epsilon because when i analyzed it,it seemed like only ans=low+high/2.0x>=epsilon was in the loop instead of ans**3x or (low+high/2.0)**3x>=epsilon. Can you pls explain why its like that

e.mccormick
 one year ago
Best ResponseYou've already chosen the best response.3ans = (low+high)/2.0 changes ans every time it is run. This makes the loop basically: while abs(((low+high)/2.0)**3x)>=epsilon But it does it more cleanly and alows for some other reuse. This is wht while loop: ``` while abs(ans**3x)>=epsilon: # start the while loop numguesses+=1 # increment the number of guesses print 'numguesses=',numguesses print low,high,ans ans =(low+high)/2.0 # reevaluate 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. ```

e.mccormick
 one year ago
Best ResponseYou've already chosen the best response.3One of the key things there is the while loop condition: `while abs(ans**3x)>=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 epsilondelta or \(\epsilon\rightarrow \delta\). The epsilondelta 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
 one year ago
Best ResponseYou've already chosen the best response.0Got it now thanks a lot.

e.mccormick
 one year ago
Best ResponseYou've already chosen the best response.3One 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
 one year ago
Best ResponseYou've already chosen the best response.3Here 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
 one year ago
Best ResponseYou've already chosen the best response.0hey you don't know how much problem you just solved for me thank again

e.mccormick
 one year ago
Best ResponseYou've already chosen the best response.3Well, 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
 one year ago
Best ResponseYou've already chosen the best response.0its really all making sense now
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.