Operator Precedence in Circuit Expressions
Expressions are evaluated using the following order of operator precedence.
- Parentheses may be used to group operations. Operations in the innermost pair of parentheses are evaluated first. The result of the operations within a set of parentheses becomes an operand for the next level of evaluation.
- The arguments to functions can be expressions. An expression that is used as the argument to a function is evaluated before the function is called.
- Functions have higher precedence than operators outside of functions. When functions are nested, the innermost function is evaluated first.
- Unary minus (-) and unary NOT (!) operators are applied to their operands before binary operators are applied. If parentheses are not used, a unary operator applies only to the very next operand.
- For example, if A=2 and B=4, '-A+B' is evaluated as '(-A)+(B)'
with value 2, while
'-(A+B)' is evaluated as '(-A)+(-B)' with value -6. - Similarly, if A=2 and B=4, the expression '!A<B' is evaluated as '(!2)< 4', with value 1 or true, while '!(A<B)' is evaluated as '!(1)', with value 0 or false.
- Power (**) is the highest binary operator. When two exponentiation operations are present without parentheses to control the precedence, evaluation is left to right.
- For example, 'A**B**C' is evaluated as '(A**B)**C)'.
- Multiply (*) and divide (/) have equal precedence. When two multiplies or divides are present without parentheses to control the precedence, evaluation is left to right.
- For example, '-A/B/C' is evaluated as '((-A)/B)/C'.
- Add (+) and subtract (-) have equal precedence. When two additions or subtractions are present without parentheses to control the precedence, evaluation is left to right.
- For example, 'A+B-C' is evaluated as '(A+B)-C'.
- Relational operators (>, <, =, ==, <>, >=, <=) have equal precedence. When two relational operators are present without parentheses to control the precedence, evaluation is left to right.
- For example, 'A>B=C' is evaluated as '(A>B)=C'.