469,603 Members | 2,057 Online

# Speaking of rounding errors...

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 IMHO
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. 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.

Jul 18 '05 #1
2 1241
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
IMHO
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 && ma*@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)
Jul 18 '05 #2
On Fri, 2 Jul 2004, Erik Max Francis wrote:
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.

Perhaps we should all just switch to balanced ternary, giving us two
possible representations of 1/2 (one rounding up, one rounding down) ;)

Jul 18 '05 #3

### This discussion thread is closed

Replies have been disabled for this discussion.