() ignored in some expressions

Mark Brader msb at sq.sq.com
Thu Apr 12 06:22:41 AEST 1990


As has been explained, in ANSI C (x+y)+z is required to evaluate x+y
first if you can tell the difference, but before ANSI C, it could begin
by evaluating x+z or y+z, and similarly for other commutative and
associative operators.

In my opinion this was a good feature and I was sorry to see X3J11
change it.  It is quite common for a macro to expand to something like
like ((x)+1)+((y)+1), and we would usually like to treat this as x+y+2
even though this may cause an overflow for certain values of x and y,
where the original expression would not overflow.  In the rare cases
where the sequence of operations is critical, the expression can always
be broken up.

However, the floating-point users were also right to want precise control
over the sequence of operations, when working with floating types.
With these types it is more common for the sequence to be critical.

So what I would have liked to see go into the standard was the old rule
about commutative and associative operators, plus one more sentence:
"Arithmetic operators with operands of floating type are not associative."
This is, of course, the truth.  (I did propose this compromise to X3J11,
but too late.)

-- 
Mark Brader		     "It is impractical for the standard to attempt to
SoftQuad Inc., Toronto	      constrain the behavior of code that does not obey
utzoo!sq!msb, msb at sq.com      the constraints of the standard."  -- Doug Gwyn

This article is in the public domain.



More information about the Comp.lang.c mailing list