Modulo Function
-
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
-
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
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) -
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
-
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
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... -
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...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
-
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
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:
-
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
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) -
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:
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) -
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:
THANK YOU!
-
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)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