Rounding curiosity

PyBo someone at somewhere.com
Wed Nov 17 07:13:33 CET 2004


Grant Edwards wrote:
> On 2004-11-17, PyBo <bartley9 at pacbell.net> wrote:
> 
> 
>>What's wrong with this picture?
> 
> 
> Wrong is in the mind of the beholder.  ;)
> 
> 
>>>>>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.
> 
> 
> There's a reason for that:
> 
>   http://www.python.org/doc/faq/general.html#why-are-floating-point-calculations-so-inaccurate
>   http://docs.python.org/tut/node15.html
> 
> 
>>Obviously, '476.83999999999997' is not rounded to two decimal
>>places.
> 
> 
> Of course it isn't.  Computers don't use decimal.  They use
> binary: aye, there's the rub...
> 
> 
>>Or am I doing something wrong?
> 
> 
> You're using binary floating point math without understanding
> it.  If you tell us what you're actually trying to accomplish,
> we can probably tell you how to do it.  
> 
> If you're just worried about how it looks on the screen:
> 
> 
>>>>print "%0.2f" % round(5e8/(1024*1024),2)
> 
> 476.84
> 
> If you want the computer to fib for you, it will. :)
> 
To all those who are feeling so smug and superior, perhapts you could 
have noticed that I was pointing out that the round() function does not
perform as documented?

By the way,
   print "%0.2f" % (5e8/(1024*1024))
yields the same result as
   print "%0.2f" % round(5e8/(1024*1024),2)
so the 'round()' portion of the expression is superfluous.




More information about the Python-list mailing list