# Arithmetic Expressions and Assignment

## Arithmetic expressions

An arithmetic expression is one which is evaluated by performing a sequence of arithmetic operations to obtain a numeric value, which replaces the expression. Arithmetic operations are denoted by the following arithmetic operators

```Operator  Operation

-         Subtraction or
unary -

*         Multiplication

/         Division

**        Exponentiation

```

Figure 4 : Arithmetic operators

An operand of an arithmetic expression may be:

• a numeric constant
• a numeric variable, which may be preceded by a unary + or -.
• an arithmetic expression in parentheses, i.e. (arithmetic_expression)

An arithmetic expression has the form:

operand [operator operand] ...

The square brackets indicate that the items operator operand are optional and the ellipsis (...) that they may be repeated indefinitely.

Spaces may be used to improve readability, but are ignored by the FORTRAN compiler.

Examples:

```	3.14159
K
(A+B)*(C+D)
-1.0/X + Y/Z**2
```
###### Restrictions:
1. Two operators cannot be written consecutively.

Example: A*-B is illegal. The second factor must be made into an expression by using parentheses, viz: A*(-B).

1. The operands must be such that the operations are mathematically defined, e.g. dividing by zero, raising zero to a negative or zero power, or raising a negative number to a real power are all illegal.

### Evaluation

An arithmetic expression is evaluated by replacing variable operands and parenthesised expressions by their values, and performing the operations indicated in sequence, using the result of each operation as an operand of the next. Clearly, the sequence in which the operations are performed is significant, as shown by the example:

4.0+6.0*2.0

which evaluates to 20.0 if the addition is performed first, or 16.0 if the multiplication is performed first.

The sequence of operations is determined by the following precedence order, in which operators on any line have equal precedence and precedence decreases downwards.

**

* /

+ - (binary and unary)

Using this precedence order, the rules for the evaluation of an arithmetic expression may be stated as follows:

1. Evaluation begins with the leftmost operand.
2. A variable operand, or a parenthesised expression, is evaluated before the following rules are applied.
3. Exponentiation is performed from right to left.

Example: 2**3**2 evaluates to 512 (29).

1. All other operations are performed from left to right unless the operator precedence rules stipulate the opposite.

Examples:

4.0+6.0*2.0 evaluates to 16.0

(4.0+6.0)*2.0 evaluates to 20.0

### Type rules for arithmetic expressions

Subject to the restrictions noted under 'Restrictions:' above, REAL and INTEGER operands may be freely mixed in an arithmetic expression. The type of the expression's value is determined by applying the following rules to each operation performed in its evaluation:

1. If both operands are of the same type, the resulting value is also of that type.
2. If one operand is INTEGER and the other REAL, the INTEGER operand is converted to its REAL equivalent before the operation is performed, and the resulting value is REAL.

Rule (i) is inconsistent with the rules of arithmetic, in which dividing one integer by another (e.g. 7/5) or raising an integer to an integer power (e.g. 2-1) does not always result in an integer. FORTRAN deals with such cases by truncating, as described under 'Type rules' on page 5, to obtain an INTEGER value.

Examples:

```          Value

99/100    0

7/3       2

-7/3      -2

N**(-1)   0

N**(1/2)  1

100*9/5   180

9/5*100   100

```

The last two examples show that the ordering of * and / operators with INTEGER operands is significant. It is usually best to avoid dividing one integer by another unless there are special reasons for doing so.

## Arithmetic assignment

As noted at the beginning of the chapter, the value of an arithmetic expression is assigned to a numeric variable in a statement of the form:

numeric_variable = arithmetic_expression

If the type of the expression differs from that of the variable, the rules listed under 'Type rules' on page 5 are applied, i.e.

```Expression     Variable      Rule
type           type

INTEGER        REAL          Convert to REAL

REAL           INTEGER       Truncate

```

[Contents]  [Previous]  [Next]  [Home]

NDP77
http://www.ndp77.net
webmaster Massimo F. ARENA
webmaster@ndp77.net
2004:02:14:17:30:17