Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!ima!mirror!cca!g-rh From: g-rh@cca.UUCP Newsgroups: comp.lang.c Subject: Re: C and Floating Point Message-ID: <14680@cca.CCA.COM> Date: Sat, 4-Apr-87 15:27:51 EST Article-I.D.: cca.14680 Posted: Sat Apr 4 15:27:51 1987 Date-Received: Mon, 6-Apr-87 23:40:40 EST References: <15958@sun.uucp> <5716@brl-smoke.ARPA> Reply-To: g-rh@CCA.CCA.COM.UUCP (Richard Harter) Organization: Computer Corp. of America, Cambridge, MA Lines: 60 Keywords: C Fortran Floating Point In article <5716@brl-smoke.ARPA> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) writes: .... > >It is totally bogus to argue that mathematicians' idea of the meaning >of parentheses in expressions has anything to do with time order of >evaluation. That is a Fortran notion. C actually conforms more >closely to what a mathematician means by parentheses, which is to >group subexpressions in order to override default rules of operator >precedence. Because C also has a heavily-used macro expansion >facility, we have to take that into account when deciding what the >"right" thing to do with parentheses is. I assure you that X3J11 has >heard the arguments that the Fortran-style proponents give, and we >decided differently for SOUND REASONS. We ALSO found that their >legitimate concern could be met by use of the unary + operator, >which was originally introduced for unrelated reasons.... Doug, much as we all respect your knowledge of C and Unix, this just doesn't wash. The essence of the problem is that floating point arithmetic is commutative but not associative. From a mathematicians viewpoint 'x + y + z', where x, y, and z are floating point numbers and '+' is floating point addition, is not well defined. I repeat, NOT WELL DEFINED. The use of 'x op y op z' where 'op' is a two place operator is ambiguous: it may either express op(op(x,y),z) or op(x,op(y,z)) Associativity, of course, means that the two are equivalent (i.e. they both yield the same result.) Furthermore we can prove (and it has to be proved) that the expression 'x1 op x2 op x3 ... op xn' is unambiguous because all possible groupings are equivalent if op is associative. If we are given an expression 'x op y op z' and 'op' is not associative then either we must use a predefined order of evaluation rule or the expression is not well defined. This is a question of mathematics, and is not, as you say, a question of Fortran versus C. As it happens, the rules for evaluating expressions in Fortran are mathematically 'correct' in that they correctly model the mathematical properties of floating point numbers (in this instance), whereas those of C are not. Now it may well be that there are 'SOUND REASONS' for leaving the situation as it is. I would guess that the argument runs as follows: It will be much more expensive (in terms of compiler and preprocessor time) if parentheses must be honored in all instances, and even more expensive if they must be detected and honored only in the case of nonassociative operators. Furthermore, the insistence upon honoring order of evaluation will 'break' large classes of existing compilers. These are legitimate reasons. They amount to asserting that for economic and historical reasons it is preferable to be living in a certain amount of mathematical 'sin' rather than to be mathematically 'pure' after the fact. But please, do not misrepresent the mathematics of the situation. It is misleading to assert that the use of parantheses is simply a matter of grouping of common subexpressions. -- Richard Harter, SMDS Inc. [Disclaimers not permitted by company policy.]