Using Remainder Operator with Negative Divident

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

    Using Remainder Operator with Negative Divident

    My rateonale is:
    -1 mod 3 = must be 3
    0 mod 3 = 0
    1 mod 3 = 1
    2 mod 3 = 2
    3 mod 3 = 3
    4 mod 3 = 0
    = 1
    = 2
    = 3
    = 0

    We note a sequence: 0 1 2 3 0 1 2 3 0 1 2 3. Going in backward direction, we
    get 3 2 1 0 3 2 1 0 3 2 1 0.

    Since (0 % N) is 0, going one step back (-1 % N) must result in N. But c#
    compiler gives me -1. It is wrong. Fix it. The C#LS seems wrong. Correct it.


  • Jon Skeet [C# MVP]

    #2
    Re: Using Remainder Operator with Negative Divident

    valentin tihomirov <V_tihomirov@be st.eewrote:
    My rateonale is:
    -1 mod 3 = must be 3
    0 mod 3 = 0
    1 mod 3 = 1
    2 mod 3 = 2
    3 mod 3 = 3
    4 mod 3 = 0
    = 1
    = 2
    = 3
    = 0
    >
    We note a sequence: 0 1 2 3 0 1 2 3 0 1 2 3. Going in backward direction, we
    get 3 2 1 0 3 2 1 0 3 2 1 0.
    >
    Since (0 % N) is 0, going one step back (-1 % N) must result in N. But c#
    compiler gives me -1. It is wrong. Fix it. The C#LS seems wrong. Correct it.
    Your understanding of "remainder" is too rigid. Fix it :)

    Seriously, there are different understandings of how remainders should
    work with negative numbers. Some systems use the sign of the dividend,
    others use the sign of the divisor. See
    http://en.wikipedia.org/wiki/Modulo_operation for a list.

    You know what I think is *really* broken? To leave it to the
    implementation - which is what C++ did, at least at the time of
    Stroustrup's 2nd edition. (I seriously hope it's been fixed since
    then.)


    (I have in my head that "remainder" should work as shown in C#, but if
    a language defines a "modulo" operator, that should always be non-
    negative. However, I can't find any evidence backing that up.)

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Doug Semler

      #3
      Re: Using Remainder Operator with Negative Divident

      "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
      news:MPG.2160f0 a1540996f14b0@m snews.microsoft .com...
      >
      >
      (I have in my head that "remainder" should work as shown in C#, but if
      a language defines a "modulo" operator, that should always be non-
      negative. However, I can't find any evidence backing that up.)

      What should be non-negative? The result of the modulus operator? If the
      dividend is negative in C#, the result of the modulus is negative, which
      actually makes sense mathematically (what would you need to ADD to the
      result of the divisor times the result of the division to get back to your
      dividend?) Note this is only for integer modulus. I always hated float
      modulus :)

      -3 / -2 = 1 - what would you need to add? (-1) so -3 % -2 = -1
      -3 / 2 = -1 - what do you add? -1 again. so -3 % 2 = -1
      3 / -2 = -1 - what do you need to add to -1 * -2?


      --
      Doug Semler, MCPD
      a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
      The answer is 42; DNRC o-
      Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
      erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?

      Comment

      • Doug Semler

        #4
        Re: Using Remainder Operator with Negative Divident

        "Doug Semler" <dougsemler@gma il.comwrote in message
        news:evaBB9i$HH A.4612@TK2MSFTN GP03.phx.gbl...
        "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
        news:MPG.2160f0 a1540996f14b0@m snews.microsoft .com...
        >
        >>
        >>
        >(I have in my head that "remainder" should work as shown in C#, but if
        >a language defines a "modulo" operator, that should always be non-
        >negative. However, I can't find any evidence backing that up.)
        >
        >
        What should be non-negative? The result of the modulus operator? If the
        dividend is negative in C#, the result of the modulus is negative, which
        actually makes sense mathematically (what would you need to ADD to the
        result of the divisor times the result of the division to get back to your
        dividend?) Note this is only for integer modulus. I always hated float
        modulus :)
        Note that my note above was for the example below, not that float modulus
        acted differently with respect to sign <g>
        >
        -3 / -2 = 1 - what would you need to add? (-1) so -3 % -2 = -1
        -3 / 2 = -1 - what do you add? -1 again. so -3 % 2 = -1
        3 / -2 = -1 - what do you need to add to -1 * -2?
        >
        >
        --
        Doug Semler, MCPD
        a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
        The answer is 42; DNRC o-
        Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
        erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Using Remainder Operator with Negative Divident

          Doug Semler <dougsemler@gma il.comwrote:
          (I have in my head that "remainder" should work as shown in C#, but if
          a language defines a "modulo" operator, that should always be non-
          negative. However, I can't find any evidence backing that up.)
          >
          What should be non-negative? The result of the modulus operator?
          Yes - in my head, anyway :)
          If the dividend is negative in C#, the result of the modulus is negative, which
          actually makes sense mathematically (what would you need to ADD to the
          result of the divisor times the result of the division to get back to your
          dividend?) Note this is only for integer modulus. I always hated float
          modulus :)
          Yes, I totally see your point. Of course, it depends on how you round
          the division aspect. If you define integer division to round down, i.e.
          to always return the largest number d such that d*x < y, then you will
          always have a non-negative integer r such that d*x+r = y. However, C#
          (and many other computer languages) use round-towards-zero for
          division, which is where the negative remainder comes in.

          At least, that's what I currently think - but I'm about to go to bed,
          so anything I write now probably shouldn't be trusted.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Using Remainder Operator with Negative Divident

            On Sep 24, 9:27 am, "valentin tihomirov" <V_tihomi...@be st.eewrote:
            Actually, I have determined that their algorithm can be implemented without
            wrapping the last column to 0 forth and back, which require these ugly
            if-else. It is the Royal Mail User Guide, which must be fixed then
            (simplified).
            I'd prefer their algorithm to be fixed - keep the content of the table
            where it is, but make 0 the first row/column. Too late to do that now,
            but clearly you can remap the table if you're willing to change where
            the contents are. It's only a lookup, after all.

            Jon

            Comment

            • valentin tihomirov

              #7
              Re: Using Remainder Operator with Negative Divident

              I'd prefer their algorithm to be fixed - keep the content of the table
              where it is, but make 0 the first row/column. Too late to do that now,
              but clearly you can remap the table if you're willing to change where
              the contents are. It's only a lookup, after all.
              The algorithm seems correctly designed. The explanation is bad moving the 0
              column into the end and back for no reason.


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Using Remainder Operator with Negative Divident

                On Sep 24, 10:21 am, "valentin tihomirov" <V_tihomi...@be st.eewrote:
                I'd prefer their algorithm to be fixed - keep the content of the table
                where it is, but make 0 the first row/column. Too late to do that now,
                but clearly you can remap the table if you're willing to change where
                the contents are. It's only a lookup, after all.
                >
                The algorithm seems correctly designed. The explanation is bad moving the 0
                column into the end and back for no reason.
                Yes, by "algorithm" I guess I really meant "data used by the
                algorithm" - i.e. the table.

                Either way, it's certainly not C#'s fault :)

                Jon

                Comment

                • james.curran@gmail.com

                  #9
                  Re: Using Remainder Operator with Negative Divident

                  On Sep 23, 5:53 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
                  You know what I think is *really* broken? To leave it to the
                  implementation - which is what C++ did, at least at the time of
                  Stroustrup's 2nd edition. (I seriously hope it's been fixed since
                  then.)
                  I doubt it. Especially considering the C++ standard still does not
                  require a particular binary representation for -1, or, for that
                  matter, a particular value for 32767+1.

                  Comment

                  Working...