[Python-ideas] Round division
Andrew Barnert
abarnert at yahoo.com
Fri Sep 11 05:25:52 CEST 2015
On Sep 10, 2015, at 20:13, Steven D'Aprano <steve at pearwood.info> wrote:
>
>> 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.
His rounds based on the exact integer remainder; yours rounds based on the inexact float fractional part. So, if b is large enough, using round division is guaranteed to do the right thing,[1] but rounding float division may have rounding, overflow, or underflow errors.
[1] Except I'm pretty sure he wants to compare r*2 to b, not r to b/2. Otherwise he's reintroduced the problem he's trying to solve.
More information about the Python-ideas
mailing list