RE: Integer division, surprising results

Discussion in 'Python' started by Tim Peters, May 10, 2004.

  1. Tim Peters

    Tim Peters Guest

    [Michael Cornelius]
    > As an old C programmer, I'm surprised by some results I'm getting with
    > integer division. For example:
    >
    > >>> -1/1000

    > -1


    As an old C programmer, you should have known that the C standard (C89)
    didn't define whether the result here was -1 or 0.

    > >>> -9/2

    > -5


    Likewise C didn't define whether this was -5 or -4.

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


    Most C compilers do generate code to return those.

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


    It's primarily driven by the desire that i%j have the same sign as j. If
    you want that, and also want

    i == (i/j)*j + (i%j)

    then integer division has to return the floor. C also requres that identity
    to hold, and then compilers that truncate i/j need to make i%j have the same
    sign as i.

    There are few real use cases for i%j when j is negative. When j is
    positive, there are many, and in virually all of them it's more useful for
    i%j to be >= 0 (if the clock says 10 now, what did it say 200 hours
    go? -190 % 12 == 2 is useful; -190 % 12 == -10 is a bug waiting to bite).
    Tim Peters, May 10, 2004
    #1
    1. Advertising

  2. "Tim Peters" <> writes:

    > It's primarily driven by the desire that i%j have the same sign as j. If


    That's the side effect, not the desire (AFAIK).

    > you want that, and also want
    >
    > i == (i/j)*j + (i%j)
    >
    > then integer division has to return the floor. C also requres that identity


    AFAIK the history is the other way around: The Unix+C-philosophy was
    essentially the "don't care"-approach, i.e. '/' does just what the CPU
    happens to do if it sees a divide instruction. Most machines seem to
    truncate here, therefore '%' can give negative results. This rarely
    disturbs anyone, since most people don't divide negative numbers.
    Those who do (e.g. mathematicians) fall outside the "good for 90% is
    good enough"-attitude and have to help themselves.

    Scheme and Lisp *do* care (offering both "remainder" (the easily
    implemented thing) and "modulo" (the real thing)), as does Python with
    its '%'[1]. (Called "rem" and "mod" in Lisp).

    [1] I don't know whether it also offers a shortcut for
    sign(x)*mod(abs(x),y) for people who insist on the bad result.

    Ralf
    --
    GS d->? s:++>+++ a+ C++++ UL+++ UH++ P++ L++ E+++ W- N++ o-- K- w--- !O M- V-
    PS+>++ PE Y+>++ PGP+ !t !5 !X !R !tv b+++ DI+++ D? G+ e++++ h+ r? y?
    Ralf Muschall, May 12, 2004
    #2
    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,859
  2. genlock
    Replies:
    22
    Views:
    25,929
    vipinlal
    Mar 17, 2010
  3. Michael Cornelius

    Integer division, surprising results

    Michael Cornelius, May 10, 2004, in forum: Python
    Replies:
    2
    Views:
    351
    Steve
    May 12, 2004
  4. Brad Tilley

    results of division

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

Share This Page