Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 21 Online
OpenStudy (anonymous):

Can anybody explain why when I type x = 1/10 [enter] then x [enter]. Mine only showing 0.1 which the video showed 0.10000000000000001? I use IDLE 2.7.2 Thank you.

OpenStudy (anonymous):

Refer to Video Lecture 5 (17:39)

OpenStudy (anonymous):

This has to do with the automatic rounding of the floating point imprecision. If you try instead of printing x, print "%.20f" % x, for instance, the error starts to show. (The .20 is inherited from C, it is just the amount of decimal numbers you want). There was another way to print the imprecision, but I don't recall now, long time since I last fiddled with Python.

OpenStudy (anonymous):

Thank you for your explanation. Is that anyway to display the actual result without rounding in Python?

OpenStudy (anonymous):

If you want to see the floating point imprecision, you will have to keep adding numbers in the %.Nf statement. Something like %.50f, or the likes. If you mean the actual result of the float division, you will have to go on faith that Python is rounding correctly, but never assume anything (i.e. never compare floating points). If you have something like 10.0 / 3.0, it will never return the "correct" answer, for instance.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!