[Tutor] Is something wrong with my round() function?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Jan 20 13:41:12 EST 2004



On Tue, 20 Jan 2004, hcohen2 wrote:

> The obvious answer is 'YES', but what's causing this strange behaviour?
>
>  >>> ans = 48.936
>  >>> round(ans, 1)
> 48.899999999999999
>  >>> round(ans, 2)
> 48.939999999999998
>  >>> round(ans, 3)
> 48.936
>  >>> round(ans, 4)
> 48.936
>  >>> round(ans, 2)
> 48.939999999999998
>  >>> round(ans, 1)
> 48.899999999999999
>  >


Hello!


Lloyd mentioned that it's the representation of the numbers as "floating
point" that's causing what looks to be erroneous output.  The Python
Tutorial also talks about this issue, since it pops up every once in a
while:

    http://www.python.org/doc/tut/node15.html


Hope this helps!




More information about the Tutor mailing list