Got Homework?
Connect with other students for help. It's a free community.
Here's the question you clicked on:
 0 viewing
EthanC
Group Title
##Can someone explains the different outcome if I ##change 0.1 to 0.5 at line " ans = ans + 0.1"
## while x = 8 ?
x = float(raw_input('Enter a number:'))
ans = 0.0
print type (ans)
while ans*ans*ans < abs(x):
ans = ans+0.1
## ans = ans +0.5
print ans
if ans*ans*ans != abs(x):
print x , ' is not a perfect cube'
else:
if x<0:
ans = ans
print 'Cube root of ', str(x), 'is' , str(ans)
 one year ago
 one year ago
EthanC Group Title
##Can someone explains the different outcome if I ##change 0.1 to 0.5 at line " ans = ans + 0.1" ## while x = 8 ? x = float(raw_input('Enter a number:')) ans = 0.0 print type (ans) while ans*ans*ans < abs(x): ans = ans+0.1 ## ans = ans +0.5 print ans if ans*ans*ans != abs(x): print x , ' is not a perfect cube' else: if x<0: ans = ans print 'Cube root of ', str(x), 'is' , str(ans)
 one year ago
 one year ago

This Question is Open

wio Group TitleBest ResponseYou've already chosen the best response.1
I'm slightly confused. A perfect cube should had a cube root that is an integer, so why are you using a float here? If you change the line to ans = ans + 0.5, then you're going to be checking less cube roots. For example you wont check 0.1 or 0.2 or 0.3 or 0.4. Thus you have a greater probability of missing the cube root.
 one year ago

wio Group TitleBest ResponseYou've already chosen the best response.1
There are better ways of finding the cube root, by the way. Like using Newton's root finding method.
 one year ago

basking Group TitleBest ResponseYou've already chosen the best response.0
Also, because you are checking fewer times (at a greater interval), that will impact the accuracy of your program's result.
 one year ago

harsimran_hs4 Group TitleBest ResponseYou've already chosen the best response.0
please use the pasting websites to paste your code http://pastebin.com/ http://dpaste.com/ definitely for the computation of a perfect cube you just need to increase the ans by 1 also you might be confused that you should not get 'not a perfect cube' , it is because of float point error.(the way computer perceives the number may be a little different than you may think)
 one year ago
See more questions >>>
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.