2.3.1 Arithmetic Expressions

Arithmetic expressions express numeric computations, and are formed with arithmetic elements and arithmetic operators. The evaluation of an arithmetic expression yields a single numeric value.

An arithmetic element can be any of the following:

Arithmetic operators specify computations to be performed on the values of arithmetic elements. These operators produce numeric values. Arithmetic operators and their functions are as follows:

Operator  Function 
**  Exponentiation 
Multiplication 
Division 
Addition or unary plus 
Subtraction or unary minus 

The plus and minus operators are called unary operators because they can operate on a single operand. When used as unary operators, the plus or minus operators precede a single operand and denote a positive or negative value, respectively. So, they preserve or change the arithmetic sign of a value. The exponentiation, multiplication, and division operators are binary operators because they operate on a pair of operands.

Variables, array elements, and field references must have defined values before being used in an arithmetic expression.

Valid arithmetic operations must have results that are mathematically defined. For example, dividing by zero or raising a zero-valued base to a zero-valued or negative-valued power is invalid. Raising a negative-valued base to a real power is also invalid.

Arithmetic expressions are evaluated in an order determined by a precedence associated with each operator, as follows:

Operator  Precedence 
**  First 
* and /  Second 
+ and -  Third 

When operators with equal precedence appear, they can be evaluated in any order as long as the order is algebraically equivalent to a left-to-right order of evaluation. Exponentiation, however, is evaluated from right to left. For example, A**B**C is evaluated as A**(B**C); B**C is evaluated first, and then A is raised to the resulting power.

Normally, two operators cannot appear together. However, Compaq Fortran 77 allows two consecutive operators if the second operator is a plus or minus.

Examples

In the following example, the exponentiation operator is evaluated first because it takes precedence over the multiplication operator:

A**B*C is evaluated as (A**B)*C

Ordinarily, the exponentiation operator would be evaluated first in the next example. However, because Compaq Fortran 77 allows the combination of the exponentiation and minus operators, the exponentiation operator is not evaluated until the minus operator is evaluated:

A**-B*C is evaluated as A**(-(B*C))

Note that the multiplication operator is evaluated first, since it takes precedence over the minus operator.

When consecutive operators are used with constants, the unary plus or minus before the constant is treated the same as any other operator. This can produce unexpected results. In the following example, the multiplication operator is evaluated first, since it takes precedence over the minus operator:

X/-15.0*Y is evaluated as X/-(15.0*Y)


Previous Page Next Page Table of Contents