A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 4 years ago
can anyone help me run this code properly?
when i test it, the code spits out obserd awnsers such as sqrtbi(9, 0.0001) = 2.25... instead of 3, how can i count on it to find the square root of any given number?
anonymous
 4 years ago
can anyone help me run this code properly? when i test it, the code spits out obserd awnsers such as sqrtbi(9, 0.0001) = 2.25... instead of 3, how can i count on it to find the square root of any given number?

This Question is Closed

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0def sqrtbi(x, z): assert x>=0, 'x must be nonnegitive, not' + str(x) assert z > 0, 'espilon must be positive, not' + str(z) low = 0 high = max(x, 1.0) guess = (low + high)/2.0 ctr = 1 while abs(guess**2  x) > z and ctr <= 100: #print 'low:', low, 'high:', high, 'guess:', guess if guess**2 < x: low = guess else: high = guess guess = (low + high)/2.0 ctr += 1 assert ctr <= 100, 'Ineration count exceeded' print 'Bi method. Num. Interactios', ctr, 'Estimation', guess return guess

maitre_kaio
 4 years ago
Best ResponseYou've already chosen the best response.1sqrtbi(9.0, 0.01): Bi method. Num. Interactios 11 Estimation 3.00146484375 And maybe I'm a little psychotic, but I say it one more time, please use a pastebin to make it easy for us to copy the code. And no need to start a new question, you'd better take into account the answers given to the other one.

maitre_kaio
 4 years ago
Best ResponseYou've already chosen the best response.1http://en.wikipedia.org/wiki/Pastebin Thx for the code, but because I'm a good guy, I reformatted it myself. And tested it too: sqrtbi(9.0, 0.00001) > Bi method. Num. Interactios 21 Estimation 3.00000143051

maitre_kaio
 4 years ago
Best ResponseYou've already chosen the best response.1lol i understand now... I reformatted your code too well sorry. If you look at the return statement, it is inside the while loop. It should be after the loop, like this: http://pastebin.com/8H6YW4Kn
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.