What are the practical uses of modulus (%) in programming?

48,787

Solution 1

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.

Solution 2

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);
}

Solution 3

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;
}

Solution 4

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.)

Solution 5

Well, there are many perspectives you can look at it. If you are looking at it as a mathematical operation then it's 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 optimized way.

And modulu division is not limited to finding odd and even numbers or clock arithmetic. There are hundreds of algorithms which need this module operation, for example, cryptography algorithms, etc. So it's a general mathematical operation like other +, -, *, /, etc.

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

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

Share:
48,787
kofucii
Author by

kofucii

Who also have an internet connection, by the way?

Updated on January 18, 2020

Comments

  • kofucii
    kofucii over 4 years

    Possible Duplicate:
    Recognizing when to use the mod operator

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

  • 2ndkauboy
    2ndkauboy almost 14 years
    I couldn't think of any useful use of such an infinite loop, but it looks cool.
  • amcc
    amcc almost 12 years
    @Kau-Boy An array of images to cycle through on a website header maybe, but then it's less trivial to state if(i>count(array))i=1; to start it again. The for loop will however nicely place the required display code inside a block.
  • RoundPi
    RoundPi over 11 years
    This this not generic enough, only works for 2^n.
  • fanbondi
    fanbondi over 11 years
    Great answer and usage.
  • supercat
    supercat over 10 years
    It's important to note that that formula only works for non-negative values of x. If x is negative, x & 255 will correctly report the lower bits, while in most compilers (including all that meet the latest standards) x % 256 will yield garbage.
  • Peter Mortensen
    Peter Mortensen over 10 years
    On some implementations this can be very expensive.
  • eaglei22
    eaglei22 over 6 years
    Can you make a code example?