Math (output instruction)

MOD: Modulo

Writes the remainder of A divided by B.

Also called: remainder, modulus.

MOD(number, a, b, real)

Arguments

NameWhat it isAcceptsDefault
destinationWhat the instruction acts ona registerrequired
aValue Aa number or a tag0
bValue Ba number or a tag1
realFloating pointtrue or falsefalse

Arguments are positional and this table is generated from the simulator's own instruction registry, so it always matches what the engine accepts.

What it does

MOD gives you what DIV throws away. Nineteen divided by five is three remainder four, and MOD is the four.

Its most useful trick is periodicity. A counter that only ever goes up, fed through MOD, becomes a value that cycles: every fifth part, every tenth pallet, every twelfth position of a rotary table. The counter keeps its running total for the shift report and the remainder drives the action.

It is also how a total is broken into units. Seconds into minutes and seconds, a tick count into hours, an index wrapped around the length of a table: each is a DIV for the whole part and a MOD for what is left over.

Dividing by zero writes 0 here, the same as DIV, and the sign of the remainder follows the first value, so -19 mod 5 is -4 rather than 1.

A rung you can run

Divert every fifth part

RUNG 1PPart_SensorADD Part_IndexPart_Ind… + 1RUNG 2MOD Part_RemainderPart_Ind… mod 5RUNG 3Part_Remainder= 0Part_SensorDivert_Gate
ONS(Part_Sensor)ADD(Part_Index,Part_Index,1)
MOD(Part_Remainder,Part_Index,5)
EQU(Part_Remainder,0)XIC(Part_Sensor)OTE(Divert_Gate)

The index counts every part and never resets, so it is still the shift total. The remainder cycles 1, 2, 3, 4, 0, and the gate fires on the zero. Toggle Part_Sensor and watch the fifth one go to the diverter.

Run this rung in the simulator

What it is called on other controllers

PlatformEquivalent
Rockwell (Studio 5000)MOD
Siemens (TIA Portal)MOD
Mitsubishi (GX Works)No separate instruction: the divide stores a remainder alongside the quotient
CODESYS and IEC 61131-3MOD

Common mistakes

  • Assuming the remainder is never negative. It follows the sign of the value being divided, so a wrapped index built on a value that can go negative lands outside the range you expected. Take ABS first, or keep the source value positive.
  • Using MOD where a counter would be clearer. Every fifth part can be a CTU with a preset of 5 and a RES on its own done bit, and on a small machine that rung is easier for the next person to read.

Practise it

Interactive exercises that use MOD. Each one checks your ladder against a real scan.

Related instructions

All ladder logic instructions