fabs(x) vs. (x) < 0 ? -(x) : (x)
jtr485 at umich.UUCP
jtr485 at umich.UUCP
Thu Feb 5 08:22:08 AEST 1987
In article <628 at sdchema.sdchem.UUCP>, tps at sdchem.UUCP (Tom Stockfisch) writes:
> >You could implement fabs() as a macro as follows:
> > #define fabs(X) ((_fabs = (X)), (_fabs < 0? -_fabs : _fabs))
> >if _fabs were declared as a float in the math library.
> This might not work for
> fabs( x ) + fabs( y )
> because _fabs gets assigned twice in one expression and the two
> comma expressions which result might get interleaved. There was
> a major discussion in this group recently on whether the sub-expressions
> a,b,c,d in
> (a,b) + (c,d)
> could be evaluated in the order
> a c b d
> Since the current defacto standard (K&R) is ambiguous on this point, I
> think your method is not safe.
> || Tom Stockfisch, UCSD Chemistry tps%chem at sdcsvax.UCSD
This fixes that objection:
#define fabs(X) (((_fabs = (X)) < 0? -_fabs : _fabs))
Again it requires the 'hidden' definition of _fabs but it does not have
order of evaluation problems.
--j.a.tainter
More information about the Comp.lang.c
mailing list