Arithmetic Operators

<arithmetic operator>

──┬─ + ─────┬──────────────────────────────────────────────────────────┤
  ├─ - ─────┤
  ├─ * ─────┤
  ├─ TIMES ─┤
  ├─ MUX ───┤
  ├─ / ─────┤
  ├─ DIV ───┤
  ├─ MOD ───┤
  └─ ** ────┘

The operators +, –, *, and / have the conventional mathematical meaning of addition, subtraction, multiplication, and division, respectively. No two operators can be adjacent, and implied multiplication is not allowed.

The TIMES operator also denotes multiplication.

The DIV operator denotes integer division. It has the following mathematical meaning:

Y DIV Z = SIGN(Y/Z) * ENTIER(ABS(Y/Z)) 

The MOD operator denotes remainder or modulo division. If Z is greater than or equal to 1, MOD has the following meaning:

Y MOD Z = Y - (Z * (Y DIV Z))

If Z is less than 1, the MOD operator produces undefined results.

The MUX operator multiplies either single-precision or double-precision arithmetic primaries, and yields a double-precision result.

The ** operator denotes exponentiation. The semantics of the exponentiation operator depend on the types and values of the primaries involved. Exponentiation: Meaning of Y**Z explains the various meanings of Y**Z.

Figure 10. Exponentiation: Meaning of Y**Z

Exponentiation: Meaning of Y**Z


Precedence of Arithmetic Operators

The sequence in which the operations of an arithmetic expression are performed is determined by the precedence of the operators involved. The order of precedence is as follows:

  1. ** (highest precedence)

  2. *, /, MOD, DIV, MUX, TIMES

  3. +, -

Operators with the same precedence are applied in their order of appearance in an expression, from left to right.

The precedence of the assignment operator (:=) is as follows:

  1. An expression to the right of an assignment operator is evaluated before the assignment.

  2. The assignment is done before the evaluation of an expression involving the variable that is the target of the assignment.

Parentheses can be used in normal mathematical fashion to override the defined order of precedence. An expression in parentheses is evaluated by itself, and the resulting value is subsequently combined with the other elements of the expression. In the following expression, for example, the addition is performed before the division because of the parentheses:

(X+1)/Y

In the following expression, 1 is first divided by Y and then the result is added to X:

X+1/Y

For information on the order of evaluation of subscripts, see “Arithmetic Update Assignment” in Statements.

Mathematical Notation illustrates how mathematical notation can be translated to an ALGOL arithmetic expression.

Table 8.  Mathematical Notation

Mathematical Expression

Equivalent ALGOL Expression

A x B
A * B
      B 
A + ----  
      2
A + B / 2
   X + 1 
----------  
     Y
(X + 1) / Y
   D + E2 
------------ 
    2A
(D + E**2) / (2 * A)
4 (X + Y)3
4 * (X + Y) ** 3
 M - N 
-------------------------------  
                          – 6   
                 P + 5 x 10
       (M + N)
(M - N) / (M + N) ** (P + 5@-6)


Types of Resulting Values

The type of the value resulting from an arithmetic operation depends on the arithmetic operator and the types of the primaries being combined, except when the resulting value is undefined. Types of Values Resulting from Arithmetic Operations describes the types of quantities that result from various combinations of arithmetic primaries.

Table 9. Types of Values Resulting from Arithmetic Operations

Operand

on

Left

Operand

on

Right

+-*

/

DIV

MOD

**

MUX

INTEGER

INTEGER

INTEGER

INTEGER

REAL

REAL

REAL

REAL

DINTEGER

DINTEGER

DINTEGER

DOUBLE

INTEGER

REAL

DINTEGER

DOUBLE

INTEGER

REAL

DINTEGER

DOUBLE

INTEGER

DINTEGER

(any other)

(any)

Note 3

REAL

DOUBLE

DOUBLE

REAL

REAL

DOUBLE

DOUBLE

DOUBLE

DOUBLE

DOUBLE

DOUBLE

REAL

REAL

DOUBLE

DOUBLE

REAL

REAL

DOUBLE

DOUBLE

DOUBLE

DOUBLE

DOUBLE

DOUBLE

INTEGER

INTEGER

DINTEGER

DINTEGER

INTEGER

INTEGER

DINTEGER

DINTEGER

DINTEGER

DINTEGER

DINTEGER

DINTEGER

INTEGER

REAL

DINTEGER

DOUBLE

REAL

REAL

DOUBLE

DOUBLE

INTEGER

DINTEGER

DOUBLE

DOUBLE

Note 1

Note 2

Note 2

Note 2

Note 2

Note 2

DOUBLE

DOUBLE

DOUBLE

DOUBLE

DOUBLE

DOUBLE

DOUBLE

DOUBLE

DOUBLE

DOUBLE

DOUBLE

DOUBLE

DOUBLE

DOUBLE

DOUBLE

DOUBLE

DOUBLE

DOUBLE

Note 1: If the operand on the right is negative or the absolute value of the result is greater than or equal to 2**39, REAL; otherwise, INTEGER

Note 2: If the operand on the right is zero, INTEGER; otherwise, REAL

Note 3: If the absolute value of the result is less than 2**39, INTEGER; otherwise, REAL


The type of an arithmetic case expression or a conditional arithmetic expression is DOUBLE if any of its constituent expressions are of type DINTEGER or DOUBLE. For more information, refer to “Precision of Arithmetic Expressions” earlier in this section. If the conditional arithmetic expression or arithmetic case expression contains only expressions of type INTEGER and REAL, its type is REAL. A conditional arithmetic expression or arithmetic case expression is of type INTEGER only if all its constituent expressions are of type INTEGER.