Next: , Previous: , Up: Expressions   [Contents][Index]


4.2.1 Arithmetic expressions

coNCePTuaL supports a variety of arithmetic expressions. The following is the language’s order of operations from highest to lowest precedence:

unary +’, ‘-’, NOT, ‘ <function>( <expr>, …)’, ‘REAL( <expr>)
power **
multiplicative *’, ‘/’, MOD, ‘<<’, ‘>>’, ‘&
additive +’, ‘-’, ‘|’, XOR
conditional <expr> IF <rel_expr> OTHERWISE <expr>

In addition, as in most programming languages, parentheses can be used to group subexpressions.

The ‘&’ (“and”), ‘|’ (“or”), XOR, and NOT operators perform bitwise, not logical, operations. That is, they accept numerical arguments, not truth-value arguments. Hence, for example, ‘3 | 5’ is equal to ‘7’.

<<’ and ‘>>’ are bit-shift operators. That is, ‘a << b’ is the coNCePTuaL equivalent of the mathematical expression a * 2^b and ‘a >> b’ is the coNCePTuaL equivalent of the mathematical expression a / 2^b. Consequently, negative values of b are valid and correspond to a shift in the opposite direction. (In contrast, C and Perl treat a negative shift amount as a—usually large—unsigned number and Python raises a ValueError exception on negative shifts.)

MOD is a modulo (i.e., remainder) operator: ‘10 MOD 3’ returns ‘1’. ‘MOD’ is guaranteed to return a nonnegative remainder. Hence, ‘16 MOD 7’ and ‘16 MOD -7’ both return ‘2’ even though ‘-5’ is also mathematically valid. Similarly, ‘-16 MOD 7’ and ‘-16 MOD -7’ both return ‘5’ even though ‘-2’ is also mathematically valid.

The function calls allowed in ‘ <function>( <expr>, …)’ are listed and described in Built-in functions. All functions take one or more arithmetic expressions as an argument. The operator ‘*’ represents multiplication; ‘/’ represents division; and ‘**’ represents exponentiation (i.e., ‘x ** y== x^y, rounded towards zero). Note that 0^y generates a run-time error for y <= 0.

A conditional expression ‘ <expr1> IF <rel_expr> OTHERWISE <expr2>’ evaluates to <expr1> if the relational expression <rel_expr> evaluates to TRUE and <expr2> if <rel_expr> evaluates to FALSE.12 Relational expressions are described in Relational expressions. As some examples of conditional expressions, ‘666 IF 2+2=5 OTHERWISE 777’ returns ‘777’ while ‘666 IF 2+2=4 OTHERWISE 777’ returns ‘666’.

All operations proceed left-to-right except power and conditional expressions, which proceed right-to-left. That is, ‘4-3-2’ means (4-3)-2 but ‘4**3**2’ means 4^(3^2). Similarly, ‘2 IF p=0 OTHERWISE 1 IF p=1 OTHERWISE 0’ associates like ‘2 IF p=0 OTHERWISE (1 IF p=1 OTHERWISE 0)’, not like ‘(2 IF p=0 OTHERWISE 1) IF p=1 OTHERWISE 0’.


Footnotes

(12)

It is therefore analogous to ‘ <rel_expr> ? <expr1> : <expr2>’ in the C programming language.


Next: , Previous: , Up: Expressions   [Contents][Index]

Scott Pakin, pakin@lanl.gov