This topic has information on the precedence and grouping properities of arithmetic, matrix, logical, comparison and bit operations. |

Operators in MacAnova such as '+', '/', '<', '&&' and '<-' have rules of association and precedence which determine the order in which they are evaluated when used together. As much as possible these mimic the rules of ordinary algebra where they apply and for most purposes that is all you need to know. This topic summarizes the rules and includes a precedence table for all operators. Association properties of operators A binary operator OP such as '+', '^' or '<=' either associates from left to right, that is, x OP y OP z means (x OP y) OP z, or from right to left, that is x OP y OP z means x OP (y OP z), or does not associate at all, that is x OP y OP z is meaningless. Binary arithmetic operators '+', '-', '*', '/', and '%%' associate from left to right. For example, x - y - z means (x - y) - z and x/y/z means (x/y)/z. See topic 'arithmetic'. Exponentiation ('^' or '**') associates from right to left, that is x^y^z is x^(y^z), not (x^y)^z. See topic 'arithmetic'. Binary logical operators '&&' and '||' associate from left to right. For example, u && v && w means ((u && v) && w). See topic 'logic'. Matrix multiplication operators '%*%', '%c%' and '%C%' associate from left to right. For example, x %*% y %c% z %C% w is interpreted as ((x %*% y) %c% z) %C% w. See topic 'matrices'. The assignment operator '<-' and arithmetic assignment operators '<-+', '<--', '<-*', '<-/', '<-^' and <-%%' (see topic 'arithmetic') also associate from right to left. That is, x <- y <- z is interpreted as x <- (y <- z) and x <-+ y <-+ z is interpreted as x <-+ (y <-+ z). See topics 'assignment' and 'arithmetic'. Comparison operators do not associate, that is, for example, x < y < z has no meaning. See topic 'logic'. Precedence of operators "Precedence" has to do with the interpretations of expressions involving more than one operator, for example 'x <- 3 + 4/5^2' which involves operators '<-', '+', '/' and '^'. Every operator has a numerical precedence level. The rule is simple: Operators with higher precedence are evaluated before operators with lower precedence. Here is a table of precedence levels for MacAnova operators: Precedence Meaning x %| y 1 Bitwise Or (OR) x %^ y 2 Bitwise Exclusive Or (XOR) x %& y 3 Bitwise And (AND) %!x 4 Bitwise Complement (COMPL) x || y 5 Logical Or x && y 6 Logical And !x 7 Logical Not x == y 8 Equal or same x != y 8 Not equal or different x < y 8 Less than x <= y 8 Less than or equal x > y 8 Greater than x >= y 8 Greater than or equal x + y 9 Addition (sum of x and y) x - y 9 Subtraction (difference of x and y) x * y 10 Multiplication (product of x and y) x / y 10 Division (x divided by y) x %% y 10 Modular division (x - y*floor(x/y)) x %*% y 11 x MatMult y x %c% y 11 transpose(x) MatMult y x %C% y 11 x MatMult transpose(y) -x 12 Unary minus ((-1)*x) +x 12 Unary plus ((+1)*x) x ^ y or x ** y 13 Exponentiation (x to the y-th power) x <- y 14 or 0 Assign value of y to x x <-+ y 14 or 0 x <- x + y x <-- y 14 or 0 x <- x - y x <-* y 14 or 0 x <- x * y x <-/ y 14 or 0 x <- x / y x <-^ y 14 or 0 x <- x ^ y x <-** y 14 or 0 x <- x ** y x <-%% y 14 or 0 x <- x %% y In the above MatMult is ordinary matrix multiplication. You may use parentheses to group terms and change the order of evaluation. Subexpressions within '(...)' or '{...}' are evaluated before the bracketed terms are combined. The dual levels 0 and 14 for the assignment operators reflect the fact that they have lower precedence than any operator to their right and higher precedence than any operator to their left. For example, x <- y + z sets x to y + z while x + y <- z assigns z to y and then computes the sum of x and the new value of y. Similarly x <-+ y + z is equivalent to x <-+ (y+z) and x + y <-+ z is equivalent to x + (y <-+ z). Examples Expression Interpretation Value Explanation 30/5/2 (30/5)/2 3 / associates to left 3^2^4 3^(2^4) 43046721 ^ associates to right 4*3-2 (4*3) - 2 12 - 2 = 10 * has higher precedence than - 3*2^4 3*(2^4) 3*16 = 48 ^ has higher precedence than * 3<2+4 3 < (2+4) 3 < 6 = T + has higher precedence than < (3*2)^4 6^4 = 1296 Parentheses change evaluation order -2^4 -(2^4) -16 ^ has higher precedence than prefix - (-2)^4 16 Parentheses change evaluation order T||F&&F T||(F&&F) T||F = T && has higher precedence than || !T||T (!T)||T F||T = T ! has higher precedence than || !(T||T) !T = F Parentheses change evaluation order See topic 'bit_ops' for examples of how precedence and parentheses affect the order of evaluation of operations '%&', '%|', '%^' and '%!'.

Gary Oehlert 2003-01-15