Rounding curiosity

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Tue Nov 16 23:12:33 EST 2004


PyBo wrote:

> What's wrong with this picture?
> 
>>>> x = 500000000.0
>>>> y = x / (1024 * 1024)
>>>> y
> 476.837158203125
>>>> z = round(y, 2)
>>>> z
> 476.83999999999997
> 
> I have tried this and get the same result using different CPU's and
> operating systems. Obviously, '476.83999999999997' is not rounded to
> two decimal places. 
> 
> Or am I doing something wrong?

You just have a fundamental misunderstanding of floating-point
arithmetic.

http://www.python.org/doc/faq/general.html#why-are-floating-point-calcul
ations-so-inaccurate

Tim Delaney



More information about the Python-list mailing list