## A community for students. Sign up today

Here's the question you clicked on:

## 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
1. wio

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.

2. wio

There are better ways of finding the cube root, by the way. Like using Newton's root finding method.

3. basking

Also, because you are checking fewer times (at a greater interval), that will impact the accuracy of your program's result.

4. harsimran_hs4

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)

#### Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy