# Integer division, surprising results

Discussion in 'Python' started by Michael Cornelius, May 10, 2004.

1. ### Michael CorneliusGuest

As an old C programmer, I'm surprised by some results I'm getting with
integer division. For example:

>>> -1/1000

-1
>>> -9/2

-5

I expect the results of these expressions to be 0 and -4,
respectively.

I've looked at faqs and documentation, and nothing jumped out at me. Can
anyone explain the reasoning for this?

Thanks,
Michael Cornelius

Michael Cornelius, May 10, 2004

2. ### Rory GeogheganGuest

> I've looked at faqs and documentation, and nothing jumped out at me. Can
> anyone explain the reasoning for this?

According to my math book integer division is defined as such:
a/b = q, where a = b*q + r and r is the remainder.

A remainder (ei a modulo) is always positive, effectively flooring all
the division operations.

Rory Geoghegan, May 10, 2004

3. ### SteveGuest

Rory Geoghegan wrote:
>>I've looked at faqs and documentation, and nothing jumped out at me. Can
>>anyone explain the reasoning for this?

>
>
> According to my math book integer division is defined as such:
> a/b = q, where a = b*q + r and r is the remainder.
>
> A remainder (ei a modulo) is always positive, effectively flooring all
> the division operations.

Remainders are always positive, but modulo can be
defined as either positive or negative (for negative
arguments). There are pros and cons for doing it either
way, although the Python way seems to have more pros
than cons.

--
Steven D'Aprano

Steve, May 12, 2004