# Appendix A: Arithmetic expressions

This appendix gives a rather fuller and more formal description of arithmetic expressions for readers who are interested in using SIMULA's very powerful facilities for mathematical programming.

The order of evaluation of expressions was said to be from left to right in chapter 2. This is obviously not the whole story and SIMULA actually follows normal rules of operator precedence and bracketing.

### Parantheses

Any sub-expression within an expression may be enclosed in parantheses. This may be done recursively to any desired depth, allowing sub-expressions to be nested.

When an expression is evaluated any parenthesised sub-expressions within it are evaluated fully first. This rule is applied recursively, so that nested sub-expressions are always evaluated before their enclosing expression. Where an expression contains more than one parenthesised sub-expression, these are evaluated left to right.

Thus the following Boolean expressions are true.

```   2 * 2 + 3 + 4 * 5 = 27

2 *(2 + 3)+ 4 * 5 = 30

(2 *(2 + 3)+ 4)* 5 = 70
```

### Precedence

Terms separated by operators form triples and arithmetic expressions are made up of such triples. Each triple yields a value. Parentheses and operator precedence determine the order of evaluation of triples and, by implication, the combinations which yield triples. The order of operator precedence is as follows.

1. exponentiation **
2. multiplication and division *, /, //
3. addition and subtraction +, -

Within these categories, triples are evaluated from left to right.

Thus the following are true.

```      4 + 2 // 2 = (2 // 2) + 4

7 * 8 + 3 * 11 ** 4 = ((11 ** 4) * 3) + (7 * 8)
```
Note how parantheses can make expressions easier to read, even if they do not change the meaning.

### Implicit conversion

Implicit conversion is performed, where necessary, each time a term-operator-term triple is evaluated. This may mean several conversions of the intermediate values in an expression, with possible rounding errors each time, during the evaluation of a complete expression.

The following rules govern where conversion is performed:

1. If the operator is integer divide, //, both terms must be integer or short integer. Use of reals or long reals constitutes a runtime error.

2. All short integer values are converted to integer. This can never cause a conversion error.

3. If the operator is real divide, /, both terms are converted to real or long real, in accordance with (4) below.

4. Where the terms are of different types and at least one is real or long real conversion is performed. If one term is long real the other is converted to long real otherwise the non-real term is converted to real.

Thye type of the final value will depend on the type of the terms after application of these rules. Type conversion is always performed before the operation is performed.

### Implicit conversion and conditional expressions

Where the alternatives of a conditional arithmetic expression yield values of different types, rules (2) and (4) above are applied to determine the type of the overall expression.