Concepts of Programming Languages

(Sean Pound) #1
7.2 Arithmetic Expressions 323

A + B + C


does not depend on the order of operator evaluation. If floating-point opera-
tions for mathematically associative operations were also associative, the com-
piler could use this fact to perform some simple optimizations. Specifically, if
the compiler is allowed to reorder the evaluation of operators, it may be able
to produce slightly faster code for expression evaluation. Compilers commonly
do these kinds of optimizations.
Unfortunately, in a computer, both floating-point representations and
floating-point arithmetic operations are only approximations of their mathe-
matical counterparts (because of size limitations). The fact that a mathemati-
cal operator is associative does not necessarily imply that the corresponding
floating-point operation is associative. In fact, only if all the operands and
intermediate results can be exactly represented in floating-point notation will
the process be precisely associative. For example, there are pathological situa-
tions in which integer addition on a computer is not associative. For example,
suppose that a program must evaluate the expression


A + B + C + D


and that A and C are very large positive numbers, and B and D are negative num-
bers with very large absolute values. In this situation, adding B to A does not
cause an overflow exception, but adding C to A does. Likewise, adding C to B
does not cause overflow, but adding D to B does. Because of the limitations of
computer arithmetic, addition is catastrophically nonassociative in this case.
Therefore, if the compiler reorders these addition operations, it affects the
value of the expression. This problem, of course, can be avoided by the pro-
grammer, assuming the approximate values of the variables are known. The
programmer can specify the expression in two parts (in two assignment state-
ments), ensuring that overflow is avoided. However, this situation can arise in
far more subtle ways, in which the programmer is less likely to notice the order
dependence.


7.2.1.3 Parentheses


Programmers can alter the precedence and associativity rules by placing paren-
theses in expressions. A parenthesized part of an expression has precedence over
its adjacent unparenthesized parts. For example, although multiplication has
precedence over addition, in the expression


(A + B) * C


the addition will be evaluated first. Mathematically, this is perfectly natural. In
this expression, the first operand of the multiplication operator is not available
until the addition in the parenthesized subexpression is evaluated. Also, the
expression from Section 7.2.1.2 could be specified as

Free download pdf