0

Reading about floating point arithmetic in python, I was trying some examples. To explain myself why print 0.1+0.2 doesn't give 0.30000000000000004, from the docs on print statement, I read that:

print evaluates each expression in turn and writes the resulting object to standard output (see below). If an object is not a string, it is first converted to a string using the rules for string conversions. The (resulting or original) string is then written.

Why does string conversion and hence the print statement too, rounds 0.30000000000000004 to 0.3? Does this not make it difficult to know what floating point values my program is using, or how to know it?

Example:

In [17]: 0.1 + 0.2
Out[17]: 0.30000000000000004
In [18]: print 0.1 + 0.2
         0.3
In [20]: str(0.1 + 0.2)
Out[20]: '0.3'
devautor
  • 2,506
  • 4
  • 21
  • 31

0 Answers0