Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!cbatt!gatech!mcnc!rti-sel!dg_rtp!meissner From: meissner@dg_rtp.UUCP Newsgroups: comp.lang.c Subject: Re: fabs(x) vs. (x) < 0 ? -(x) : (x) Message-ID: <1070@dg_rtp.UUCP> Date: Mon, 16-Feb-87 19:02:01 EST Article-I.D.: dg_rtp.1070 Posted: Mon Feb 16 19:02:01 1987 Date-Received: Wed, 18-Feb-87 20:45:29 EST References: <4943@mimsy.UUCP> <2550005@hpisod2.HP> <628@sdchema.sdchem.UUCP> <68@umich.UUCP> Reply-To: meissner@dg_rtp.UUCP (Michael Meissner) Distribution: world Organization: Data General (Languages @ Research Triangle Park, NC.) Lines: 32 In article <68@umich.UUCP> jtr485@umich.UUCP (Johnathan Tainter) writes: > In article <628@sdchema.sdchem.UUCP>, tps@sdchem.UUCP (Tom Stockfisch) writes: > > >You could implement fabs() as a macro as follows: > > > #define fabs(X) ((_fabs = (X)), (_fabs < 0? -_fabs : _fabs)) > > > /* ... */ > > This might not work for > > fabs( x ) + fabs( y ) > > /* ... */ > > #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 It may solve that problem, but: fabs( fabs( y ) - 10.0 ) would still get the wrong answer. In my opinion, the "best" way to do this is to make fabs (and abs, min, max, etc.) builtin to the compiler. In order to allow people to still write their own "fabs" function, you might want to have some special keyword (I use $builtin which is not a legimate standard C identifier) to key in the compiler to use the builtin version of the library function (which would compile down to the "right" instructions for your machine, and no call overhead). I also applaud C++'s inline keyword, which should obviate the need for macro functions, which have the traditional problems described above. -- Michael Meissner, Data General ...mcnc!rti-sel!dg_rtp!meissner