2011/10/23

The modulo operator in programming

A little known and often ignored Arithmetic operator in JavaScript and other programming languages used in Web Design (and other applications) is the modulo operator represented by the percent sign (%).

In JavaScript (and most C based languages) the Arithmetic operators are the following:

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Modulus: %
  • Increment (x=x+1): ++
  • Decrement (x=x-1): --
 Most people learn all the others but ignore the modulus operator because they don't understand what it does or how it can be useful. So let us start with the basics.

In mathematics the modulo operator basically finds the remainder, or modulus, of a number after it has been divided by a second number. So 10 % 3 = 1 reads 10 modulo 3 equals 1 and means that after 10 has been divided by 3 there is a remainder of 1 that cannot be divided by 3 without resulting in a floating point or decimal number. Here are a few examples:

  • 57 % 2 = 1
  • 75 % 10 = 5
  • 33 % 3 = 0
  • 4 % 4 = 0
  • 0 % 2 = 0
  • 2 % 0 = error: illegal division by zero
  • 185 % 60 = 5
As can deduct the product of the modulo operator can never be more than the second parameter (the divider). This is especially useful when we must conform a number to a certain range but the input number can be anything e.g. an angle needs to be between 0 and 360, modulo can be used to determine the current angle if more than one revolutions has occurred; or if more than 60 seconds has passed on a counter and you must determine the number of seconds on a second hand of a clock.

So how can you use this operator practically? Well, the previous examples are good and there are many more. However, the most common use for modulo is to determine whether a number is even or odd using the simple formula:
if x % 2 = 0 then "number is even"
else if x % 2 = 1 then "number is odd".

Some other uses are:

  • to determine perfect squares
  • making a clock that counts hours, minutes, and seconds (x % 60 = sec; Math.floor(x/60) % 60 = min)
  • angle calculations (x % 360)
  • non-destructive function for cryptology
  • getting the decimal part of a number (x % 1)
  • getting the day of the week from the day of the year ((x+offset) % 7)
I hope this tutorial has shown you how useful modulo can be and that you can use this in your programming.

4 comments:

  1. Your post was well detailed and I like it. I appreciated it. Thank you for sharing.


    philippine seo

    ReplyDelete
  2. Hi,In the area of Web Design Cochin, a client always faces difficulty understanding the web designer’s language. The reason being client speaks a layman language and designer speaks his designing language.Thanks...

    ReplyDelete
  3. Hi… by the look of your website I gather that you are a professional. Can you help me make money, blogging? I have a few sample articles. Can I send it to you for your review? I have already begun posting my skills on sites like selling my logo design,eye catching logo, and www.gicree.com. Quite a number of buyers have responded. I like to post my blogs on other marketplaces too. Do you have any suggestions?

    ReplyDelete