negative integer division

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Imbaud Pierre

    negative integer division

    integer division and modulo gives different results in c and python,
    when negative numbers
    are involved. take gdb as a widely available c interpreter
    print -2 /3
    0 for c, -1 for python.
    more amazing, modulos of negative number give negative values! (in c).
    from an algebraic point of view, python seems right, but I thought
    python conformity to the underlying c compiler was a strong commitment,
    obviously not here, and I found no explanation whatsoever in python doc.
    no actual programming challenge for me here, I just had this bug, after
    confidently translating from python to c.

  • Skip Montanaro

    #2
    Re: negative integer division


    Imbaud> integer division and modulo gives different results in c and
    Imbaud> python, when negative numbers are involved.



    Skip

    Comment

    • Robert Kern

      #3
      Re: negative integer division

      Imbaud Pierre wrote:[color=blue]
      > integer division and modulo gives different results in c and python,
      > when negative numbers
      > are involved. take gdb as a widely available c interpreter
      > print -2 /3
      > 0 for c, -1 for python.
      > more amazing, modulos of negative number give negative values! (in c).
      > from an algebraic point of view, python seems right, but I thought
      > python conformity to the underlying c compiler was a strong commitment,
      > obviously not here, and I found no explanation whatsoever in python doc.
      > no actual programming challenge for me here, I just had this bug, after
      > confidently translating from python to c.[/color]

      I don't think there's much *commitment* to follow the behaviour of your
      C compiler. It's more of a default stance when the issues get too
      complicated for Python to implement itself. Thus, much of the floating
      point behaviour defaults to the behaviour of your platform because
      floating point math is hard and getting consistent behaviour on lots of
      platforms is really hard.

      Integers are a piece of cake, relatively speaking, and I think that it's
      worth fixing up a few warts from the C behaviour.

      As for documentation, see


      --
      Robert Kern
      rkern@ucsd.edu

      "In the fields of hell where the grass grows high
      Are the graves of dreams allowed to die."
      -- Richard Harter

      Comment

      • Scott David Daniels

        #4
        Re: negative integer division

        Imbaud Pierre wrote:[color=blue]
        > integer division and modulo gives different results in c and python,
        > when negative numbers
        > are involved. take gdb as a widely available c interpreter
        > print -2 /3
        > 0 for c, -1 for python.
        > more amazing, modulos of negative number give negative values! (in c).
        > from an algebraic point of view, python seems right, but I thought
        > python conformity to the underlying c compiler was a strong commitment,
        > obviously not here, and I found no explanation whatsoever in python doc.
        > no actual programming challenge for me here, I just had this bug, after
        > confidently translating from python to c.
        >[/color]
        If you read the C standard, you can see that the C compiler is free to
        make -1 / 3 either -1 or 0 (but it must describe what it does). The
        decision is usually to do whatever the underlying CPU does. The
        rationale is that often you will be dividing positive by positive,
        and you don't want the compiler sprinkling around a lot of test code.

        While the difference in compiled C is a substantial time penalty
        (DIVIDE vs. DIVIDE; COMPARE; JUMPGE), in Python the trade-off is
        adding two instructions to many (50? 100?). Python doesn't really
        mind paying for a few extra instructions to get the canonical value,
        and the guarantee simplifies the user's code.

        --Scott David Daniels
        Scott.Daniels@A cm.Org

        Comment

        • Mark Jackson

          #5
          Re: negative integer division

          Imbaud Pierre <pierre.imbaud@ wanadoo.fr> writes:[color=blue]
          > integer division and modulo gives different results in c and python,
          > when negative numbers
          > are involved. take gdb as a widely available c interpreter
          > print -2 /3
          > 0 for c, -1 for python.
          > more amazing, modulos of negative number give negative values! (in c).
          > from an algebraic point of view, python seems right, but I thought
          > python conformity to the underlying c compiler was a strong commitment,[/color]

          AIUI the C standard is silent on the issue, and hence the C behavior is
          implementation-dependent. Anyway back in 2000 I found and fixed a
          Y2K-related problem in an open-source C program (xvtdl) which was down
          to precisely this misbehavior. While diagnosing the problem I
          implemented the algorithm in Python for test purposes, and was led
          astray for a while by the fact that it *didn't* fail!

          A: 42

          Q: What multiple of 7 did I add to the critical expression in the Zeller
          algorithm so it would remain nonnegative for the next few centuries?

          --
          Mark Jackson - http://www.alumni.caltech.edu/~mjackson
          People who write obscurely are either unskilled in writing
          or up to mischief. - Sir Peter Medawar


          Comment

          • Jive Dadson

            #6
            Re: negative integer division

            Python does it right. C is allowed to do it anyway it likes, which was
            a stupifyingly horrible decision, IMHO.

            Way back when, there was a language named Pascal. I lobbied the Pascal
            standards committee to define the modulus operator correctly, which they
            eventually did. To my astonishment, they proceded to define the
            division operator backwards to the modulus operator, so that division
            and mod did not play together correctly. Duh.

            Comment

            • Dan Bishop

              #7
              Re: negative integer division

              Mark Jackson wrote:[color=blue]
              > Imbaud Pierre <pierre.imbaud@ wanadoo.fr> writes:[color=green]
              > > integer division and modulo gives different results in c and[/color][/color]
              python,[color=blue][color=green]
              > > when negative numbers
              > > are involved. take gdb as a widely available c interpreter
              > > print -2 /3
              > > 0 for c, -1 for python.
              > > more amazing, modulos of negative number give negative values! (in[/color][/color]
              c).[color=blue][color=green]
              > > from an algebraic point of view, python seems right, but I thought
              > > python conformity to the underlying c compiler was a strong[/color][/color]
              commitment,[color=blue]
              >
              > AIUI the C standard is silent on the issue, and hence the C behavior[/color]
              is[color=blue]
              > implementation-dependent.[/color]

              The *original* C standard is silent on the issue, but the current
              standard requires the wrong behavior.

              Comment

              • Mike Meyer

                #8
                Re: negative integer division

                Jive Dadson <jdadson@yahoo. com> writes:
                [color=blue]
                > Python does it right. C is allowed to do it anyway it likes, which was
                > a stupifyingly horrible decision, IMHO.[/color]

                C only does it wrong if you think that C is a high level language. It
                isn't - it's a portable assembler. As such, low level things (like
                this, or what happens on integer overflow, or ...) are left up to the
                implementation, so it can do what's most natural for the underlying
                hardware. This means that when you don't care - which I'd argue is
                most of the time - you get the fastest thing the machine will do. When
                you do care, you have to take care of it yourself. Of course, if you
                care, you probably shouldn't be writing in assembler, you should
                probably be writing in a high level language - which will make sure
                the low level things get done right, irregardless of what the
                underlying machine does.

                Now, I'll agree with you if you want to argue that some machines do
                negative integer division in stupifyingly horrible ways.

                <mike
                --
                Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
                Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

                Comment

                • Jive Dadson

                  #9
                  Re: negative integer division



                  Mike Meyer wrote:[color=blue]
                  >
                  > Jive Dadson <jdadson@yahoo. com> writes:
                  >[color=green]
                  > > Python does it right. C is allowed to do it anyway it likes, which was
                  > > a stupifyingly horrible decision, IMHO.[/color]
                  >
                  > C only does it wrong if you think that C is a high level language.[/color]

                  I didn't say it does it wrong. I said it does it anyway it likes --
                  maybe right, maybe wrong. There *is* a right way, IMHO. Python does it
                  that way.
                  [color=blue]
                  > [C] isn't - it's a portable assembler.[/color]

                  I've heard that many times, but it makes no sense to me. By definition,
                  the syntax of an assembly language closely resembles the format of
                  individual hardware instructions for a particular processor. An
                  assembler assembles individual hardware instructions. Back in the day,
                  Unix (written largely in C) and Steve Johnson's pcc (the *portable* C
                  compiler) together constituted a big leap forward. Implementing Unix on
                  new processors was infinitely easier than porting OS's written in
                  assembly language. So I disagree on two counts: C code is not entirely
                  portable, (for example, division and mod may not work the same on two
                  different machines), and a C compiler is most certainly not an
                  assembler.

                  [color=blue]
                  > Now, I'll agree with you if you want to argue that some machines do
                  > negative integer division in stupifyingly horrible ways.[/color]

                  That's why I think it was a stupifyingly horrible decision.
                  Understandable, but in the end an s.h.d. nonetheless. It would have
                  been okay to define / and % correctly, in the mathematical sense, but
                  also provide functions quick_div() and quick_mod() that were guaranteed
                  to work correctly only when both arguments were positive. The way they
                  did it is just too error prone, akin to early optimization. It's bitten
                  me before, when I was called on to port code (which I did not write)
                  from one machine to another. Having standard operators with
                  under-defined behavior is just inviting trouble: long debugging
                  sessions, or worse, unexplained failures in the field. Of course you
                  and I would avoid all the pitfalls at the start. :-)

                  .... and now back to your regularly scheduled Python newsgroup.

                  Comment

                  • Grant Edwards

                    #10
                    Re: negative integer division

                    On 2005-02-09, Jive Dadson <jdadson@yahoo. com> wrote:
                    [color=blue][color=green]
                    >> [C] isn't - it's a portable assembler.[/color]
                    >
                    > I've heard that many times, but it makes no sense to me.[/color]

                    I think the point is that C is a low-level, hardware twiddling
                    language to be used by people writing things like kernel code --
                    something that was always done in assembler before C came
                    along.
                    [color=blue][color=green]
                    >> Now, I'll agree with you if you want to argue that some
                    >> machines do negative integer division in stupifyingly horrible
                    >> ways.[/color]
                    >
                    > That's why I think it was a stupifyingly horrible decision.[/color]

                    For a language meant to write user-space applications where one
                    probably cares what happens when a division results in a
                    negative integer, it is a horrible decision. For a "portable
                    assembler" used to write device drivers it makes sense. People
                    writing that sort of code presumably know how their hardware
                    behaves, don't expect that everything write is portable, and
                    just don't do division with negative numbers. When they do
                    division, it's with postive numbers and they don't want to
                    waste the extra clock cycles to do it in a way that's
                    machine-independant for negative numbers.

                    The fact that C ended up in the rather inappropriate role of
                    a user-land application language is different problem.

                    --
                    Grant Edwards grante Yow! ... I think I'd
                    at better go back to my
                    visi.com DESK and toy with a few
                    commonMISAPPREH ENSIONS...

                    Comment

                    • Jive Dadson

                      #11
                      Re: negative integer division



                      Grant Edwards wrote:[color=blue]
                      >
                      > On 2005-02-09, Jive Dadson <jdadson@yahoo. com> wrote:
                      >[color=green][color=darkred]
                      > >> [C] isn't - it's a portable assembler.[/color]
                      > >
                      > > I've heard that many times, but it makes no sense to me.[/color]
                      >
                      > I think the point is that C is a low-level, hardware twiddling
                      > language to be used by people writing things like kernel code --[/color]

                      And Python interpreters?
                      [color=blue]
                      >
                      > The fact that C ended up in the rather inappropriate role of
                      > a user-land application language is different problem.[/color]

                      In the early 80's, either C was the "appropriat e language" or there was
                      none ... and that's coming from someone who wrote a commercial
                      Pascal compiler, runtime support, and debugger. I did it all in C.
                      Pascal, as we all know, was ill-conceived. C++ was a momentous
                      advance, but it intensionally inherited many of C's warts.

                      I've forgotten what we are arguing about, but I'm sure I'm right.

                      J.

                      Comment

                      • Jive Dadson

                        #12
                        Re: negative integer division

                        intentionally

                        Comment

                        • Grant Edwards

                          #13
                          Re: negative integer division

                          On 2005-02-09, Jive Dadson <jdadson@yahoo. com> wrote:
                          [color=blue][color=green][color=darkred]
                          >>>> [C] isn't - it's a portable assembler.
                          >>>
                          >>> I've heard that many times, but it makes no sense to me.[/color]
                          >>
                          >> I think the point is that C is a low-level, hardware twiddling
                          >> language to be used by people writing things like kernel code --[/color]
                          >
                          > And Python interpreters?[/color]

                          No. That's my point. C isn't a good language in which to write
                          user applications such as a Python language. One uses C when
                          the other choice is assembly, not when the other choice is
                          "real" high level language like Python, or Modula-3, or
                          Smalltalk, or whatever.
                          [color=blue][color=green]
                          >> The fact that C ended up in the rather inappropriate role of
                          >> a user-land application language is different problem.[/color]
                          >
                          > In the early 80's, either C was the "appropriat e language" or
                          > there was none ... and that's coming from someone who wrote a
                          > commercial Pascal compiler, runtime support, and debugger. I
                          > did it all in C. Pascal, as we all know, was ill-conceived.[/color]

                          I never thought so. I did embedded systems development using
                          Pascal and quite enjoyed it. Later when Pascal waned and C
                          waxed, I thought C was a definite step backwards. I thought
                          Modula-2 and Modula-3 were both good languages as well.
                          [color=blue]
                          > C++ was a momentous advance,[/color]

                          Now C++ _was_ ill-conceived. It's more of an agglomeration of
                          features than Perl. And dangerous to boot: at least Perl tries
                          to protect you from memory leaks and stray pointers.
                          [color=blue]
                          > but it intensionally inherited many of C's warts.[/color]

                          And added a bunch of it's own.

                          This is pretty much completely off-topic now. :)

                          --
                          Grant Edwards grante Yow! I'm definitely not
                          at in Omaha!
                          visi.com

                          Comment

                          • Grant Edwards

                            #14
                            Re: negative integer division

                            On 2005-02-09, Jive Dadson <jdadson@yahoo. com> wrote:
                            [color=blue]
                            > intentionally[/color]

                            I disagree!

                            --
                            Grant Edwards grante Yow! ... I don't like
                            at FRANK SINATRA or his
                            visi.com CHILDREN.

                            Comment

                            • Peter Hansen

                              #15
                              Re: negative integer division

                              Grant Edwards wrote:[color=blue]
                              > This is pretty much completely off-topic now. :)[/color]

                              No discussion of how lame other languages are is ever
                              completely off-topic in comp.lang.pytho n. After all,
                              these discussions continue to remind us how lucky we
                              all are to be able to program in Python, and that
                              can only be a good thing. ;-)

                              -Peter

                              Comment

                              Working...