Math Instructions
Perform arithmetic on two numeric sources and write the result to a destination tag. Math instructions act like coils — place them on the right side of a rung.
Available Math Instructions
| Instruction | Symbol | Purpose |
|---|---|---|
| ADD | ADD Source A sourceA Source B sourceB Dest dest | Writes sourceA + sourceB into the destination tag. |
| SUB | SUB Source A sourceA Source B sourceB Dest dest | Writes sourceA − sourceB into the destination tag. |
| MUL | MUL Source A sourceA Source B sourceB Dest dest | Writes sourceA × sourceB into the destination tag. |
| DIV | DIV Source A sourceA Source B sourceB Dest dest | Writes sourceA ÷ sourceB into the destination tag. DINT ÷ DINT truncates toward zero; guard against divide-by-zero. |
Operands
| Name | Type |
|---|---|
sourceA | DINT | REAL |
sourceB | DINT | REAL |
dest | DINT | REAL |
How It Works
When the rung is true, the instruction reads both sources, performs the operation, and writes the result to the destination — once per scan. If the rung stays true, the instruction runs every scan.
Example — Running Total
XIC(PartDetected)ONS(EdgeBit)ADD(TotalParts,1,TotalParts)
Each rising edge of PartDetected adds 1 to TotalParts.
Example — Scale a Reading
Convert a raw ADC value (0–4095) into a percentage (0–100).
MUL(RawValue,100,Scaled);DIV(Scaled,4095,Scaled)
If Scaled is a DINT, the final division truncates — RawValue = 2047 becomes Scaled = 50, not 49.97. Declare Scaled as REAL if you need the fractional precision.
Data Type Notes
DINT / DINTtruncates (integer division).10 / 3=3.- Mixing
DINTandREAL: the result is promoted toREAL. Make sure the destination matches. DIVby zero produces a fault. Guard with a compare:
NE(Divisor,0)DIV(Numerator,Divisor,Result)
Common Mistakes
- Running an
ADDaccumulator without anONS— the value runs away as it adds every scan. - Expecting integer division to round — it truncates toward zero.
- Writing the operation the wrong way around — the destination is the last operand.
Related
- MOVE — copy a value without computing
- Compare instructions — act on the result of a calculation
- One Shot — guard per-scan accumulation