Rounding curiosity

Tim Peters tim.peters at gmail.com
Tue Nov 16 23:13:36 EST 2004


[PyBo]
> What's wrong with this picture?
> 
> >>> x = 500000000.0
> >>> y = x / (1024 * 1024)
> >>> y
> 476.837158203125
> >>> z = round(y, 2)
> >>> z
> 476.83999999999997

What's wrong is that you haven't yet read the Python tutorial's
appendix on floating-point issues.  That much is easily repaired
<wink>:

    http://docs.python.org/tut/node15.html



More information about the Python-list mailing list