The operand * (often said "star")
represents normal arithmetic multiplication and
takes INT
operands yielding an INT
result.
For example:
INT product = 45 * 36
Likewise, *
is also defined for multiplication of two
values of mode REAL
:
REAL real product = 2.4e-4 * 0.5
It is important to note that although the actions of the two operators are quite different, they both represent multiplication so they both use the same symbol.
Like +
and -
, multiplication can occur
several times:
INT factorial six = 1 * 2 * 3 * 4 * 5 * 6
the order of elaboration being left-to-right.
You can also combine multiplication with addition and subtraction.
For example, the value of the formula 2+3*4
is
14
. At school, you were probably taught that
multiplication should be done before addition (your teachers may have
used the mnemonic BODMAS to show the order in which
operations are done. It stands for Brackets, Over, Division,
Multiplication, Addition and Subtraction). In Algol 68, the same
sort of thing applies and it is done by operators having a
priority. The priority
of multiplication is higher than the priority for addition or
subtraction. The priority of the dyadic +
and
-
operators is 6, and the priority of the
*
operator is 7.
Here are identity declarations using a combination of multiplication and addition and subtraction:
INT i1 = 3, i2 = -7; INT result1 = i1 * i2 - 8; REAL r1 = 35.2, r2 = -0.04; REAL result2 = r1 * -r2 + 12.67 * 10.0
In the elaboration of result2
, the multiplications are
elaborated first, and then the addition.
Remember from chapter 1 that widening is allowed in the context of the right-hand side of an identity declaration, so the following declaration is valid:
REAL a = 24 * -36
It is important to note that an operand is not in a strong context, so no widening is allowed. The context of an operand is firm. Because widening is not allowed in a firm context, it is possible for the compiler to examine the modes of the operands of an operator and determine which declaration of the operator is to be used in the elaboration of the formula. This also applies to monadic operators (see 6.2.1 for details).
Looking again at the above identity declaration, the context of
the denotation 36
is firm (it is the operand of the
monadic -
), the contexts of the 24
and the
-36
are also firm because they are the operands of the
dyadic *
, but the value yielded by the
formula is on the right-hand side of the identity declaration, so it
is in a strong context. It is this value which is
coerced to a value of mode REAL
by the widening. Note
that the value of the formula (which has mode INT
) does
not change. Instead, it is replaced by the coercion
with a value of mode REAL
whose whole number part has the
same value as the INT
value. It is worth saying that the
value of the formula obtained by elaboration is lost after the
coercion. You could hang on to the intermediate integer value by using
another identity declaration:
INT intermediate value = 24 * -36; REAL a = intermediate value
INT d1 = 12, d2 = -5; REAL d3 = 4.0 * 3.5, d4 = -3.0What is the value of each of the following formulæ? Ans
ABS d2
- ABS d4 + d3 * d4
d2 - d1 * 3 + d2 * 4
Sian Mountbatten 2012-01-19