Unable to calculate 1.2-1 in Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Unable to calculate 1.2-1 in Python

when I try to calculate 1.2-1 the answer I got is .199999999999996 why it is not giving the accurate answer (.2)? Ian using Python 3.5.3

19th Feb 2017, 8:06 AM
Nithin
Nithin - avatar
2 Answers
+ 5
Floats are represented as binary fractions in computing. There is only a limited precision to what extent this representation can be accurate. What is even more important, a boolean test of: .1999999999999996 == 0.2 might give you True as a result. That's just the way it is. If you are not ok with how this looks on the screen, try using .format string method to show only one, two or how many you want decimal digits.
19th Feb 2017, 8:14 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 1
https://docs.python.org/3.5/tutorial/floatingpoint.html
19th Feb 2017, 8:17 AM
wi34rd
wi34rd - avatar