Skip to main content

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

InstructionSymbolPurpose
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

NameType
sourceADINT | REAL
sourceBDINT | REAL
destDINT | 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 (04095) into a percentage (0100).

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 / DINT truncates (integer division). 10 / 3 = 3.
  • Mixing DINT and REAL: the result is promoted to REAL. Make sure the destination matches.
  • DIV by zero produces a fault. Guard with a compare:
NE(Divisor,0)DIV(Numerator,Divisor,Result)

Common Mistakes

  • Running an ADD accumulator without an ONS — 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.