[Python-ideas] division oddness

Chris Rebert clp2 at rebertia.com
Thu May 6 22:14:41 EDT 2010


On Thu, May 6, 2010 at 6:33 PM, Xavier Ho <contact at xavierho.com> wrote:
> On Fri, May 7, 2010 at 11:13 AM, Chris Rebert <pyideas at rebertia.com> wrote:
>> On Thu, May 6, 2010 at 5:43 PM, Mathias Panzenböck
>> <grosser.meister.morti at gmx.net> wrote:
>> > Shouldn't by mathematical definition -x // y be the same as -(x // y)?
>> > I think this rather odd. Is there any deeper reason to this behaviour? I
>> > guess changing this will break a lot of code, but why does it behave
>> > like
>> > this?
>>
>> I would suppose it's what programmers have found more
>> useful/intuitive. Most programmers aren't number theorists.
>
> Yeah, I was stinged by that before, too. I really don't find it intuitive at
> all - to keep the modulos remainer equal, you lose on negations and possibly
> integer equality. Ah well, too bad I didn't invent Python. =].

Personally, I find the following the most unintuitive:
divmod(-11, 3) == (-4, 1)

So, we overshoot -11 and then add 1 to go back to the right place?
That violates my intuitive thought that abs((n//d)*d) <= abs(n) ought to hold.

Cheers,
Chris
--
This is why we don't rely on intuition alone.
http://blog.rebertia.com



More information about the Python-list mailing list