Python uses a minimalist repr
for float
s when possible. That is, it prints a value that is as precise as necessary to reproduce the exact value of the original float
if you typed it in directly.
For 1.1 + 2.2
, the imprecision means it doesn't actually produce 3.3
precisely (1.1 + 2.2 == 3.3
will evaluate to False
). By contrast, 4 / (2.0 + 3)
is exact enough that 0.8
represents it precisely, (4 / (2.0 + 3) == 0.8
evaluates to True
) so it doesn't include a whole bunch of 0
s that aren't required to reproduce the value.
If you want to explicitly print a certain number of places after the decimal, use str
formatting to specify, e.g.:
>>> print('{:.16f}'.format(4 / (2.0 + 3)))
0.8000000000000000
If you formatted it with additional digits, you'd see the 4
, but that value is still exactly the same as 0.8
; 0.80000000000000004 == 0.8
evaluates to True
, so Python avoids the verbosity and just gives you shortest of many equivalent representations.
The What's new in Python 3.1 docs (scroll to end of linked section, just before "New, Improved and Deprecated Modules") are a useful explanation for why/when Python 2.7/3.1+ have much shorter float
repr
s for some values. Straight from the horse's mouth, so to speak.