Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!ames!ucbcad!ucbvax!hplabs!hp-pcd!uoregon!omepd!jimv From: jimv@omepd (Jim Valerio) Newsgroups: comp.lang.c Subject: Re: Optimizing Floating-Point Expression Evaluation Message-ID: <589@omepd> Date: Mon, 20-Apr-87 23:02:09 EST Article-I.D.: omepd.589 Posted: Mon Apr 20 23:02:09 1987 Date-Received: Wed, 22-Apr-87 05:35:10 EST References: <16646@sun.uucp> <4616@utcsri.UUCP> Reply-To: jimv@omepd.UUCP (Jim Valerio) Organization: Intel Corp., Hillsboro Lines: 39 Summary: floating-point arithmetic is not associative In article <4616@utcsri.UUCP> flaps@utcsri.UUCP (Alan J Rosenthal) writes: >please explain how you can know whether or not I care about >evaluation order in the following: > #define THING (a + 3.5) > ... > c = (THING + 1.1) * 2.6; >How do I say that I want the 3.5 and 1.1 to be added at compile time? The answer is simple: the compiler can't read your mind. In this particular example, assume for example that all the variables are IEEE single precision numbers, all intermediate arithmetic is done in single precision, and `a' has the value -4.6, correctly rounded. In this case, (a + (3.5 + 1.1)) is zero and ((a + 3.5) + 1.1) is nonzero and long ways away from underflowing. Which answer is "right"? Can you guarantee that `a' is never -4.6 here, so the only difference between the two expressions is a rounding error? When computing the expression, in which direction do you want rounding errors to be incurred? How is the result going to be used? Will errors cancel out here, or will they accumulate? Welcome to the world of error analysis. But I think you are asking the wrong question. I would ask: why partake in the reprehensible practice of hiding the floating-point arithmetic behind a macro name if you don't want it to be taken a an indivisible entity? Why declare `THING' unless you want to treat it in the program as a single variable? If `THING' is imitating a single variable, then the parentheses should be honored. If `THING' is imitating a cheap function call, then following the parentheses exactly mimics the desired semantics. My point is that this is an example of intent to evaluate the expression in the parenthesized order, or plain poor code. -- Jim Valerio {verdix,intelca!mipos3}!omepd!jimv, jimv@omepd.intel.com