Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Negative Mod numbers

Negative Mod numbers

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NeverHeardOfMe
    wrote on last edited by
    #1

    According to VB.NET

    -1 Mod 2 = -1

    Is this a WTF moment or has maths changed since I did my degree in it?

    P L 2 Replies Last reply
    0
    • N NeverHeardOfMe

      According to VB.NET

      -1 Mod 2 = -1

      Is this a WTF moment or has maths changed since I did my degree in it?

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      It's certainly very incongruous.

      I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

      Forgive your enemies - it messes with their heads

      My blog | My articles | MoXAML PowerToys | Onyx

      L 1 Reply Last reply
      0
      • N NeverHeardOfMe

        According to VB.NET

        -1 Mod 2 = -1

        Is this a WTF moment or has maths changed since I did my degree in it?

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        the behavior of division and modulo is always tricky when dealing with negative numbers; each language can make its own choice, as long as one invariant remains true:

        a = b * (a DIV b) + (a MOD b)

        for all values of a and b If it doesn't fit your expectation, avoid negative numbers, or write your own little method (or create your own integer type and use operator overloading). :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

        N 1 Reply Last reply
        0
        • P Pete OHanlon

          It's certainly very incongruous.

          I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

          Forgive your enemies - it messes with their heads

          My blog | My articles | MoXAML PowerToys | Onyx

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          is it? would you prefer the integer quotient of -1 and 2 to be -1 and the remainder +1? making sure the remainder has the sign of the dividend makes a lot of sense to me. :) PS: don't ask about negative divisors!

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

          P 1 Reply Last reply
          0
          • L Luc Pattyn

            the behavior of division and modulo is always tricky when dealing with negative numbers; each language can make its own choice, as long as one invariant remains true:

            a = b * (a DIV b) + (a MOD b)

            for all values of a and b If it doesn't fit your expectation, avoid negative numbers, or write your own little method (or create your own integer type and use operator overloading). :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            N Offline
            N Offline
            NeverHeardOfMe
            wrote on last edited by
            #5

            Well, it's easy to work around once one knows, of course... but it took me a while to figure out why I wasn't getting the expected results (out of my function of which this was but one line.) It certainly isn't what I was taught about modulo.

            1 Reply Last reply
            0
            • L Luc Pattyn

              is it? would you prefer the integer quotient of -1 and 2 to be -1 and the remainder +1? making sure the remainder has the sign of the dividend makes a lot of sense to me. :) PS: don't ask about negative divisors!

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              I was making a pun. A play on words. Modulus operations are part of congruence relations.

              I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Onyx

              L 1 Reply Last reply
              0
              • P Pete OHanlon

                I was making a pun. A play on words. Modulus operations are part of congruence relations.

                I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Onyx

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                you've had me there. but still, I'd rather call it congruous, as I got used to a slightly counter-intuitive but perfectly rational behavior of integer division and modulo (which text books and school classes mostly fail to deal with). :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups