Integer math question

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

    Integer math question

    Hi,

    can anybody help with the following problem?

    In C++

    i = 5 / 10 and
    i = -5 / 10 both have the same result 0.

    In python

    i = 5 / 10 gives me 0 as expected, but
    i = -5 / 10 gives -1 as result.

    Is this a feature or a bug? I remember Delphi gave me the same result as
    C++.

    TIA,
    Frank
  • John Roth

    #2
    Re: Integer math question


    "Frank" <mersmann@szut. uni-bremen.de> wrote in message
    news:3987e01c.0 401030832.114c6 f2a@posting.goo gle.com...[color=blue]
    > Hi,
    >
    > can anybody help with the following problem?
    >
    > In C++
    >
    > i = 5 / 10 and
    > i = -5 / 10 both have the same result 0.
    >
    > In python
    >
    > i = 5 / 10 gives me 0 as expected, but
    > i = -5 / 10 gives -1 as result.
    >
    > Is this a feature or a bug? I remember Delphi gave me the same result as
    > C++.[/color]

    That's a feature. Integer division is explicitly defined in
    Python to do exactly that.

    The basic thing to remember is that the *correct*
    mathematical result of dividing one integer by
    another integer is a rational. Python does not
    have a rational data type, so it has to pick one
    of the multitude of possible ways of rounding a
    non-integral result to an integer.

    There is no universally right answer to this process:
    the "right" answer to any rounding problem is
    what the customer wants it to be.

    John Roth
    [color=blue]
    >
    > TIA,
    > Frank[/color]


    Comment

    • Sean Ross

      #3
      Re: Integer math question

      "Frank" <mersmann@szut. uni-bremen.de> wrote in message
      news:3987e01c.0 401030832.114c6 f2a@posting.goo gle.com...[color=blue]
      > Hi,
      >
      > can anybody help with the following problem?
      >
      > In C++
      >
      > i = 5 / 10 and
      > i = -5 / 10 both have the same result 0.
      >
      > In python
      >
      > i = 5 / 10 gives me 0 as expected, but
      > i = -5 / 10 gives -1 as result.
      >
      > Is this a feature or a bug? I remember Delphi gave me the same result as
      > C++.
      >
      > TIA,
      > Frank[/color]


      Using the division algorithm:

      Let a,b be integers with b>0. Then there exist unique integers q and r such
      that:

      a = bq + r and 0<=r<b [1]

      If we let a=5 and b=10, then a/b is represented by [1] as

      a = bq + r
      5 = (10) q + r

      .... skipping some steps, we find that q=0 and r=5
      5 = 10(0) + 5
      5 = 0 + 5
      5 = 5
      LHS = RHS

      so, 5/10 = 0 in integer division (with no remainder).

      Now, let a = -5 and b = 10

      -5 = (10)q + r

      If we have q = -1, then

      -5 = (10)(-1) + r
      -5 = -10 + r

      Recall [1] above, 0 <= r < b. r must be nonnegative.
      In this case r=5,

      -5 = -10 + 5
      -5 = -5
      LHS = RHS

      So, q = -1, and r=5, in which case -5/10 = -1 in integer division (without
      remainder).

      Suppose we let q=0 for the second problem above. Then

      -5 = (10)(0) + r
      -5 = 0 + r
      -5 = r
      or
      r = -5

      But, by [1], r must be nonnegative. So, we have a contradiction. In which
      case we can say that q cannot equal 0 in the algorithm above.

      So, I suppose the answer to your question would be,

      "This is a feature - Python does it properly, where the others do not."

      HTH
      Sean



      Comment

      • Sean Ross

        #4
        Re: Integer math question


        "Sean Ross" <sross@connectm ail.carleton.ca > wrote in message
        news:11EJb.2046 1$Vl6.3782481@n ews20.bellgloba l.com...[color=blue]
        >then a/b is represented by [1] as[/color]

        'represented' is the wrong word here, but hopefully, you get the idea ...



        Comment

        • Sean Ross

          #5
          Re: Integer math question

          Here's an interactive Python example to help clarify the previous response:
          [color=blue][color=green][color=darkred]
          >>> a = 5
          >>> b = 10
          >>> q , r = divmod(a,b)
          >>> q[/color][/color][/color]
          0[color=blue][color=green][color=darkred]
          >>> r[/color][/color][/color]
          5[color=blue][color=green][color=darkred]
          >>> a = -a
          >>> a[/color][/color][/color]
          -5[color=blue][color=green][color=darkred]
          >>> q , r = divmod(a,b)
          >>> q[/color][/color][/color]
          -1[color=blue][color=green][color=darkred]
          >>> r[/color][/color][/color]
          5[color=blue][color=green][color=darkred]
          >>> b*q + r # division algorithm[/color][/color][/color]
          -5[color=blue][color=green][color=darkred]
          >>> -5 == a[/color][/color][/color]
          True[color=blue][color=green][color=darkred]
          >>>[/color][/color][/color]


          Comment

          • Rainer Deyke

            #6
            Re: Integer math question

            Sean Ross wrote:[color=blue]
            > a = bq + r and 0<=r<b [1][/color]

            But 0 <= r < b is a contradiction when b < 0.

            --
            Rainer Deyke - rainerd@eldwood .com - http://eldwood.com


            Comment

            • Sean Ross

              #7
              Re: Integer math question

              "Rainer Deyke" <rainerd@eldwoo d.com> wrote in message
              news:kTEJb.7242 42$HS4.5376202@ attbi_s01...[color=blue]
              > Sean Ross wrote:[color=green]
              > > a = bq + r and 0<=r<b [1][/color]
              >
              > But 0 <= r < b is a contradiction when b < 0.
              >[/color]

              Right. But, the division algorithm states "Let a,b be integers with b>0"
              (which I mentioned in that post).




              Comment

              • Sean Ross

                #8
                Re: Integer math question

                "Rainer Deyke" <rainerd@eldwoo d.com> wrote in message
                news:kTEJb.7242 42$HS4.5376202@ attbi_s01...[color=blue]
                > Sean Ross wrote:[color=green]
                > > a = bq + r and 0<=r<b [1][/color]
                >
                > But 0 <= r < b is a contradiction when b < 0.
                >
                > --
                > Rainer Deyke - rainerd@eldwood .com - http://eldwood.com
                >
                >[/color]

                Hmm....
                [color=blue][color=green][color=darkred]
                >>> a = 5
                >>> b = -10
                >>> q,r = divmod(a,b)
                >>> q[/color][/color][/color]
                -1[color=blue][color=green][color=darkred]
                >>> r[/color][/color][/color]
                -5[color=blue][color=green][color=darkred]
                >>>[/color][/color][/color]

                Here, the division algorithm does not apply (b is a negative integer).
                Perhaps there's some other theorem for this case?
                b<r<=0, when b < 0? I don't know.



                Comment

                • Sean Ross

                  #9
                  Re: Integer math question


                  "Sean Ross" <sross@connectm ail.carleton.ca > wrote in message
                  news:5sFJb.2092 3$Vl6.3818930@n ews20.bellgloba l.com...[color=blue]
                  > "Rainer Deyke" <rainerd@eldwoo d.com> wrote in message
                  > news:kTEJb.7242 42$HS4.5376202@ attbi_s01...[color=green][color=darkred]
                  > >>> a = 5
                  > >>> b = -10
                  > >>> q,r = divmod(a,b)
                  > >>> q[/color][/color]
                  > -1[color=green][color=darkred]
                  > >>> r[/color][/color]
                  > -5[color=green][color=darkred]
                  > >>>[/color][/color]
                  >
                  > Here, the division algorithm does not apply (b is a negative integer).
                  > Perhaps there's some other theorem for this case?
                  > b<r<=0, when b < 0? I don't know.
                  >[/color]

                  I think you're supposed to do something like this
                  a = bq + r, 0<= r < |b|
                  5 = (-10)q + r
                  -5 = -(-10)q - r
                  -5 = 10q - r

                  But, then, q would be 0 and r would be 5. <shrug>



                  Comment

                  • Elaine Jackson

                    #10
                    Re: Integer math question

                    C rounds toward the nearest integer and Python rounds down. The behavior is
                    consistent in each case.

                    "Frank" <mersmann@szut. uni-bremen.de> wrote in message
                    news:3987e01c.0 401030832.114c6 f2a@posting.goo gle.com...
                    | Hi,
                    |
                    | can anybody help with the following problem?
                    |
                    | In C++
                    |
                    | i = 5 / 10 and
                    | i = -5 / 10 both have the same result 0.
                    |
                    | In python
                    |
                    | i = 5 / 10 gives me 0 as expected, but
                    | i = -5 / 10 gives -1 as result.
                    |
                    | Is this a feature or a bug? I remember Delphi gave me the same result as
                    | C++.
                    |
                    | TIA,
                    | Frank


                    Comment

                    • Elaine Jackson

                      #11
                      Re: Integer math question

                      Sorry, I should have said "C rounds toward zero". In other words, the result of
                      casting x to integer type is sgn(x)*floor(ab s(x)).

                      "Elaine Jackson" <elainejackson7 355@home.com> wrote in message
                      news:uEOJb.9337 87$pl3.753391@p d7tw3no...
                      | C rounds toward the nearest integer and Python rounds down. The behavior is
                      | consistent in each case.
                      |
                      | "Frank" <mersmann@szut. uni-bremen.de> wrote in message
                      | news:3987e01c.0 401030832.114c6 f2a@posting.goo gle.com...
                      | | Hi,
                      | |
                      | | can anybody help with the following problem?
                      | |
                      | | In C++
                      | |
                      | | i = 5 / 10 and
                      | | i = -5 / 10 both have the same result 0.
                      | |
                      | | In python
                      | |
                      | | i = 5 / 10 gives me 0 as expected, but
                      | | i = -5 / 10 gives -1 as result.
                      | |
                      | | Is this a feature or a bug? I remember Delphi gave me the same result as
                      | | C++.
                      | |
                      | | TIA,
                      | | Frank
                      |
                      |


                      Comment

                      • Derek Ledbetter

                        #12
                        Re: Integer math question

                        On Sat, 3 Jan 2004 8:32:07 -0800, Frank wrote
                        (in message <3987e01c.04010 30832.114c6f2a@ posting.google. com>):
                        [color=blue]
                        > In C++
                        >
                        > i = 5 / 10 and
                        > i = -5 / 10 both have the same result 0.[/color]

                        Actually this is implementation defined in C89 and standard C++. Either
                        -5/10 == 0 and -5%10 == -5, as in your implementation, or -5/10 == -1
                        and -5%10 == 5, like Python. In C99, and possibly a future version of
                        C++, it's always done the first way (rounding towards zero).

                        --
                        Derek Ledbetter
                        derekl@serve.co m

                        Heavy boots of lead
                        fills his victims full of dread
                        Running as fast as they can
                        Iron Man lives again!


                        Comment

                        • Dan Bishop

                          #13
                          Re: Integer math question

                          mersmann@szut.u ni-bremen.de (Frank) wrote in message news:<3987e01c. 0401030832.114c 6f2a@posting.go ogle.com>...[color=blue]
                          > Hi,
                          >
                          > can anybody help with the following problem?[/color]
                          ....[color=blue]
                          > i = 5 / 10 gives me 0 as expected, but
                          > i = -5 / 10 gives -1 as result.[/color]

                          The problem is that you aren't using "from __future__ import division"
                          ;-) (This causes the results to be 0.5 and -0.5, and will be the
                          default division semantics in Python 3.0. If you want integer
                          division, use the // operator.)
                          [color=blue]
                          > Is this a feature or a bug?[/color]

                          It's a feature. The advantage of defining x // y as floor(x / y) is
                          that x % y is always nonnegative.

                          As as example of why this is useful, consider the
                          datetime.date.w eekday method. This could be implemented as

                          def weekday(self):
                          return (self.fromordin al() - 1) % 7

                          If the datetime module was changed to allow BC(E) dates (which have
                          nonpositive Rata Die numbers), the weekday method would still work.
                          In C++, you'd have to treat such dates as a special case.

                          Comment

                          • Samuel Walters

                            #14
                            Re: Integer math question

                            |Thus Spake Sean Ross On the now historical date of Sat, 03 Jan 2004
                            16:31:17 -0500|[color=blue]
                            > I think you're supposed to do something like this a = bq + r, 0<= r <
                            > |b|[/color]
                            It is, indeed, 0 <= r < abs(b)

                            "If a and b are integers such that b != 0, then there exist unique
                            integers r and q such that a = q*b + r and 0 <= r < abs(b)"

                            For non-mathematical spectators, the divmod() function is defined so that
                            q, r = divmod(a, b) by the definition above.

                            Sam Walters

                            --
                            Never forget the halloween documents.

                            """ Where will Microsoft try to drag you today?
                            Do you really want to go there?"""

                            Comment

                            • Sean Ross

                              #15
                              Re: Integer math question

                              "Samuel Walters" <swalters_usene t@yahoo.com> wrote in message
                              news:pan.2004.0 1.05.17.54.22.8 94833@yahoo.com ...[color=blue]
                              > "If a and b are integers such that b != 0, then there exist unique
                              > integers r and q such that a = q*b + r and 0 <= r < abs(b)"
                              >
                              > For non-mathematical spectators, the divmod() function is defined so that
                              > q, r = divmod(a, b) by the definition above.[/color]

                              Right. The only thing that was still mildly interesting to me was why does
                              divmod() return a negative remainder?
                              [color=blue][color=green][color=darkred]
                              >>> a = 5
                              >>> b = -10
                              >>> q,r = divmod(a,b)
                              >>> q[/color][/color][/color]
                              -1[color=blue][color=green][color=darkred]
                              >>> r[/color][/color][/color]
                              -5

                              If divmod() where defined based on the definition above, then divmod(5, -10)
                              should return (0, 5).
                              Well ... that's too strong. The above is a theorem - it doesn't say
                              remainders have to be nonnegative,
                              it only says that there exist yada yada yada ... whatever, I'm not that
                              interested.



                              Comment

                              Working...