Python Problem II | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Python Problem II

Can any one explain why floating point calculations are so inaccurate? This is because I discovered something: I just add an float to an another float just like this: print(1.2 - 1.0) But instead of displaying 0.2, why output is 0.19999999999999996? And situations are the same even I make it more specific, lets say: print(1.200000 - 1.000000) Why 0.19999999999999996 is still the output? Thanks for any response that you make.

7th Jul 2020, 8:11 AM
James Clark I. Vinarao
James Clark I. Vinarao - avatar
4 Answers
0
No. That's not a bug. It's nature of binary floating point numbers. So that's why it is not advisable to relay of floating point numbers in large codes because that messy thing. Edit: James Clark I. Vinarao This will explain in more details, take a look https://docs.python.org/3/tutorial/floatingpoint.html#tut-fp-issues)
7th Jul 2020, 8:43 AM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
+ 1
When python stores floating-point numbers it uses binary instead of the way we do it which is Decimal
8th Jul 2020, 6:48 AM
Jeremy Cheung
Jeremy Cheung - avatar
0
decimal is when we get to ten itโ€™s two digit, but binary is when u count to two it gets to ten like this: 1,10,11,20,21
8th Jul 2020, 6:51 AM
Jeremy Cheung
Jeremy Cheung - avatar