[Python-ideas] Round division

Steven D'Aprano steve at pearwood.info
Fri Sep 11 05:13:04 CEST 2015

On Fri, Sep 11, 2015 at 01:39:59AM +0300, Serhiy Storchaka wrote:
> On 11.09.15 00:48, Paul Moore wrote:
> >On 10 September 2015 at 22:13, Mark Young <marky1991 at gmail.com> wrote:
> >>Pardon my ignorance, but what is the definition of round division? (if it
> >>isn't "round(a/b)")
> >
> >I assumed it would be "what round(a/b) would give if it weren't
> >subject to weird floating point rounding issues". To put it another
> >way, if a / b is d remainder r, then I'd assume "round division" would
> >be d if r < b/2, d+1 if r > b/2, and (which of d, d+1?) if r == b/2.
> >(a, b, d and r are all integers).
> >
> >If not, then I also would like to know what it means...
> Yes, it is what you have described. If r == b/2, the result is even 
> (i.e. (d+1)//2*2).

How does this differ from round(a/b)? round() also rounds to even.

Perhaps a more general solution would be a round-to-direction, or 
divide-and-round-to-direction. Now that we have Enums, we could define 
enums for round-to-zero, round-to-nearest, round-to-infinity, 
round-to-even, and have a function divide(a, b, dir=ROUNDEVEN), say.


More information about the Python-ideas mailing list