Arithmetic Operators

Arithmetic operators perform mathematical computations on numeric values.

IEC 61131-3

Section 3.3.1

Support

Supported

Syntax

Operator

Syntax

Description

+

a + b

Addition

-

a - b

Subtraction

*

a * b

Multiplication

/

a / b

Division (integer division for integer types)

MOD

a MOD b

Modulo (remainder after division)

**

a ** b

Exponentiation (power)

-

-a

Unary negation

Description

Arithmetic operators apply to integer types (SINT, INT, DINT, LINT, USINT, UINT, UDINT, ULINT) and floating-point types (REAL, LREAL). For integer division, the result is truncated toward zero. The MOD operator returns the remainder of integer division and is defined only for integer types.

When the two operands have different numeric types, the narrower one widens to the wider, and the operation is carried out at the wider type, which is also the result’s type: INT + DINT is a DINT, and INT + REAL is a REAL. Two types where neither widens to the other, such as DINT and REAL, are an error (P4049). See Type Conversions.

+, -, * and / are also defined on the time and date types, each combination being one of the typed time functions:

Expression

Result

Same as

TIME + TIME, TIME - TIME

TIME

ADD_TIME, SUB_TIME

TIME_OF_DAY + TIME, TIME_OF_DAY - TIME

TIME_OF_DAY

ADD_TOD_TIME, SUB_TOD_TIME

DATE_AND_TIME + TIME, DATE_AND_TIME - TIME

DATE_AND_TIME

ADD_DT_TIME, SUB_DT_TIME

DATE - DATE

TIME

SUB_DATE_DATE

TIME_OF_DAY - TIME_OF_DAY

TIME

SUB_TOD_TOD

DATE_AND_TIME - DATE_AND_TIME

TIME

SUB_DT_DT

TIME * number, TIME / number

TIME

MUL_TIME, DIV_TIME

The long types (LTIME, LDATE, LTIME_OF_DAY, LDATE_AND_TIME) combine the same way, with each type in the table replaced by its long form; a combination with at least one long operand has the long result. Arithmetic on bit-string types requires --allow-bit-string-arithmetic; see Type Conversions.

The unary negation operator - has higher precedence than the binary arithmetic operators. Exponentiation ** has higher precedence than multiplication, division, and modulo.

Example

PROGRAM main
    VAR
        a : INT := 17;
        b : INT := 5;
        sum : INT;
        diff : INT;
        product : INT;
        quotient : INT;
        remainder : INT;
    END_VAR

    sum := a + b;          (* 22 *)
    diff := a - b;         (* 12 *)
    product := a * b;      (* 85 *)
    quotient := a / b;     (* 3 *)
    remainder := a MOD b;  (* 2 *)
END_PROGRAM

See Also