Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!mcvax!ukc!eagle!icdoc!cl-jenny!am From: am@cl.cam.ac.uk (Alan Mycroft) Newsgroups: comp.lang.c Subject: Re: Paying attention to parentheses Message-ID: <697@jenny.cl.cam.ac.uk> Date: Mon, 27-Apr-87 05:48:51 EDT Article-I.D.: jenny.697 Posted: Mon Apr 27 05:48:51 1987 Date-Received: Sun, 3-May-87 00:50:58 EDT References: <15958@sun.uucp> <5716@brl-smoke.ARPA> <532@omepd> <2094@mmintl.UUCP> Reply-To: am@cl.cam.ac.uk (Alan Mycroft) Organization: U of Cambridge Comp Lab, UK Lines: 26 Consider the following examples from an article by Jim Valerio (532@omepd): >>Here are some examples of parenthesized expressions, pulled from the >>Elefunt tests, which want the order of evaluation guaranteed by the >>parentheses: >> x = ((one + x * a) - one) * 16.0; >> y = x / ((half + x * half) *((half - x) + half)); >> x = (x + eight) - eight; >> while (((b+one)-b)-one == zero); >> if ((a+betam1)-a != zero) Frank Adams (Ashton Tate) then suggests that these need to be: > x = +(+(one + x * a) - one) * 16.0; (etc - lots of unary '+'s) Now, by my understanding of the ANSI draft, none of the above 4 expressions may be re-arranged so that none of the unary '+'s are required here: The only rearrangements that are allowed are ASSOCIATIVITY AND COMMUTATIVITY. So, by the mathematical definition, the only changes allowed are (a+b)+c <-> a+(b+c) and a+b <-> b+a and the analogues for '*'. None of the above expressions contain a+b+c or a*b*c and unary plus cannot be used anyway to stop a+b being re-arranged as b+a. Therefore using unary plus cannot affect the allowable compilations of the above expressions. As I have remarked before, the re-arrangements only apply to the above 4 cases. a+b-c CANNOT be rearranged as a-c+b or even a+(b-c) since '-' is not an associative commutative operator. Similar reasoning forbids any other rearrangement.