469,603 Members | 2,057 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,603 developers. It's quick & easy.

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.

Similar topics

20 posts views Thread by Raoul Watson | last post: by
6 posts views Thread by Jeff Boes | last post: by
9 posts views Thread by Joe Attardi | last post: by
6 posts views Thread by shaqattack1992-newsgroups | last post: by
29 posts views Thread by Marco | last post: by
3 posts views Thread by gabo22003 | last post: by
206 posts views Thread by md | last post: by
30 posts views Thread by bdsatish | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.