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

Steven D'Aprano steve at pearwood.info
Thu Sep 27 01:29:59 EDT 2018


On Wed, Sep 26, 2018 at 07:26:17AM -0400, jab at math.brown.edu wrote:

> I did find the revealingly-invalid bug report
> https://bugs.python.org/issue32956 ("python 3 round bug"), so I asked
> there, but wanted to invite anyone else on this list who might be
> interested to help. 

What about those of us who are interested in hindering?

But seriously, even if round-to-even was a mistake, Python 3.x has used 
it for seven releases now, about a decade. Backwards compatibility means 
we cannot just change it. By now, there are people relying on this 
behaviour. Changing it would need to go through a deprecation cycle, 
which probably means one release with a silent warning, a second release 
with warning enabled, and not until 3.10 would the default change.

That's a lot of inconvenience just for the sake of almost-but-not-quite 
matching the behaviour of some other programming languages, while 
breaking compatibility with others:

julia> round(2.5)
2.0
julia> round(3.5)
4.0

In any case, I would oppose any proposal to revert this change. Round- 
to-even ("banker's rounding") is generally mathematically better, and 
its been said (half in jest) that if you're not using it, you're 
probably up to shenanigans :-)

For users who don't specifically care about the rounding mode, round-to- 
even generally makes the safest default, even if it is surprising to 
those used to the naive technique taught in primary schools. For those 
who care about compatibility with some other language, well, there are a 
lot of languages and we can't match them *all* by default:

# Javascript
js> Math.round(-2.5)
-2

# Ruby
irb(main):001:0> (-2.5).round()
=> -3

so you probably need your own custom round function. On the other hand, 
I wouldn't object out of hand to a feature request to support the same 
eight rounding modes as the decimal module. But as always, the Devil is 
in the details.


-- 
Steve


More information about the Python-Dev mailing list