views:

254

answers:

9

Possible Duplicate:
Recognizing when to use the mod operator

I wonder, what are the practical uses of modulus? I know what modulo division is. The first scenario which comes to my mind is to using it to find odd and even numbers, and clock arithmetic. But where esle you could use it?

+4  A: 

To print a number as string, you need the modulus to find the value of a digit.

string number_to_string(uint number) {
  string result = "";
  while (number != 0) {
    result = cast(char)((number % 10) + '0') ~ result;
    //                   ^^^^^^^^^^^
    number /= 10;
  }
  return result;
}
KennyTM
+1 for using D. :)
Peter Alexander
+3  A: 

The most common use I've found is for "wrapping round" your array indices.

For example, if you just want to cycle through an array repeatedly, you could use:

int a[10];
for (int i = 0; true; i = (i + 1) % 10)
{
  // ... use a[i] ...
}

The modulo ensures that i stays in the [0, 10) range.

Peter Alexander
I couldn't think of any useful use of such an infinite loop, but it looks cool.
Kau-Boy
+3  A: 
  • Cryptography. That alone would account for an obscene percentage of modulus (I exaggerate, but you get the point).

Try the wikipedia too!

Modular arithmetic is referenced in number theory, group theory, ring theory, knot theory, abstract algebra, cryptography, computer science, chemistry and the visual and musical arts.

IME any sufficiently advanced algorithm is probably going to touch on one more of the above topics.

dirkgently
+4  A: 

One use for the modulus operation is when making a hash table. It's used to convert the value out of the hash function into an index into the array. (If the hash table size is a power of two, the modulus could be done with a bit-mask, but it's still a modulus operation.)

Donal Fellows
+1  A: 

Well there are many perspective you can look at it.If you are looking at is as a mathematical operation then its just a modulo division. Even we don't need this as whatever % do,we can achieve using subtraction as well,but every programming language implement it in very optimize way.And module division is not limited to find odd and even or clock arithmetic,there are hundreds of algorithm which need this module operation,eg. cryptography algos etc. So its a general mathematical operation like other +,-,*,/ etc.

Except the mathematical perspective,different language use this symbol for defining build-in data structure like in perl %hash, is used to show that programmer declared hash. So it all varies based on the programing language design.

So still there are lot of other perspective which can do add to the list of use of %.

Anil Vishnoi
+1  A: 

The usual implementation of buffered communications uses circular buffers, and you manage them with modulus arithmetic.

dmckee
+2  A: 

For languages that don't have bitwise operators, modulus can be used to get the lowest n bits of a number. For example, to get the lowest 8 bits of x:

x % 256

which is equivalent to:

x & 255
imgx64
+1  A: 

I usually use them in tight loops, when i have to do something every X loops as opposed to on every iteration..

Example:

int i;
for (i = 1; i <= 1000000; i++)
{
   do_something(i);
   if (i % 1000 == 0)
       printf("%d processed\n", i);
}
+2  A: 

For the control number of international bank account numbers. The mod97 technique:

http://en.wikipedia.org/wiki/International_Bank_Account_Number

Also in large batches to do something after n iterations, here is an exemple for nHibernate:

ISession session = sessionFactory.openSession();
ITransaction tx = session.BeginTransaction();

for ( int i=0; i<100000; i++ ) {
    Customer customer = new Customer(.....);
    session.Save(customer);
    if ( i % 20 == 0 ) { //20, same as the ADO batch size
        //flush a batch of inserts and release memory:
        session.Flush();
        session.Clear();
    }
}

tx.Commit();
session.Close();
Pierre 303