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

mouse at mcgill-vision.UUCP mouse at mcgill-vision.UUCP
Tue Feb 10 18:33:41 AEST 1987


In article <756 at unc.unc.UUCP>, steele at unc.UUCP (Oliver Steele) writes:
> In article <2550005 at hpisod2.HP> decot at hpisod2.HP (Dave Decot) writes:
>>    #define fabs(X)     ((_fabs = (X)), (_fabs < 0? -_fabs : _fabs))
> Beware
> 	fabs(a,fabs(b,c)),
> as well as
> 	fabs(a,func(b,c))
> where func is a macro which invokes fabs.

fabs(a,fabs(b,c)) ->
((_fabs=(a,((_fabs=(b,c)),(_fabs<0?-_fabs:_fabs)))),(_fabs<0?-_fabs:_fabs))

which looks reliable to me (outer assignment doesn't happen until the
inner expression is done).  Problem arises with fabs(a)<fabs(b) and
similar expressions.

However, if fabs is a macro as above, fabs(a,b) will do something very
different from what it would if fabs were a function.  If fabs is a
function, "a,b" is treated as two arguments; the macro treats a,b as a
comma expression instead of two arguments!  Most compilers will
probably ignore extra arguments to functions, meaning that fabs(a,b)
returns fabs(a) if it's a function and fabs(b) if it's a macro...hmmm,
maybe this could be useful (ok, ok, quit shuddering, I *know* it's
nonportable!).

					der Mouse

USA: {ihnp4,decvax,akgua,utzoo,etc}!utcsri!mcgill-vision!mouse
     think!mosart!mcgill-vision!mouse
Europe: mcvax!decvax!utcsri!mcgill-vision!mouse
ARPAnet: think!mosart!mcgill-vision!mouse at harvard.harvard.edu



More information about the Comp.lang.c mailing list