Arithmetic expressions can contain arithmetic operands, arithmetic operators, and parentheses. There must always be at least one operand. The operands can belong to any of the four arithmetic data types (integer, real, double precision, or complex); the result also has an arithmetic data type. Operands can be any of the following:
operand |
+operand |
-operand |
arithmetic-expression arith-op operand |
+ |
addition |
- |
subtraction |
* |
multiplication |
/ |
division |
** |
exponentiation |
The effect of these rules is that an expression consists of a string of operands separated by operators and, optionally, a plus or minus at the start. A leading plus sign has no effect; a leading minus sign negates the value of the expression.
All literal arithmetical constants used in expressions must be
unsigned: this is to prevent the use of two consecutive operators
which is confusing and possibly ambiguous:
4 / -3.0**-1
(illegal).
The way around this is to use parentheses, for example:
4 / (-3.0)**(-1)
which makes the order of evaluation explicit.
The order of evaluation of an expression is:
Within each of these groups evaluation proceeds from left to right, except that exponentiations are evaluated from right to left. Thus: A / B / C is equivalent to (A / B) / C whereas X ** Y ** Z is equivalent to X ** (Y ** Z).
An expression does not have to be evaluated fully if its value can
be determined otherwise: for example the result of:
X * FUNC(G)
can be determined without calling the function FUNC if X happens
to be zero. This will not cause problems if you only use functions
that have no side effects.