[Python-Dev] Change in Python 3's "round" behavior

Steven D'Aprano steve at pearwood.info
Sun Sep 30 08:17:03 EDT 2018


On Sat, Sep 29, 2018 at 09:40:03PM -0400, Alex Walters wrote:

> ...and we have a stats module that would be a great place for a round
> function that needs to cancel rounding errors.

This has nothing to do with statistics.

You should consider that this is often called "Banker's Rounding" and 
what that tells you. (It's also called Dutch Rounding.)


> The simple case should be the intuitive case for most users.

Should it? I think that having the most correct behaviour should be the 
default.

Who decides what is "intuitive"?

I asked my three year old nephew whether 1.5 should round to down to 1 
or up to 2, and he said that he didn't care about numbers because he 
was sailing across the ocean and I was standing in the way of his boat.


> My experience and that of many users of
> the python irc channel on freenode is that round-half-to-even is not the
> intuitive, or even desired, behavior - round-half-up is.

It would be very informative to ask *why* they want round-half-up.

I expect that the reason given will boil down to "because it is the 
rounding method I learned in school" even if they can't articulate it 
that way, and start going on about it being "intuitive" as if rounding 
ties upwards was more intuitive than rounding ties downward.

Compatibility with "other languages" isn't the answer, because other 
languages differ in how they do rounding and we can't match them all:

# Javascript
js> Math.round(2.5) + Math.round(-2.5)
1

# Ruby
steve at orac ~ $ ruby -e 'puts (2.5).round() + (-2.5).round()'
0


VBScript is another language which uses Bankers Rounding:

https://blogs.msdn.microsoft.com/ericlippert/2003/09/26/bankers-rounding/

although the example given (calculating an average) is misleading, 
because as I said this is not about statistics. Bankers Rounding 
produces better *averages* because it produces better *sums* (to quote 
one of the comments).

Similarly for differences. If you perform many subtractions (let's say 
you are paying off a loan, and calculating interest, then rounding to 
the nearest cent) you have to care about bias. If each rounding 
introduces a 0.5 cent bias (as round-half-up does) then the total bias 
increases as the number of transactions increases.


> This wouldn't be frustrating to the human user 

Did you intend to imply I'm not human, or was it an accident?


-- 
Steve


More information about the Python-Dev mailing list