fabs(x) vs. (x) < 0 ? -(x) : (x)
drw at cullvax.UUCP
drw at cullvax.UUCP
Fri Feb 20 02:22:11 AEST 1987
meissner at dg_rtp.UUCP (Michael Meissner) writes:
> In article <68 at umich.UUCP> jtr485 at umich.UUCP (Johnathan Tainter) writes:
> > #define fabs(X) (((_fabs = (X)) < 0? -_fabs : _fabs))
>
> It may solve that problem, but:
>
> fabs( fabs( y ) - 10.0 )
>
> would still get the wrong answer.
Eh? Let's look at
(_fabs = X) < 0 ? -_fabs : _fabs
1. X has to be computed first.
2. Its value is assigned to _fabs (because the assignment must be
performed before the value of the assignment is used).
3. There is a sequence point after the test-expression of a ? :, so
all side-effects of X must be completed.
4. We get to choose -_fabs or _fabs.
The only problem could arise if X affects _fabs via some side-effect.
But this is not possible, even with nested fabs() calls, because the
only code which changes _fabs is "_fabs = X", which is required to
store into _fabs before having its value used.
Dale
--
Dale Worley Cullinet Software
UUCP: ...!seismo!harvard!mit-eddie!cullvax!drw
ARPA: cullvax!drw at eddie.mit.edu
More information about the Comp.lang.c
mailing list