[Tutor] The '45' bug in round()
Terry Carroll
carroll at tjc.com
Tue Mar 20 22:24:03 CET 2007
On Tue, 20 Mar 2007, Dick Moores wrote:
> >>> print round(0.19965, 4)
> 0.1997
>
> (which rounds up to an odd number, 7)
Now that's weird. It should (I say) round to .1996; not because 6 is even
and 7 is not[1], but because 0.19965 is actually closer to 0.1996 than to
0.1997:
>>> 0.19965
0.19964999999999999
[1] The Python rounding rule is to round "away from zero," i.e., to round
up for positive numbers and down (where "down" means "more negative") for
negative numbers.
More information about the Tutor
mailing list