next up previous contents index
Next: Data Type Conversions Up: Arithmetic Expressions Previous: Arithmetic Expressions

General Rules

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:

The rules for forming more complicated arithmetic expressions are as follows. An arithmetic expression can have any of the following forms:
operand
+operand
-operand
arithmetic-expression   arith-op   operand
where the arith-op can be any of these operators:
+ 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:

1.
sub-expressions in parentheses
2.
function references
3.
exponentiation, i.e. raising to a power
4.
multiplication and division
5.
addition, subtraction, or negation.

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.


next up previous contents index
Next: Data Type Conversions Up: Arithmetic Expressions Previous: Arithmetic Expressions
Helen Rowlands
8/27/1998