Skip to content

SUB — Subtract

Category
Compute
Type
Output
Availability
All platforms

SUB instruction block: Source A Tank_Full, Source B Used, Dest Remaining


SUB (Subtract) writes Source A minus Source B to Dest every scan its rung is true. It is an output instruction with no state and no status bits. Use SUB for a remaining quantity, an error between setpoint and measurement, or an offset removed from a raw reading. Do not use SUB to count down; an ungated SUB Left 1 Left subtracts one every scan. Use CTD for events, or gate the SUB with an ONS.


Operand Type Format Valid Range Required Description
Source A Any numeric tag, member, element or bit, or a literal Tag, Tag.Member, Tag[n], Tag.bit, or a literal Any resolvable numeric reference Yes The value subtracted from.
Source B Same as Source A Same Any resolvable numeric reference Yes The value subtracted.
Dest A writable numeric tag, member or element Tag, Tag.Member, Tag[n] SINT, INT, DINT or REAL; not a literal, not an input pin Yes Where the difference is written.

Literals may be whole numbers (25, -3, 0x1F) or decimals (72.5). A BOOL source reads as 0 or 1. Dest may be the same tag as a source.

Dest type. A whole-number Dest truncates a fractional result toward zero and wraps silently past its range (INT ±32767, DINT ±2,147,483,647); a REAL keeps the fraction. Every type is signed, so a result below zero is stored as a negative number, never clamped.


Nothing. Dest keeps its power-up value.

The instruction does nothing. Dest holds its last value.

Both sources are read, Source B is subtracted from Source A and the result is written to Dest, every scan the rung is true.

Nothing.


Scenario: A day tank holds 500 litres when full. A flow meter accumulates what has been drawn off, and the operator display shows what is left.

Tags:

  • Update — Recalculate, BOOL
  • Tank_Full — Full capacity in litres, INT
  • Used — Litres drawn, INT
  • Remaining — Litres left, INT

Rung 1:

—] [Update———[SUB Tank_Full Used Remaining]———

Scan 1 — Update = 0. Tank_Full reads 500 and Used reads 135. The rung is false, so Remaining still shows 0.

Figure 1 — SUB rung false: Tank_Full 500, Used 135, Remaining 0, rung dark

Scan 2 — Update = 1. 500 − 135 is written: Remaining reads 365.

Figure 2 — SUB rung true: Remaining reads 365, rung lit

If Used ever exceeds Tank_Full, Remaining goes negative. Test for that with LES if the display should clamp at zero.


  • ADD — Add
  • NEG — Negate (0 − Source in one operand)
  • ABS — Absolute Value (the size of a difference regardless of sign)
  • CPT — Compute
  • Compute Instructions — Category index

Control error. SUB Setpoint Measured Error is the classic use. Follow it with ABS when only the size of the error matters, or use LIM on Error for a deadband.

Counting down. SUB Left 1 Left behind an ONS decrements once per event. Without the ONS it runs every scan.

The block never colours. Watch the rung wire and the Dest value line.

Applies to LadderIDE >=1.2.2 · Last reviewed 2026-09-11 · Screenshots verified 2026-09-11