1. Computing

The JavaScript Modulo Bug

How to Fix It.

By

The easiest way to visualise how modulo numbers work is to imagine a circle with numbers written around the edge. These numbers start from zero and go to one less than the number you are using as the modulo (so that there are exactly that many numbers around the circle including zero). So for modulo 4 for example we have a circle with the numbers 0,1,2,3 written around the edge. To work out the modulo 4 of any number we start from the zero and count around the circle until we have counted off the number we want the modulo 4 of and the number that appears on that spot around the circle is the modulo 4 of our starting number. So if we want 6 modulo 4 we start counting 1,2,3,0,1,2 and so our answer is 2. If we want -5 modulo 4 we count around the circle the other way 3,2,1,0,3 giving an answer of 3. Note that with modulo numbering the only numbers that exist are greater than or equal to zero and less than the number of the modulo value.

Another way of looking at the modulo value is that it it the remainder that is left after dividing the original number by that modulo value so 6 modulo 4 can also be calculated by dividing 6 by 4 giving 1 with a remainder of 2.I am sure you can remember doing divisions like this back in your school days prior to learning about fractions.

Many programming languages (including JavaScript) use the % symbol to represent a modulo operation so that our 6 modulo 4 can be written in JavaScript as 6%4.

The problem is that JavaScript doesn't process modulo values correctly when you start with a negative number. With modulo arithmatic in modulo 4 the only numbers that exist are 0,1,2, and 3 and yet if you ask for -5%4 JavaScript does not return the correct answer of 3 but instead returns -1 (a number which doesn't exist in modulo 4 since modulo numbers don't have negatives). The strange thing is that when you divide -5 by 4 in JavaScript and use Math.floor() to convert it to an integer you get the correct answer of -2 so producing the wrong answer for the modulo doesn't even fix itself in those situations where you combine its use with the division.

So having identified the problem, how can we fix it? Well probably the simplest way is to add the modulo number to the answer achieved from the first modulo and then take the modulo again. So instead of -5%4 which gives the wrong answer in JavaScript we substitute ((-5%4)+4)%4 which gives the correct answer regardless of whether the number we start with is positive or negative.

So let's look at how we can add our own modulo method to JavaScript which always gives the right answer rather than getting it wrong the way JavaScript does for negative numbers.


Number.prototype.mod = function(n) {
return ((this%n)+n)%n;
}

With this method added to our numbers we then just replace all the x%y references in our code with x.mod(y) and our code will now return the correct answer for when the value of that x is negative.

  1. About.com
  2. Computing
  3. JavaScript
  4. Problem Solving
  5. The JavaScript Modulo Bug

©2014 About.com. All rights reserved.