C Compiler bug
Steve Summit
stevesu at copper.UUCP
Thu Jul 24 16:56:39 AEST 1986
In article <501 at bunny.UUCP>, mlr0 at bunny.UUCP (Martin Resnick) writes:
> Please try this code on your flavor of C compiler...
(followed by code demonstrating essentially
> i = i/2.5;
vs.
> i /= 2.5;
The answer is absolutely unequivocal on this point. A compiler
that does not treat i=i/float and i/=float identically is broken,
although many compilers are so broken. This question was
discussed to death on the net a year or so ago. The discussion
came to a screeching halt, quite unlike most net discussions,
when the following article appeared from one dmr at research:
> From: dmr at research.UUCP
> Newsgroups: net.lang.c
> Subject: bug in type conversion
> Date: Wed, 4-Jan-84 21:32:17 PST
>
> Mike O'Brien points out that in the C compilers he has available, the
> expression
> i *= d;
> where i is int and d is double is evaluated in fixed point, and
> wonders why. The answer: it is a compiler bug. I fixed it in
> (a post V7 version) of the 11 compiler, and it is fixed in the current
> System V compiler (by "current" I mean the one I tried;
> I don't know what is being shipped at this instant.)
>
> The manual is reasonably clear and unambiguous on the point, but
> it's not surprising that people for search for definition problems when
> the compilers are unanimously wrong.
>
> Dennis Ritchie
What the manual says is (K&R page 191):
"The behavior of an expression of the form E1 op= E2 may
be inferred by taking it as equivalent to E1 = E1 op (E2);
however, E1 is evaluated only once."
The single evaluation of E1 is the only difference between the
two forms; any type casting, including truncation, should be done
exactly as if E1 = E1 op (E2) had been performed.
Steve Summit
tektronix!copper!stevesu
More information about the Comp.lang.c
mailing list