The order of precedence in which operators are evaluated

Expressions can involve several operations:

6 + 2 - 8 / 10 * 20.

It is important to know the order in which the operators in the expression will be evaluated, because it can lead to different results. Each operator in PolyAnalyst has a property called its "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 (exclusive OR)

Consider some of the following examples of expressions with multiple types of operators to get a better understanding of precedence.

  • 1 + 1 >= 2 returns true. The expression first calculates 1 + 1, which results in 2. Next the expression 2 >= 2 is calculated, and since we know 2 = 2 is true, 2 >= 2 is true.

  • 1 > 2 OR 3 = 3 returns true. The expression first calculates 1 > 2, which is false. Next, 3 = 3 is evaluated, which results in true. Finally, false OR true is calculated, which results in true, since at least one of the operands of the OR operator is true.

  • NOT (1 > 2) returns true. 1 > 2 evaluates to false. NOT(false) evaluates to true.

  • 1 + 3 != 4 returns false. 1 + 3 evaluates to 4. 4 != 4 evaluates to false.

  • 2 + 3 AND 5 - 6 results in a syntax error. The values preceding and following the AND operator must be true or false values. 2 + 3 evaluates to the number 5, and 5 - 6 evaluates to the number -1. 5 AND -1 results in a syntax error, because the numbers 5 and -1 are not true/false values. This expression would not be evaluated within PolyAnalyst.

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:

(6 + 2 - 8) / 10 * 20.

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:

6 + (2 - (8 / 10)) * 20

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:

  • 6 + 2 - 8 / 10 * 20 – here we have two sets of parentheses around part of the equation, when only one set is needed to delineate this part to set precedence;

  • ((6 + 2 - 8) / 10 * 20) – here the outermost parentheses, surrounding the entire expression, are not needed at all;

  • 6 + 2 - (8 / 10 * 20) – here the parentheses are not necessary, because we already know that the rules of precedence imply that division and multiplication operations are evaluated first.