Speaking of rounding errors...

Erik Max Francis max at alcyone.com
Sat Jul 3 07:25:47 CEST 2004

Christopher T King wrote:

> Speakign of rounding errors, why is round() defined to round away from
> zero? I've always been quite fond of the mathematical definition of
> rounding of floor(n+.5). As a contrived example of why the latter is
> better, I present the following:
> for x in [x+.5 for x in xrange(-10,10)]:
>         print int(round(x))
> This prints -10, -9, ..., -2, -1, 1, 2, ..., 9, 10 (it skips zero),
> probably not what you'd expect.

Probably because the conventional meaning of "rounding" is usually only
applied to nonnegative numbers, and most people don't care what happens
with negative numbers.  When they _do_ care, they're likely to specify
the exact rounding behavior they want to remove all doubt, and not use
the round function.

> I'm not sure how often a case like
> this
> comes up in real usage, but I imagine it's more often than a case
> relying
> on the current behaviour would.

Well, for one thing, if you changed what it did now, it would break
anybody using the function for negative numbers now.  It's mostly
arbitrary what it does for negative numbers, and once that arbitrariness
has been solidified with a concrete choice, it makes less and less sense
to change it as time goes on.

 __ Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
/  \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ The work of many cannot be done alone.
    -- (a Bemba proverb)

More information about the Python-list mailing list