Wednesday 29 October 2014

The Ecology of the Modulus

The four most commonly-used mathematical operators in programming are obvious - addition (+), subtraction (-), multiplication (*) and division (/). But there's a fifth.

The Modulus, commonly known as "Remainder", is used to derive the remainder from a dividend (a) and divisor (b). Basically, when you ask for the Modulus of 3 and 2, you get 1. (3 divided by 2, remainder 1) That is what you would get, assuming that both your dividend and divisor are positive whole numbers.

The Modulus can be an elusive and frustrating operator as its execution varies from language to language. Useful enough to warrant fairly moderate usage, yet not frequently-used enough to stick in the mind. I have found myself looking it up repeatedly whenever I needed to use it because I would forget it soon after. Admittedly this happened because at that time, I was switching between languages, a necessary evil when one is  maintaining legacy code.

Just for posterity, I have included the various forms of the Modulus in some of the different languages I had to use it in. (Note that this list is by no means exhaustive)

LanguageOperatorExample
PHP, C++, C#, JavaScript, Flash ActionScript, Python (and most other languages whose syntax is based on C)%a % b
Coldfusion, Basic, VBScriptmoda mod b
SQLmod(x,y)mod(a,b)

So, what can the Modulus do for you?

Beyond the obvious function of giving you the remainder when a is divided by b, The Modulus helps you determine if a given number is odd or even. Or if the number is divisible by another number. Basically, if x%2 equals 0, then x is an even number. If x%y equals 0, then x is divisible by y. Again, this only works if both your dividend and divisor are positive whole numbers, so adjust your exceptions accordingly!

Thus, it also follows that the Modulus is great at pattern sequences.

If, for example, you need a table with 5 rows that is colored differently every alternating row, you might do this:

<table>
   <?php
     for ($i=1;$i<=5;$i++) 
    {
   ?>
  <tr>
      <td style="background-color:<?php echo ($i%2==0?"#AAAAAA":"#EEEEEE");?>">Test row</td>
  </tr>
   <?php
     }
   ?>
</table>

That would net you this:

Test row
Test row
Test row
Test row
Test row

 Or if you wanted a table of 20 rows with a different colored row every 5 rows...

<table>
   <?php
     for ($i=1;$i<=20;$i++) 
    {
   ?>
  <tr>
      <td style="background-color:<?php echo ($i%5==0?"#AAAAAA":"#EEEEEE");?>">Test row</td>
  </tr>
   <?php
     }
   ?>
</table>

Test row
Test row
Test row
Test row
Test row
Test row
Test row
Test row
Test row
Test row
Test row
Test row
Test row
Test row
Test row
Test row
Test row
Test row
Test row
Test row

Note that CSS3 has built-in this particular functionality without the need for coding. However, the point here is to illustrate, as simply as possible, the Modulus operator's capability.

In fact, try the above exercise with a nested loop. (see below) Or recursively. The possibilities are endless. And this is just the simple stuff!

<table>
   <?php
     for ($i=1;$i<=5;$i++) 
    {
   ?>
  <tr>
   <?php
        for ($j=1;$j<=5;$j++) 
       {
           if ($j%2==0) 
          {
   ?>
      <td style="background-color:<?php echo ($i%2==0?"#AAAAAA":"#EEEEEE");?>">x</td>
   <?php
           }
           else 
           {
   ?>
       <td style="background-color:<?php echo ($i%2==0?"#EEEEEE":"#AAAAAA");?>">x</td>
   <?php
            }
        }
   ?>
   </tr>
   <?php
     }
   ?>
</table>

x x x x x
x x x x x
x x x x x
x x x x x
x x x x x

Signing off with much Modu-love
T___T

1 comment: