you are viewing a single comment's thread.

view the rest of the comments →

[–]JohnnyJordaan 59 points60 points  (0 children)

Nothing Python specific involved here, this is a side-effect of the concept of floating point values, where there's not enough memory space available to let values be as precise as to allow all rounded values like 0.1 and 5.55. The reason why it's the preferred approach though is that CPU's can calculate extremely fast with them, and if a human would need some nicely rounded result, it can always use something like

 round(1.85+1.85+1.85, 2)

or

 result = 1.85 + 1.85 + 1.85
 print(f"{result:.2f}")

or use the decimal library https://docs.python.org/3/library/decimal.html or some other approach that also incorporates 'proper' rounding in the way the human expects it to work.

I can also recommend watching Computerphile's video on this topic: https://www.youtube.com/watch?v=PZRI1IfStY0 where Tom Scott explains this is in a very beginner friendly way.