Arithmetic Operators¶
Arithmetic operators perform mathematical computations on numeric values.
IEC 61131-3 |
Section 3.3.1 |
Support |
Supported |
Syntax¶
Operator |
Syntax |
Description |
|---|---|---|
|
|
Addition |
|
|
Subtraction |
|
|
Multiplication |
|
|
Division (integer division for integer types) |
|
|
Modulo (remainder after division) |
|
|
Exponentiation (power) |
|
|
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 |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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¶
Comparison Operators — relational operators
Logical Operators — boolean operators
Assignment — storing expression results