fabs(x) vs. (x) < 0 ? -(x) : (x)

jtr485 at umich.UUCP jtr485 at umich.UUCP
Fri Feb 20 00:58:35 AEST 1987


In article <1070 at dg_rtp.UUCP>, meissner at dg_rtp.UUCP 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.
> 	Michael Meissner

No.  This will still get the right answer.  The only thing to ever worry about
would be a side effect explicitly manipulating _fabs.  Such as:
    fabs( _fabs++ )
which expands to:
    (((_fabs = (_fabs++)) < 0? -_fabs : _fabs))
And now there is no way of knowing when the ++ gets done relative to the
expressions after the '?'.

But only direct manipulation of the 'hidden' variable can do this.

--j.a.tainter



More information about the Comp.lang.c mailing list