Integer division, surprising results

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

  1. 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
    #1
    1. Advertising

  2. > 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
    #2
    1. Advertising

  3. Michael Cornelius

    Steve Guest

    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
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    12
    Views:
    1,873
  2. genlock
    Replies:
    22
    Views:
    25,994
    vipinlal
    Mar 17, 2010
  3. Tim Peters
    Replies:
    1
    Views:
    354
    Ralf Muschall
    May 12, 2004
  4. Brad Tilley

    results of division

    Brad Tilley, Dec 9, 2004, in forum: Python
    Replies:
    14
    Views:
    504
    Peter Hansen
    Dec 10, 2004
  5. Replies:
    94
    Views:
    4,487
    ┬Ča\\/b
    Feb 9, 2007
Loading...

Share This Page