Short circuit evaluation/expression rearrangement (2nd summary)
Dale Worley
worley at compass.com
Sat Jun 1 00:17:32 AEST 1991
In article <16283 at smoke.brl.mil>, gwyn at smoke.brl.mil (Doug Gwyn) writes:
> >... in "a + b + c",
> >the first addition must be performed before the second, because it
> >associates as "(a + b) + c", showing that the second addition has as
> >an argument the result of the first.
>
> The above is incorrect. A conforming implementation is obliged to
> perform the operations so that it doesn't create an exception where
> one would not have occurred in evaluating the expression strictly
> according to the precedence implied by the grammar, but within
> those constraints it may perform the additions in any order. To
> force an order of evaluation (in Standard C), you must use parentheses.
However, if the computation is floating-point, problems can arise
above and beyond exceptions. The usual problem is loss of
significance. See section 3.3 of the Rationale of the C standard for
an explanation, which makes it clear that (in Standard C) a + b + c
*must* be evaluated as (a + b) + c if a, b, and c are floating point.
(Also, beware that the Rationale claims that Fortran has the same
no-regrouping rule as the C standard. That's not true.)
Dale Worley Compass, Inc. worley at compass.com
--
Dammit, we're all going to die, let's die doing something *useful*!
-- Hal Clement, on comments that space exploration is dangerous
More information about the Comp.lang.c
mailing list