Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!rutgers!husc6!necntc!cullvax!drw From: drw@cullvax.UUCP Newsgroups: comp.lang.c Subject: fabs(x) vs. (x) < 0 ? -(x) : (x) Message-ID: <815@cullvax.UUCP> Date: Thu, 19-Feb-87 11:22:11 EST Article-I.D.: cullvax.815 Posted: Thu Feb 19 11:22:11 1987 Date-Received: Fri, 20-Feb-87 21:39:13 EST Organization: Cullinet Software, Inc., Westwood, MA Lines: 34 meissner@dg_rtp.UUCP (Michael Meissner) writes: > In article <68@umich.UUCP> jtr485@umich.UUCP (Johnathan Tainter) writes: > > #define fabs(X) (((_fabs = (X)) < 0? -_fabs : _fabs)) > > It may solve that problem, but: > > fabs( fabs( y ) - 10.0 ) > > would still get the wrong answer. Eh? Let's look at (_fabs = X) < 0 ? -_fabs : _fabs 1. X has to be computed first. 2. Its value is assigned to _fabs (because the assignment must be performed before the value of the assignment is used). 3. There is a sequence point after the test-expression of a ? :, so all side-effects of X must be completed. 4. We get to choose -_fabs or _fabs. The only problem could arise if X affects _fabs via some side-effect. But this is not possible, even with nested fabs() calls, because the only code which changes _fabs is "_fabs = X", which is required to store into _fabs before having its value used. Dale -- Dale Worley Cullinet Software UUCP: ...!seismo!harvard!mit-eddie!cullvax!drw ARPA: cullvax!drw@eddie.mit.edu