Dyadic operators
Keywords: pame

Subsections

- Exercises

#
Dyadic operators

A dyadic operator takes two operands and is written between them. The simplest operator is dyadic `+`. Here is an identity declaration using it:

       INT one = 1;
       INT two = one + one

This operator takes two operands of mode `INT` and yields a result of mode `INT`. It is also defined for two operands of mode `REAL` yielding a result of mode `REAL`:

    REAL x = 1.4e5 + 3.7e12

The `+` operator performs an action quite different for `REAL` operands from that performed for `INT` operands. Yet the meaning is essentially the same, and so the same symbol is used for the two operators.

Before we continue with the other dyadic operators, a word of caution is in order. As we have seen, the maximum integer which the computer can use is `max int` and the maximum real is `max real`. The dyadic `+` operator could give a result which is greater than those two values. Adding two integers such that the sum exceeds `max int` is said to give “integer overflow”. Algol 68 contains no specific rules about what should happen in such a case.3.1

The dyadic `-` operator can take two operands of mode `INT` or two operands of mode `REAL` and yields an `INT` or `REAL` result respectively:

       INT minus 4 = 3 - 7,
       REAL minus one point five = 1.9 - 3.4

Note that the dyadic `-` is quite different from the monadic `-`. You can have both operators in the same formula:

       INT minus ten = -3 - 7

The first minus sign represents the monadic operator and the second, the dyadic.

Since a formula yields a value of a particular mode, you can use it as an operand for another operator. For example:

       INT six = 1 + 2 + 3

The operators are elaborated in left-to-right order. First the formula `1+2` is elaborated, then the formula `3+3`. What about the formula `1-2-3`? Again, the first `-` operator is elaborated giving `-1`, then the second giving the value `-4`.

Instead of saying “the value of mode `INT`”, we shall sometimes say “the `INT` value” or even “the `INT`”--all these expressions are equivalent.

## Exercises

2.1
  Write an identity declaration for the `INT` value `-35`. Ans
2.2
  What is the value of each of the following formulæ? Ans
  (a)
    `3 - 2`

  (b)
    `3.0 - 2.0`

  (c)
    `3.0 - -2.0`

  (d)
    `2 + 3 - 5`

  (e)
    `-2 + +3 - -4`

2.3
  Given the following declarations

         INT a = 3, REAL b = 4.5

  what is the value of the following formulæ? Ans
  (a)
    `a+a`

  (b)
    `-a-a`

  (c)
    `b+b+b`

  (d)
    `-b - -b + -b`