fabs(x) vs. (x) < 0 ? -(x) : (x)
decot at hpisod2.UUCP
decot at hpisod2.UUCP
Thu Jan 29 07:33:01 AEST 1987
> In article <4477 at ut-ngp.UUCP> jjr at ngp.UUCP (Jeff Rodriguez) writes:
> >Why isn't fabs() implemented as a macro [ (X) < 0 ? -(X) : (X) ]?
>
> I think it's primarily because of things like "y = fabs(sin(x))", which would
> be inefficent, and "y = fabs(*px++)", which would be wrong.
>
> Generally, the standard library functions are not implemented as macros unless
> they evaluate each argument exactly once. (There are exceptions, though.)
>
> Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint
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.
Of course, then you would have to include <math.h> to use fabs(), but you
probably already do anyway.
Dave
More information about the Comp.lang.c
mailing list