Math (output instruction)

DIV: Divide

Writes A divided by B into the destination.

Also called: divide, quotient.

DIV(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

DIV completes the scaling pair, and it carries the two traps that make integer arithmetic on a controller different from arithmetic on paper.

The first is truncation. With whole numbers, 7 divided by 2 is 3, not 3.5, and the remainder is simply discarded. If you need it, MOD gives it to you; if you need a fraction, the instruction has a floating point setting.

The second is dividing by zero. On a real controller that is a fault, and depending on the platform it can stop the machine. In this simulator it writes 0 to the destination and carries on, which is gentler but arguably more dangerous, because nothing at all tells you it happened. Either way the fix is the same and it is not optional: put a compare in front of any divide whose divisor is a tag, so the instruction only runs when the divisor cannot be zero.

A rung you can run

An average that cannot divide by zero

RUNG 1Cycle_Count> 0DIV Avg_Cycle_MsTotal_Ms ÷ Cycle_Co…RUNG 2Cycle_Count≤ 0CLR Avg_Cycle_Ms:= 0RUNG 3Avg_Cycle_Ms> 5000Running_Slow
GRT(Cycle_Count,0)DIV(Avg_Cycle_Ms,Total_Ms,Cycle_Count)
LEQ(Cycle_Count,0)CLR(Avg_Cycle_Ms)
GRT(Avg_Cycle_Ms,5000)OTE(Running_Slow)

The compare on the first rung is the guard: the divide only runs once at least one cycle has been recorded. The second rung keeps the answer honest at zero cycles rather than leaving a stale average on the screen. Set Cycle_Count to 0 and watch the divide simply not execute.

Run this rung in the simulator

What it is called on other controllers

PlatformEquivalent
Rockwell (Studio 5000)DIV
Siemens (TIA Portal)DIV
Mitsubishi (GX Works)The divide instruction, written as /
CODESYS and IEC 61131-3DIV, or / in structured text

Common mistakes

  • Dividing by a tag without guarding it. A count, a total or an elapsed time is zero before the machine has run, and that is exactly when the rung first executes. One GRT in front of the DIV is the whole fix.
  • Expecting a fraction. Whole number division truncates towards zero, so an average of 9.8 reads as 9. Scale up before dividing, or switch the instruction to floating point.

Practise it

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

Related instructions

All ladder logic instructions