unary +

Steve Villee steve at anasazi.UUCP
Fri Mar 28 07:48:38 AEST 1986


> How do you tell order-of-evaluation-forcing parentheses from parentheses
> that are there just to get the precedence right?  There is no practical
> way to write "a*(c+d)" without parentheses (or other annoying kludges).
> Yet optimizing this to "a*c + a*d" might well be useful in various
> situations.  Parentheses already have a role:  overriding default operator
> binding rules.  Asking them to do two things at once would get messy.
> 				Henry Spencer @ U of Toronto Zoology
> 				{allegra,ihnp4,linus,decvax}!utzoo!henry

I think there is a fundamental confusion here.  If I remember correctly,
the original objection was that some compilers would evaluate (a+(b+c))
as if it were ((a+b)+c).  The issue is not "order of evaluation".  The
programmer has instructed the compiler to evaluate the expressions "a"
and "b+c" in some order, and then add the two to get the result.  Instead,
the compiler has gone and done something different.  This is justified
(as is any optimization) if and only if the compiler can guarrantee that
the result in either case is identical.  Of course, with pure mathematical
objects, the results would be identical.  But with funny beasts like floating
point numbers, the results are in general not identical, and thus the
compiler is wrong if it switches expressions on me.

I emphasize again, parentheses are not being used to specify an order of
evaluation.  The expression (a+b+c) has two interpretations which in general
are quite different, and parentheses are simply being used to specify one
or the other.  Once an unambiguous expression like (a+(b+c)) has been
specified, the order in which the pieces of this expression are evaluated
are (by the language definition) up to the compiler.  If the order of
evaluation makes a difference (which it might if "a" were a function call,
for instance), and if the programmer depends on some particular order
of evaluation, then the programmer is wrong in that case.

In your example above, the expressions "a*(c+d)" and "a*c + a*d" are, in
general, different expressions.  I don't want the compiler making this
switch either, unless it can guarrantee that the results are identical.
Loosely speaking, I would say it is okay for integers but not for floating
point numbers.

--- Steve Villee (ihnp4!terak!anasazi!steve)
    International Anasazi, Inc.
    7500 North Dreamy Draw Drive, Suite 120
    Phoenix, Arizona 85020
    (602) 870-3330



More information about the Comp.lang.c mailing list