Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!rutgers!sri-unix!hplabs!hpcea!hpda!hpirs!hpisod2!decot From: decot@hpisod2.UUCP Newsgroups: comp.lang.c Subject: fabs(x) vs. (x) < 0 ? -(x) : (x) Message-ID: <2550005@hpisod2.HP> Date: Wed, 28-Jan-87 16:33:01 EST Article-I.D.: hpisod2.2550005 Posted: Wed Jan 28 16:33:01 1987 Date-Received: Sat, 31-Jan-87 05:53:08 EST References: <4943@mimsy.UUCP> Lines: 21 > In article <4477@ut-ngp.UUCP> jjr@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@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 to use fabs(), but you probably already do anyway. Dave