Speaking of rounding errors...

  • Thread starter Christopher T King
  • Start date
C

Christopher T King

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

Erik Max Francis

Christopher said:
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.
 
C

Christopher T King

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) ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top