Math (output instruction)
DIV: Divide
Writes A divided by B into the destination.
Also called: divide, quotient.
DIV(number, a, b, real)Arguments
| Name | What it is | Accepts | Default |
|---|---|---|---|
| destination | What the instruction acts on | a register | required |
| a | Value A | a number or a tag | 0 |
| b | Value B | a number or a tag | 1 |
| real | Floating point | true or false | false |
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
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 simulatorWhat it is called on other controllers
| Platform | Equivalent |
|---|---|
| Rockwell (Studio 5000) | DIV |
| Siemens (TIA Portal) | DIV |
| Mitsubishi (GX Works) | The divide instruction, written as / |
| CODESYS and IEC 61131-3 | DIV, 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.
