A community for students.
Here's the question you clicked on:
 0 viewing
EthanC
 3 years ago
##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)
EthanC
 3 years ago
##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)

This Question is Open

wio
 3 years ago
Best ResponseYou've already chosen the best response.1I'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.

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

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

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.0please 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)
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.