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. C#
  4. Modulo Function

Modulo Function

Scheduled Pinned Locked Moved C#
csharpjavaalgorithmsquestion
10 Posts 5 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.
  • Q Offline
    Q Offline
    Quake2Player
    wrote on last edited by
    #1

    Hi, Can someone tell me a MODULO algorithm like google's, typical calculator's, excel's, etc? Because, they all return 0 for: 4029800 mod 100 But C# (and java I think) return -10

    D L D 3 Replies Last reply
    0
    • Q Quake2Player

      Hi, Can someone tell me a MODULO algorithm like google's, typical calculator's, excel's, etc? Because, they all return 0 for: 4029800 mod 100 But C# (and java I think) return -10

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      4029800 % 100 gets 0 for me :confused:

      Dave
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
      Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
      Why are you using VB6? Do you hate yourself? (Christian Graus)

      1 Reply Last reply
      0
      • Q Quake2Player

        Hi, Can someone tell me a MODULO algorithm like google's, typical calculator's, excel's, etc? Because, they all return 0 for: 4029800 mod 100 But C# (and java I think) return -10

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Quake2Player wrote:

        But C# (and java I think) return -10

        Could you show the code? That really isn't what should happen, not in Java either.

        1 Reply Last reply
        0
        • Q Quake2Player

          Hi, Can someone tell me a MODULO algorithm like google's, typical calculator's, excel's, etc? Because, they all return 0 for: 4029800 mod 100 But C# (and java I think) return -10

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Your code is bad somewhere. For two positive integers, modulo is never negative.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008
          But no longer in 2009...

          Q 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Your code is bad somewhere. For two positive integers, modulo is never negative.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008
            But no longer in 2009...

            Q Offline
            Q Offline
            Quake2Player
            wrote on last edited by
            #5

            Random r = new Random(); int key = r.Next(0, 10000); int prime = r.Next(key, key * 999); int a = r.Next(1, prime - 1); int b = r.Next(0, prime - 1); Console.WriteLine(key); Console.WriteLine(prime); Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine( ((key*a + b)%prime) % 1000); Console.ReadLine(); For example: In google: (((8 568 * 4 974 445) + 2 820 002) % 7 661 773) % 1 000 = 563} In c#: (((8 568 * 4 974 445) + 2 820 002) % 7 661 773) % 1 000 = -732

            D L 2 Replies Last reply
            0
            • Q Quake2Player

              Random r = new Random(); int key = r.Next(0, 10000); int prime = r.Next(key, key * 999); int a = r.Next(1, prime - 1); int b = r.Next(0, prime - 1); Console.WriteLine(key); Console.WriteLine(prime); Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine( ((key*a + b)%prime) % 1000); Console.ReadLine(); For example: In google: (((8 568 * 4 974 445) + 2 820 002) % 7 661 773) % 1 000 = 563} In c#: (((8 568 * 4 974 445) + 2 820 002) % 7 661 773) % 1 000 = -732

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

              Quake2Player wrote:

              8 568 * 4 974 445)

              using 32-bit signed arithmetic (as in int) this results in an overflow, that is where the negative stuff is coming from. Nothing wrong with the modulo operator. test: declare long variables, initialize them, and use those for your expression, instead of numeric constants. (decimal should work too). :)

              Luc Pattyn

              :badger: :jig: :badger:

              Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.

              :jig: :badger: :jig:

              D Q 2 Replies Last reply
              0
              • Q Quake2Player

                Random r = new Random(); int key = r.Next(0, 10000); int prime = r.Next(key, key * 999); int a = r.Next(1, prime - 1); int b = r.Next(0, prime - 1); Console.WriteLine(key); Console.WriteLine(prime); Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine( ((key*a + b)%prime) % 1000); Console.ReadLine(); For example: In google: (((8 568 * 4 974 445) + 2 820 002) % 7 661 773) % 1 000 = 563} In c#: (((8 568 * 4 974 445) + 2 820 002) % 7 661 773) % 1 000 = -732

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                It's because of the overflow in the first part of the calculation. Make one of the values a double and you get the correct result. (((8568 * 4974445d) + 2820002) % 7661773) % 1000)

                Dave
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
                Why are you using VB6? Do you hate yourself? (Christian Graus)

                Q 1 Reply Last reply
                0
                • L Luc Pattyn

                  Quake2Player wrote:

                  8 568 * 4 974 445)

                  using 32-bit signed arithmetic (as in int) this results in an overflow, that is where the negative stuff is coming from. Nothing wrong with the modulo operator. test: declare long variables, initialize them, and use those for your expression, instead of numeric constants. (decimal should work too). :)

                  Luc Pattyn

                  :badger: :jig: :badger:

                  Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.

                  :jig: :badger: :jig:

                  D Offline
                  D Offline
                  DaveyM69
                  wrote on last edited by
                  #8

                  Beat me to it! :mad: :laugh:

                  Dave
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                  Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
                  Why are you using VB6? Do you hate yourself? (Christian Graus)

                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Quake2Player wrote:

                    8 568 * 4 974 445)

                    using 32-bit signed arithmetic (as in int) this results in an overflow, that is where the negative stuff is coming from. Nothing wrong with the modulo operator. test: declare long variables, initialize them, and use those for your expression, instead of numeric constants. (decimal should work too). :)

                    Luc Pattyn

                    :badger: :jig: :badger:

                    Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.

                    :jig: :badger: :jig:

                    Q Offline
                    Q Offline
                    Quake2Player
                    wrote on last edited by
                    #9

                    THANK YOU!

                    1 Reply Last reply
                    0
                    • D DaveyM69

                      It's because of the overflow in the first part of the calculation. Make one of the values a double and you get the correct result. (((8568 * 4974445d) + 2820002) % 7661773) % 1000)

                      Dave
                      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                      Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
                      Why are you using VB6? Do you hate yourself? (Christian Graus)

                      Q Offline
                      Q Offline
                      Quake2Player
                      wrote on last edited by
                      #10

                      How can I use long if I'm generating random numbers??? This is all about the UNIVERSAL HASHING FUNCTION:

                              public int Evaluate(int key) 
                              {
                                      // Random prime bigger than 10000
                                      do {
                                        randomPrime = rnd.Next(10001, 10000*999);
                                      }
                                      while (!IsPrime(randomPrime));
                      
                                      this.randomA = rnd.Next(1, randomPrime - 1);
                                      this.randomB = rnd.Next(0, randomPrime - 1);
                      
                                      return ((randomA * key + randomB) % randomPrime) % tableLength;
                              }
                      

                      Notes: -10000 is the maximum possible key -tableLength is 1000. -randomA, randomB and randomPrime are ints As Im getting negative numbers Im trying return Math.Abs(((randomA * key + randomB) % randomPrime) % tableLength); But its not giving me fast results in the hash table

                      modified on Saturday, August 29, 2009 6:01 PM

                      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