% Modulo

a = b % c;

 

Where b and c are variables, properties, or expressions that resolve to numbers. a is the remainder of the division b/c.

 

Compatible with Flash 4 or later, although Flash 4 uses an approximate method. This action allows you to tell if one number is divisible exactly by another.

Description

The modulo division arithmetic operator calculates a number corresponding to the "remainder" of a hand calculated long division sum. For example, recall the junior math sum below:

 

 

5 into 292 is calculated as follows;

5 into 2 doesn’t go, so try 5 into 29

5 into 29 is 5 carry 4. Write the 5 and carry the 4.

5 into 42 is 8 carry 2. Write the 8 and carry the 2.

5 into 20 is 4. Write the 4. Because there is no carry, the division is complete, and the answer is 58.4.

 

That division assumes knowledge of decimals, but most kids are taught division before decimal notation, and the division then becomes;

 

5 into 2 doesn’t go, so try 5 into 29

5 into 29 is 5 with carry 4. Write the 5 and carry the 4.

5 into 42 is 8 with carry 2. Because we don’t know about decimals, we can’t carry the 2 anywhere, so we express is as a remainder, and at my school, we expressed this as 58 r2.

 

The "r2" or remainder 2 is the value modulo returns; 292 % 5 is 2.

 

 

Code

 

 

Results

 

 

 

Notes

 

x = 292%5;

y = "292"%5;

z = 292.11%5.2;

a = 292;

b = 5;

c = a%b;

 

x = 2

y = 2

z = 0.910000000000004

c = 2

 

 

 

Modulo will turn all arguments to numbers before performing the modulo division.

 

Although modulo division is normally carried out between integers, using floating-point arguments is acceptable. The value returned, however, is less useful.

 

 

// a is undefined

x = a%5;

y = 292%a;

z = 292%0

 

 

x = 0

y = NaN

z = NaN

 

 

An undefined argument is treated as zero.

 

Examples and practical uses

The uses of the % operator are described as follows.

Motion Graphics

This operator allows you to limit (or "clamp") an increasing positive value so that it never exceeds a maximum value. The following code:

 

ball.onEnterFrame = function() {

   this._x = (this._x+1)%400;

};

 

will prevent instance ball from going off the screen. As soon as ball._x exceeds 400, it will go back to 1, or more generally:

 

a = a%b;

 

will limit a between 0 and b if a is positive and rising.

 

Note that the above event script is a much more efficient way of writing the following:

 

ball.onEnterFrame = function() {

   this._x++

   if (this._x>400){

      this._x = this._x-400

   }

};

 

An example using this event script is included as (modulo).fla and (modulo).swf

 

Modulo is also a very quick way of implementing a snap function. The following code will cause the position of instance ball to jump to the nearest 10 pixel snap position:

 

ballXPos = 0;

pixelSnap = 10;

ball.onEnterFrame = function() {

   ballXPos++;

   ball._x = ballXPos-ballXPos%pixelSnap;

};

 

The use of modulo along with the drawing API would allow the creation of a Flash based draw program with an auto-snap feature.

Programming

The modulo operator is useful when calculating values that can be integers only. For example, the following code would solve this problem:

 

If I have boxes that can hold 6 bottles each, how many boxes do I need to pack 20 bottles?

 

bottles = 20;

bottlesPerBox = 6;

boxes = Math.round(bottles/bottlesPerBox)+(bottles%bottlesPerBox != 0);

 

Math.round(bottles/bottlesPerBox) gives us the number of full boxes. If there is a remainder, then (bottles%bottlesPerBox != 0) will be 1 (true), or 0 (false) if there is no remainder. Adding this second term tells us whether we need an additional part filled box.