views:

132

answers:

10

What are some uses of the modulus operator? I know that it calculates the remainder in division so really I am asking what uses does the remainder have?

So far I have used it to check if a number was even and alternate colors on a table.

A: 

Bitwise calculations, including conditional checking.

Andrew Sledge
codymanix
True...I use it a lot more for conditional checking against queued pairs.
Andrew Sledge
+2  A: 
  • Unit conversion, e.g. 13425 m is 13425 / 1000 km and 13425 % 1000 m = 13 km and 425 m
  • random number trimming, if you're using C/C++'s rand(), a common idiom is rand() % (HIGH - LOW) + LOW to generate a random number between HIGH and LOW
  • modular arithmetic: angles are limited to 360 degrees or 2*pi, you can normalize their range using modulus operator
  • even/odd check: if "n % 2" is true then n is even otherwise it's odd
Lie Ryan
+2  A: 
  • Primes
  • Convert numbers from base x to base y
codymanix
+6  A: 
for(int i=0;i<10;i++)
{
  if((i % 2) == 0 )
  {
   // I'm in an even row
  }else{
   // I'm in an odd row
  }
}  

The most basic use

Note: lang used Java

org.life.java
you seem to have that backwards. even numbers mod to 0 which is false. Also, might it be simpler to just unroll it and have two steps in the loop, for both even and odd?
Tesserex
@Tesserex didn't get you
org.life.java
i mean instead of using the mod operator, your loop body could contain: code for even row; i++; code for odd row. It's really about readability, which will depend on the exact scenario.
Tesserex
@Tesserex :) Here I am demonstrating the most basic and simpler use of `%` operator , I didn't say this is the most efficient code
org.life.java
A: 

A programming 101 exapmle would be to modulate row colors for data:

for(int i = 0; i < 100; i++)
{
    write-color i % 2;
}

Determine if a number is evan or odd:

return number % 2;
Nate Bross
+4  A: 

Getting an indication of progress in a long running loop by printing a message once every so many iterations.

List<Thing> bigList = readBigList();

for (int i = 0; i < bigList.size(); i++) {
    processThing(bigList.get(i));
    if (i % 10000 == 0) {
        LOG.info("Processed " + i + " out of " + bigList.size() + " items");
    }
}
oksayt
A: 

Chinese arithmetic (is that the preferred nomenclature, dude?)

Peter Turner
Are you referring to `Chinese Remainder Theorem`?
Lie Ryan
@Lie Yeah, see Chapter 39 of A.K Dewdney's "The Turing Omnibus" not sure why, but the subtitle to the chapter is "Chinese Arithmetic" and he writes about the Chinese Remainder Theorem.
Peter Turner
A: 

The modulus operator is the single-most important operator in Clock Arithmetic.

Ani
This is where I use it most often, it makes it easy to setup a repeating sequence of numbers by just adding 1 to the counter each clock tick. The last time I did this it was to drive the animation loop in a sprite.
asawyer
+1  A: 

It's generally used to check if one number is evenly divisible by another.

if(number % 2 == 0){
    // the number is even
} else {
    // the number is odd
}

or

if(number % 3 == 0){
    // the number is evenly divisible by three
} else {
    // the number is not evenly divisible by three
}

If the result of a mod operation is 0, the dividend (number) is evenly divisible by the divisor.

You can take advantage of this to do things like "piano-keys" style alternate-row shading on table data, or printing new column headings every X number of rows, or what have you.

Brad
A: 

72 minutes modulo 60 = 12 minutes past the hour

smirkingman