[Tutor] Python 2.5.4 - error in rounding

Steven D'Aprano steve at pearwood.info
Sat May 22 14:30:57 CEST 2010


On Sat, 22 May 2010 07:51:31 am Alan Gauld wrote:
> "Neven Gorsic" <neven.gorsic at gmail.com> wrote
>
> > I run into Python error in rounding and not know how to predict
> > when it will
> > occur in order to prevent wrong result.
>
> It depends how you define wrong. When I was at scvhool the rules f
> or rounding decimals said that if it ended in 5 you rounded to the
> nearest even digit.
> So 0.45 -> 0.4 and 0.55 ->0.6

That's called banker's rounding, and if you perform many calculations, 
on average it leads to the smallest rounding error.

> But you seem to assume a 5 always rounds up...

That's how I was taught to do rounding in school too.



-- 
Steven D'Aprano


More information about the Tutor mailing list