SUB

Returns the difference of two inputs.

Signature

     ┌─────────┐
IN1 ─┤         │
     │   SUB   ├─ OUT
IN2 ─┤         │
     └─────────┘
FUNCTION SUB : ANY_NUM
  VAR_INPUT
    IN1 : ANY_NUM;
    IN2 : ANY_NUM;
  END_VAR
END_FUNCTION

The return type matches the input type. SUB accepts SINT, INT, DINT, LINT, USINT, UINT, UDINT, ULINT, REAL, LREAL. Inputs of different numeric types widen to the wider one, which is also the return type; see Type Conversions. The time and date types are covered by the overloads below.

Inputs

Name

Type

Description

IN1

ANY_NUM

The minuend.

IN2

ANY_NUM

The subtrahend.

Outputs

Name

Type

Description

Return value

ANY_NUM

IN1 minus IN2. Same type as the inputs.

Description

Returns IN1 minus IN2. SUB(a, b) is the functional form of the - operator: a - b. Both forms are equivalent.

For integer types, underflow behavior wraps around (modular arithmetic).

Time and date overloads

SUB and the - operator are also defined on the following time and date operands (IEC 61131-3 Table 30). Each combination is the typed function in the last column and computes what it computes.

IN1

IN2

Return value

Same as

TIME

TIME

TIME

SUB_TIME

DATE

DATE

TIME

SUB_DATE_DATE

TIME_OF_DAY

TIME

TIME_OF_DAY

SUB_TOD_TIME

TIME_OF_DAY

TIME_OF_DAY

TIME

SUB_TOD_TOD

DATE_AND_TIME

TIME

DATE_AND_TIME

SUB_DT_TIME

DATE_AND_TIME

DATE_AND_TIME

TIME

SUB_DT_DT

d1 - d2 on two DATE values is therefore a TIME, not a DATE.

Each typed function has a long form over LTIME, LDATE, LTIME_OF_DAY and LDATE_AND_TIME (SUB_LTIME, SUB_LDATE_LDATE, SUB_LTOD_LTIME, SUB_LTOD_LTOD, SUB_LDT_LTIME, SUB_LDT_LDT), which applies when either operand is of a long type.

Any other combination of types is an error (P4049).

Example

result := SUB(30, 10);   (* result = 20 *)
result := 30 - 10;       (* result = 20, operator form *)

See Also

  • ADD — addition

  • MUL — multiplication

  • DIV — division

References