Subsections

- Exercises

# Relational operators

Values of modes `INT`, `REAL`, `CHAR` and `[]CHAR` can be compared with each other. The expression

       3 = 1+2

yields `TRUE`. Similarly,

       1+1=1

yields `FALSE`. The equals symbol `=` can also be written `EQ`. Likewise, the formula

       35.0 EQ 3.5e1

should also yield `TRUE`, but you should be chary of comparing two `REAL`s for equality or inequality because the means of transforming the denotations into binary values may yield values which differ slightly. The operator is also defined for both operands being `CHAR` or `[]CHAR`. In the latter case, the two multiples must have the same number of elements, and corresponding elements must be equal if the operator is to yield `TRUE`. Thus

       "a" = "abc"

yields `FALSE`. Notice that the bounds do not have to be the same. So `a` and `b` declared as

       []CHAR a = "Dodo" [@0],
              b = "Dodo"

yield `TRUE` when compared with the equals operator. Because the rowing coercion is not allowed in formulæ, the operator is declared in the standard prelude for mixed modes (such as `REAL` and `INT`).

The converse of `=` is `/=` (not equal). So the formula

       3 /= 2

yields `TRUE`, and

       "r" /= "r"

yields `FALSE`. An alternative representation of `/=` is `NE`. The priority of both `=` and `/=` is 4. The operands of `=` and `/=` can be any combination of values of mode `INT` and `REAL`. No widening takes place, the operators being declared for the mixed modes.

The ordering operators `<`, `>`, `<=` and `>=` can be used to compare values of modes `INT`, `REAL`, `CHAR` and `[]CHAR` in the same way as `=` and `/=`. They are read “less than”, “greater than”, “less than or equal to” and “greater than or equal to” respectively. The formula

       3 < 3.1

yields `TRUE`.

If the identifiers `b` and `c` are declared as having mode `CHAR`, then the formula

       c < b

will yield the same value as

       ABS c < ABS b

and similarly for the operator `>`. The operators `<=` and `>=` can both be used with equal values. For example,

       24 <= 24.0

yields `TRUE`.

For values of mode `[]CHAR`, the formula

       "abcd" > "abcc"

yields `TRUE`. Two values of mode `[]CHAR` of different length can be compared. For example, both

       "aaa" <= "aaab"

and

       "aaa" <= "aaaa"

yield `TRUE`. Alternative representations for these operators are `LT` and `GT` for `<` and `>` and `LE` and `GE` for `<=` and `>=` respectively. The priority of all four ordering operators is 5.

Note that apart from values of mode `[]CHAR`, no operators are defined in the standard prelude for multiples.

## Exercises

4.1
  What is the value of each of the following formulæ? Ans
  (a)
    `ABS NOT TRUE`

  (b)
    `3.4 + ABS TRUE`

  (c)
    `-3.5 <= -13.4`

  (d)
    `2e10 >= 3e9`

  (e)
    `"abcd" > "abc"`

4.2
  In the context of these declarations

         []INT i1 = (2,3,5,7);
         []CHAR t = "uvwxyz"

  what is the value of each of the following? Ans
  (a)
    `UPB i1 < UPB t`

  (b)
    `t[2:4] >= t[2:3]`

  (c)
    `i1[3] < UPB t[2:]`