negative numbers and integer division

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

    negative numbers and integer division

    Hi-

    I just discovered this:
    [color=blue][color=green][color=darkred]
    >>> -1 // 12[/color][/color][/color]
    -1[color=blue][color=green][color=darkred]
    >>> 1 // 12[/color][/color][/color]
    0[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    I thought that -1 // 12 would be 0 also. I'm writing a simple monthly
    date class and i need (-1,2001) to be translated to (11,2000). Any ideas?

  • Martin v. Löwis

    #2
    Re: negative numbers and integer division

    "Matthew Wilson" <mwilson@sarcas tic-horse.com> writes:
    [color=blue]
    > Hi-
    >
    > I just discovered this:
    >[color=green][color=darkred]
    > >>> -1 // 12[/color][/color]
    > -1[color=green][color=darkred]
    > >>> 1 // 12[/color][/color]
    > 0[color=green][color=darkred]
    > >>>[/color][/color]
    >
    > I thought that -1 // 12 would be 0 also.[/color]

    Why did you think that? Dividing -1 by 12 gives -1, with a remainder
    of 11: -1*12 + 11 == -1.
    [color=blue]
    > I'm writing a simple monthly date class and i need (-1,2001) to be
    > translated to (11,2000). Any ideas?[/color]

    def norm_month(mont h, year):
    delta_year, month = divmod(month, 12)
    return month, year+delta_year

    print norm_month(-1, 2001)

    Regards,
    Martin

    Comment

    • Mark Hahn

      #3
      Re: negative numbers and integer division

      That definitely seems wrong to me, even though it is a correct "floor"
      function.

      I was given the impression that (int // int) was going to be the replacement
      for (int / int) when (int / int) is changed to return a float, but -1/12 now
      gives 0, not -1, so (int // int) is not a replacement for (int / int).

      "Matthew Wilson" <mwilson@sarcas tic-horse.com> wrote in message
      news:mailman.10 65206646.19185. python-list@python.org ...[color=blue]
      > Hi-
      >
      > I just discovered this:
      >[color=green][color=darkred]
      > >>> -1 // 12[/color][/color]
      > -1[color=green][color=darkred]
      > >>> 1 // 12[/color][/color]
      > 0[color=green][color=darkred]
      > >>>[/color][/color]
      >
      > I thought that -1 // 12 would be 0 also. I'm writing a simple monthly
      > date class and i need (-1,2001) to be translated to (11,2000). Any ideas?
      >[/color]


      Comment

      • Mark Hahn

        #4
        Re: negative numbers and integer division

        oops -- ignore that last post -- i was totally wrong

        "Mark Hahn" <mark@hahnca.co m> wrote in message
        news:HWjfb.5187 $hp5.1152@fed1r ead04...[color=blue]
        > That definitely seems wrong to me, even though it is a correct "floor"
        > function.
        >
        > I was given the impression that (int // int) was going to be the[/color]
        replacement[color=blue]
        > for (int / int) when (int / int) is changed to return a float, but -1/12[/color]
        now[color=blue]
        > gives 0, not -1, so (int // int) is not a replacement for (int / int).
        >
        > "Matthew Wilson" <mwilson@sarcas tic-horse.com> wrote in message
        > news:mailman.10 65206646.19185. python-list@python.org ...[color=green]
        > > Hi-
        > >
        > > I just discovered this:
        > >[color=darkred]
        > > >>> -1 // 12[/color]
        > > -1[color=darkred]
        > > >>> 1 // 12[/color]
        > > 0[color=darkred]
        > > >>>[/color]
        > >
        > > I thought that -1 // 12 would be 0 also. I'm writing a simple monthly
        > > date class and i need (-1,2001) to be translated to (11,2000). Any[/color][/color]
        ideas?[color=blue][color=green]
        > >[/color]
        >
        >[/color]


        Comment

        • Rainer Deyke

          #5
          Re: negative numbers and integer division

          Matthew Wilson wrote:[color=blue]
          > I thought that -1 // 12 would be 0 also. I'm writing a simple monthly
          > date class and i need (-1,2001) to be translated to (11,2000). Any
          > ideas?[/color]

          Luckily for you, Python integer division does the right thing. You
          *want* -1 // 12 to be -1 for your application.

          def normalize_date( month, year):
          return (month % 12, year + month // 12)

          normalize_date(-1, 2001) # Returns (11, 2000)


          'normalize_date ' as given assumes 'month' is in the range from 0 to 11; use
          this if your months are in the range from 1 to 12:

          def normalize_date( month, year):
          return ((month - 1) % 12 + 1, year + (month - 1) // 12)



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


          Comment

          • Skip Montanaro

            #6
            Re: negative numbers and integer division


            Mark> I was given the impression that (int // int) was going to be the
            Mark> replacement for (int / int) when (int / int) is changed to return
            Mark> a float, but -1/12 now gives 0, not -1, so (int // int) is not a
            Mark> replacement for (int / int).
            [color=blue][color=green][color=darkred]
            >>> from __future__ import division
            >>> -1/12[/color][/color][/color]
            -0.0833333333333 33329[color=blue][color=green][color=darkred]
            >>> -1//12[/color][/color][/color]
            -1

            Skip

            Comment

            • Peter Abel

              #7
              Re: negative numbers and integer division

              "Matthew Wilson" <mwilson@sarcas tic-horse.com> wrote in message news:<mailman.1 065206646.19185 .python-list@python.org >...[color=blue]
              > Hi-
              >
              > I just discovered this:
              >[color=green][color=darkred]
              > >>> -1 // 12[/color][/color]
              > -1[color=green][color=darkred]
              > >>> 1 // 12[/color][/color]
              > 0[color=green][color=darkred]
              > >>>[/color][/color]
              >
              > I thought that -1 // 12 would be 0 also. I'm writing a simple monthly
              > date class and i need (-1,2001) to be translated to (11,2000). Any ideas?[/color]

              Maybe the following could help:[color=blue][color=green][color=darkred]
              >>> def normalize_date( month,year):[/color][/color][/color]
              .... return (((month+11) % 12)+1,year+((mo nth-1)/12))
              ....[color=blue][color=green][color=darkred]
              >>> normalize_date( 1,2003)[/color][/color][/color]
              (1, 2003)[color=blue][color=green][color=darkred]
              >>> normalize_date( 12,2003)[/color][/color][/color]
              (12, 2003)[color=blue][color=green][color=darkred]
              >>> normalize_date( 0,2003)[/color][/color][/color]
              (12, 2002)[color=blue][color=green][color=darkred]
              >>> normalize_date(-1,2003)[/color][/color][/color]
              (11, 2002)[color=blue][color=green][color=darkred]
              >>> normalize_date( 13,2003)[/color][/color][/color]
              (1, 2004)[color=blue][color=green][color=darkred]
              >>>[/color][/color][/color]

              Regards
              Peter

              Comment

              • mensanator

                #8
                Re: negative numbers and integer division

                "Matthew Wilson" <mwilson@sarcas tic-horse.com> wrote in message news:<mailman.1 065206646.19185 .python-list@python.org >...[color=blue]
                > Hi-
                >
                > I just discovered this:
                >[color=green][color=darkred]
                > >>> -1 // 12[/color][/color]
                > -1[color=green][color=darkred]
                > >>> 1 // 12[/color][/color]
                > 0[color=green][color=darkred]
                > >>>[/color][/color]
                >
                > I thought that -1 // 12 would be 0 also. I'm writing a simple monthly
                > date class and i need (-1,2001) to be translated to (11,2000). Any ideas?[/color]
                [color=blue][color=green][color=darkred]
                >>> -1 % 12[/color][/color][/color]
                11

                Comment

                Working...