Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!cbatt!ucbvax!sdcsvax!sdchem!tps From: tps@sdchem.UUCP Newsgroups: comp.lang.c Subject: Re: fabs(x) vs. (x) < 0 ? -(x) : (x) Message-ID: <628@sdchema.sdchem.UUCP> Date: Sun, 1-Feb-87 19:33:15 EST Article-I.D.: sdchema.628 Posted: Sun Feb 1 19:33:15 1987 Date-Received: Tue, 3-Feb-87 04:05:11 EST References: <4943@mimsy.UUCP> <2550005@hpisod2.HP> Sender: news@sdchem.UUCP Reply-To: tps@sdchemf.UUCP (Tom Stockfisch) Organization: UC San Diego Lines: 35 In article <2550005@hpisod2.HP> decot@hpisod2.HP (Dave Decot) writes: >In article <4477@ut-ngp.UUCP> jjr@ngp.UUCP (Jeff Rodriguez) writes: >>Why isn't fabs() implemented as a macro [ (X) < 0 ? -(X) : (X) ]? > >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. >Dave 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. The real solution for this is to have an optimizer which can place function calls inline. (There are complilers which can do this). This even runs faster than your method because it eliminates the store to _fabs. || Tom Stockfisch, UCSD Chemistry tps%chem@sdcsvax.UCSD