Operators precedence
Expressions can involve several operations:
Each operator in PolyAnalyst has a property called a "precedence". Certain operators are always evaluated before other operators. After considering the precedence, then PolyAnalyst works from left to right:
1) In the above example, the multiplication and division operators would be evaluated first, because these operators have a higher precedence than addition and subtraction.
2) Multiplication and division both have the same level of precedence, so PolyAnalyst would first evaluate 8 / 10, starting from left to right which result in 0.80.
3) Next PolyAnalyst evaluates the multiplication, 0.80 * 20, resulting in the value 16.00.
4) So now the expression is evaluated as 6 + 2 - 16.00. Since addition and subtraction are at the same level of precedence, working from left to right, we first evaluate 6 + 2, resulting in 8.
5) Next we evaluate 8 - 16.00, resulting in -8.00.
It is always important to keep in mind operator precedence when creating expressions to avoid accidentally writing expressions that will produce the wrong output and be evaluated differently than you expected. Here is a brief reference of the precedence of operators. Note that operators at the same level of precedence are simply evaluated from left to right.
The general precedence of the SRL elements is shown below (in descending order):
-
1 - expressions in parentheses
-
2 - functions
-
3 - unary
+and-(e.g. negative number sign) -
4 -
*,/,%arithmetical multiplication and division operations -
5 - addition and subtraction
-
6 - equivalence operators
-
7 -
NOT(logical negation),AND -
8 -
OR,XOR(exclusiveOR)
Consider some of the following examples of expressions with multiple types of operators to get a better understanding of precedence.
-
1 + 1 >= 2returns True.
The expression first calculates1 + 1, which results in2. Next the expression2 >= 2is calculated, and since we know2 = 2is True,2 >= 2is True. -
1 > 2 OR 3 = 3returns True.
The expression first calculates 1 > 2, which is False. Next,3 = 3is evaluated, which results in True. Finally,False OR Trueis calculated, which results in True, since at least one of the operands of the OR operator is True. -
NOT (1 > 2)returns True.
1 > 2evaluates to False.NOT Falseevaluates to True. -
1 + 3 != 4returns False.
1 + 3evaluates to4.4 != 4evaluates to False. -
2 + 3 AND 5 - 6results in a syntax error.
The values preceding and following theANDoperator must be True or False values.2 + 3evaluates to the number5, and5 - 6evaluates to the number-1.5 AND -1results in a syntax error, because the numbers5and-1are not True/False values.
How to use parentheses to force the order of operators
To help avoid some the confusion that arises when using multiple operators in an expression, you can use parentheses.
| Placing a part of an expression inside parentheses forces PolyAnalyst to evaluate it first. |
| Parentheses also play a role with functions, i.e. parentheses that are part of the function syntax do not play a role in precedence. |
Returning to our above equation of 6 + 2 - 8 / 10 * 20, suppose we want to force PolyAnalyst to perform the addition and subtraction before the division and multiplication. We could rewrite the expression as follows:
When PolyAnalyst evaluates the expression, it will first evaluate 6 + 2 - 8, to get the value of 0, and then evaluate 0 / 10 * 20.
Parentheses can also be nested within each other, as in the following example:
In this case, PolyAnalyst first evaluates the innermost expression, i.e. 8 / 10, resulting in the value 0.80. Next, it evaluates the next expression within the parentheses, 2 - 0.80, resulting in 1.20. Now the expression is 6 + 1.20 * 20. Evaluating from left to right, multiplication takes precedence over addition, and 1.20 * 20 is evaluated first, resulting in the value 24.00. Finally, 6 + 24.00 is evaluated, resulting in the value 30.00.
It is a good practice to use parentheses liberally throughout your expressions as they improve the readability of your expression. There are also times when you should avoid using parentheses.
Consider the following examples:
-
the outermost parentheses, surrounding the entire expression, are not needed at all, e.g.
((6 + 2 - 8) / 10 * 20)
-
the parentheses are not necessary, i.e. the rules of precedence imply that division and multiplication operations are evaluated first, e.g.
6 + 2 - (8 / 10 * 20)