Expressions in ST
An expression is a language construct that consists of a defined combination of operands (like →literals, →variables, →calls) and operators (like +, -, *, /
) and yields one value of a particular data type.
Neuron Power Engineer applies the following rules when evaluating the expressions:
-
The operators apply the operands in a sequence defined by the operator precedence: The operator with highest precedence in an expression is applied first, followed by the operator of next lower precedence, etc., until evaluation is complete. Operators of equal precedence are applied from left to right.
Example: IfA
,B
,C
are of typeINT
with values1
,2
and4
, respectively, then the expressionA+B-C*(C / B)
is evaluated as follows:-
The parentheses
(C / B)
is evaluated at first, due to the highest precedence: It equals to(4 / 2)
and returns value2
. -
The multiplication
C*(2)
is evaluated next, due to the next lower precedence: It equals to4*(2)
and returns value8
. -
The addition
A+B
and subtractionB-8
are operators of equal precedence. As addition is left of subtraction, addition is applied first. Addition equals to1+2
and returns value3
. -
At last, the subtraction
3-8
is applied: Hence, the result of the expression is the value-5
.
-
-
When an expression has two operands, the leftmost operand is evaluated first.
Example: In the expressionEQ(A,B)=AND(A,B)
the call of theEQ
-blockEQ(A,B)
is evaluated first, followed by the call of theAND
-blockAND(A,B)
, followed by the equality operation (due to operator=
). -
→Boolean expressions are evaluated only to the extent necessary to determine the resultant value including possible side effects.
Example: IfA<=B
, it is sufficient for the expression(A>B)&(C<D)
to evaluate only(A>B)
to decide that the result of the expression is valueFALSE
(or an equivalent). -
Expressions without a concrete data type are typed. See "Typing of expressions" for explanations on this.
No detection of mathematical errors If you assign expressions in Neuron Power Engineer, it is possible that mathematical errors are not detected and reported
Additional information: If →constant expressions are used when initializing variables, errors are no longer detected. Workaround: Check with suitable tools whether the specified expression can be mathematically evaluated. |
Avoid using complex expressions with more than 20 operands/operators in order to avoid considerable implications on the performance when saving or when importing. |