(Numeric) should -7 % 5 = -2 ?

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

    (Numeric) should -7 % 5 = -2 ?

    Is the following behavior expected, or have I broken my Numeric
    installation somehow?

    $python
    Python 2.2.2 (#1, Mar 21 2003, 23:01:54)
    [GCC 3.2.3 20030316 (Debian prerelease)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> import Numeric
    >>> Numeric.__versi on__[/color][/color][/color]
    '23.0'[color=blue][color=green][color=darkred]
    >>> -7 % 5[/color][/color][/color]
    3[color=blue][color=green][color=darkred]
    >>> Numeric.array(-7) % 5[/color][/color][/color]
    -2[color=blue][color=green][color=darkred]
    >>> Numeric.remaind er(-7, 5)[/color][/color][/color]
    -2

    --
    Stan
  • Fredrik Lundh

    #2
    Re: (Numeric) should -7 % 5 = -2 ?

    Stan Heckman wrote:
    [color=blue]
    > Is the following behavior expected, or have I broken my Numeric
    > installation somehow?
    >
    > $ python[color=green][color=darkred]
    > >>> import Numeric
    > >>> Numeric.__versi on__[/color][/color]
    > '23.0'[color=green][color=darkred]
    > >>> -7 % 5[/color][/color]
    > 3[color=green][color=darkred]
    > >>> Numeric.array(-7) % 5[/color][/color]
    > -2[color=green][color=darkred]
    > >>> Numeric.remaind er(-7, 5)[/color][/color]
    > -2[/color]

    looks like Numeric implements C semantics, which is different
    from how Python does it.

    </F>




    Comment

    • Tim Roberts

      #3
      Re: (Numeric) should -7 % 5 = -2 ?

      "Louis M. Pecora" <pecora@anvil.n rl.navy.mil> wrote:
      [color=blue]
      >Fredrik Lundh <fredrik@python ware.com> wrote:
      >[color=green][color=darkred]
      >> > >>> -7 % 5
      >> > 3
      >> > >>> Numeric.array(-7) % 5
      >> > -2
      >> > >>> Numeric.remaind er(-7, 5)
      >> > -2[/color]
      >>
      >> looks like Numeric implements C semantics, which is different
      >> from how Python does it.[/color]
      >
      >Hmmm... "remainder" makes sense. But "%" is mod, right. IIRC from my
      >abstract algebra days (only 30 yrs ago :-) ) The "X mod n" function
      >maps onto the postive integers from 0 to n-1. So sounds like numeric
      >contradicts the math texts. Not good since it's a math module.[/color]

      That's a bit harsh. The problem is that there is no universal agreement in
      the world of computer science as to what the semantics of the modulo
      operator should be when presented with a negative operand. Contradicting
      Fredrik, something I do with great reluctance, the C standard specifies
      that the behavior is implementation-defined, so in fact BOTH answers
      "implement C semantics".

      Fortran, on the other hand, defines A mod P as A-INT(A/P)*P, which is
      exactly what Numeric produces. Since folks interested in numerical
      programming often have a strong Fortran background, it is not terribly
      surprising that Numeric should follow Fortran's lead.
      --
      - Tim Roberts, timr@probo.com
      Providenza & Boekelheide, Inc.

      Comment

      • Richard Brodie

        #4
        Re: (Numeric) should -7 % 5 = -2 ?


        "Tim Roberts" <timr@probo.com > wrote in message news:3g32gvcoli s4485956egtc35a km6lh3uq5@4ax.c om...
        [color=blue]
        > Contradicting Fredrik, something I do with great reluctance, the C standard
        > specifies that the behavior is implementation-defined, so in fact BOTH answers
        > "implement C semantics".[/color]

        Only if you're looking at an old version of the standard; it's not implementation
        dependent in C99.


        Comment

        • Louis M. Pecora

          #5
          Re: (Numeric) should -7 % 5 = -2 ?

          In article <3g32gvcolis448 5956egtc35akm6l h3uq5@4ax.com>, Tim Roberts
          <timr@probo.com > wrote:
          [color=blue][color=green]
          > >Hmmm... "remainder" makes sense. But "%" is mod, right. IIRC from my
          > >abstract algebra days (only 30 yrs ago :-) ) The "X mod n" function
          > >maps onto the postive integers from 0 to n-1. So sounds like numeric
          > >contradicts the math texts. Not good since it's a math module.[/color]
          >
          > That's a bit harsh.[/color]

          You may be right. I got to work and checked my old Abstract Algebra
          book. The defintion is,

          We write a=b mod m if m divides (a-b) (i.e. no remeinder).

          The defintion does not say how to compute the mod, rather it is an
          expression of a relationship between a and b. Hence, writing -2=-7 mod
          5 appears to be OK.

          The "uniqueness " comes in when we recogize that mod m defines an
          equivalence relation on the integers and so for a given m every integer
          falls into a unique class (or subset of integers). The set of m
          subsets is equivalent to the positive integers 0 to m-1.

          So it appears that the translation between math and computer science is
          not as clear as I thought. In math (well, number theory) mod is a
          relation, not an operation. In computer science it is an operation.

          Waddayathink?

          --
          Lou Pecora
          - My views are my own.

          Comment

          • Bob Gailer

            #6
            Re: (Numeric) should -7 % 5 = -2 ?

            At 11:19 AM 7/1/2003 -0400, Louis M. Pecora wrote:
            [color=blue]
            >In article <3g32gvcolis448 5956egtc35akm6l h3uq5@4ax.com>, Tim Roberts
            ><timr@probo.co m> wrote:
            >[color=green][color=darkred]
            > > >Hmmm... "remainder" makes sense. But "%" is mod, right. IIRC from my
            > > >abstract algebra days (only 30 yrs ago :-) ) The "X mod n" function
            > > >maps onto the postive integers from 0 to n-1. So sounds like numeric
            > > >contradicts the math texts. Not good since it's a math module.[/color]
            > >
            > > That's a bit harsh.[/color]
            >
            >You may be right. I got to work and checked my old Abstract Algebra
            >book. The defintion is,
            >
            >We write a=b mod m if m divides (a-b) (i.e. no remeinder).
            >
            >The defintion does not say how to compute the mod, rather it is an
            >expression of a relationship between a and b. Hence, writing -2=-7 mod
            >5 appears to be OK.
            >[snip][/color]

            To quote from "Number Theory and its History" by Oystein Ore, page 213f:
            "When an integer a is divided by another m, one has
            a = km + r
            where the remainder is some positive integer less than m. Thus for any
            number a there exists a congruence
            a (is congruent to) r (mod m)
            where r is a unique one among the numbers 0, 2, 1, .... m-1"

            Bob Gailer
            bgailer@alum.rp i.edu
            303 442 2625


            ---
            Outgoing mail is certified Virus Free.
            Checked by AVG anti-virus system (http://www.grisoft.com).
            Version: 6.0.492 / Virus Database: 291 - Release Date: 6/24/2003

            Comment

            • Erik Max Francis

              #7
              Re: (Numeric) should -7 % 5 = -2 ?

              "Louis M. Pecora" wrote:
              [color=blue]
              > We write a=b mod m if m divides (a-b) (i.e. no remeinder).
              >
              > The defintion does not say how to compute the mod, rather it is an
              > expression of a relationship between a and b. Hence, writing -2=-7
              > mod
              > 5 appears to be OK.[/color]

              Right. Equivalences modulo m are really alternate numerical spaces in
              which arithmetic is done; in mathematics, the modulo is not strictly an
              operator. In those cases, you don't really have to pick a unique
              residue when doing arithmetic (mod m), since it's all equivalence
              relation anyway.

              In computer science, where modulo is an operator that must return a
              unique value, it's not really specified whether (-n % m) (m, n positive)
              should be negative or not. Some languages/systems chose negative, some
              don't. The choice is never "wrong" unless it's done inconsistently
              within a particular language/system.

              --
              Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
              __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
              / \ People are taught to be racists.
              \__/ Jose Abad

              Comment

              • Julian Tibble

                #8
                Re: (Numeric) should -7 % 5 = -2 ?

                In article <02072003145905 0258%pecora@anv il.nrl.navy.mil >,
                Louis M. Pecora wrote:[color=blue]
                > In article <mailman.105708 8943.30504.pyth on-list@python.org >, Bob
                > Gailer <bgailer@alum.r pi.edu> wrote:
                >[color=green]
                >> "When an integer a is divided by another m, one has
                >> a = km + r
                >> where the remainder is some positive integer less than m.[/color]
                >
                > Maybe I'm misunderstandin g this, but what about -7 divided by 5? We
                > get k=-1 and r=-2.[/color]

                -7 = (-2) * 5 + 3

                So actually, k = -2 and r = 3

                [color=blue]
                > m can be negative. Maybe your quote was for the positive integers only.[/color]

                True, that definition does not make sense when m is negative. When I came
                across the subject in introductory group theory they said "positive divisor".

                Julian

                Comment

                • Julian Tibble

                  #9
                  Re: (Numeric) should -7 % 5 = -2 ?

                  In article <02072003145905 0258%pecora@anv il.nrl.navy.mil >,
                  Louis M. Pecora wrote:[color=blue]
                  > In article <mailman.105708 8943.30504.pyth on-list@python.org >, Bob
                  > Gailer <bgailer@alum.r pi.edu> wrote:
                  >[color=green]
                  >> "When an integer a is divided by another m, one has
                  >> a = km + r
                  >> where the remainder is some positive integer less than m.[/color]
                  >
                  > Maybe I'm misunderstandin g this, but what about -7 divided by 5? We
                  > get k=-1 and r=-2.[/color]

                  -7 = (-2) * 5 + 3

                  So actually, k = -2 and r = 3

                  [color=blue]
                  > m can be negative. Maybe your quote was for the positive integers only.[/color]

                  True, that definition does not make sense when m is negative. When I came
                  across the subject in introductory group theory they said "positive divisor".

                  Julian

                  Comment

                  Working...