Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site lsuc.UUCP Path: utzoo!lsuc!msb From: msb@lsuc.UUCP (Mark Brader) Newsgroups: net.lang.c Subject: Re: unary + Message-ID: <1147@lsuc.UUCP> Date: Tue, 11-Mar-86 01:10:43 EST Article-I.D.: lsuc.1147 Posted: Tue Mar 11 01:10:43 1986 Date-Received: Tue, 11-Mar-86 02:10:30 EST References: <1227@mtx5a.UUCP> <1223@mit-eddie.MIT.EDU> Reply-To: msb@lsuc.UUCP (Mark Brader) Distribution: net Organization: Law Society of Upper Canada, Toronto Lines: 34 Summary: Don't forget macros People who don't think (a+b)+c, a+(b+c), and a+b+c should be interchangeable with respect to optimization -- as they are and will continue to be -- should consider this toy example: #define C_TO_F(temp) ((temp)*1.8 + 32) #define JUST_ABOVE(temp) ((temp) + epsilon) /* extern epsilon */ #define ROUND(real) ((long) .5 + (real)) scanf ("%d", &melt_pt); desired_f_temp = ROUND (JUST_ABOVE (C_TO_F (melt_pt))); There are, of course, NO unnecessary parentheses in the example*. After macros are eliminated, the last line ends up as: desired_f_temp = ((long) .5 + ((((melt_pt)*1.8 + 32)) + epsilon)); The expression uses 3 macros because that most clearly expresses what is going on. (Well, maybe not in this toy case, but there are lots of real-life examples.) And the parentheses are there just because they're needed to make the macros work properly in general*. They SHOULD NOT have anything to do with the order in which the 3 additions actually occur. Mark Brader *For the benefit of any novices to whom this seems strange: Consider the first one, C_TO_F. If we omitted the inner parentheses, then C_TO_F(a+b) would expand to (a + b*1.8 + 32), and only b would be multiplied by 1.8. If we omitted the outer ones, then C_TO_F(c)*k would expand to (c)*1.8 + 32*k, and only 32 would be multiplied by k. Admittedly temperatures are neither commonly added nor multiplied, but it is still very bad practice to omit parentheses from macros where they might be needed for such reasons. End of lecture.