Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ihnp4!ihlpf!nevin1 From: nevin1@ihlpf.ATT.COM (00704A-Liber) Newsgroups: comp.lang.c Subject: Re: Power proposal for ANSI C Message-ID: <3474@ihlpf.ATT.COM> Date: 22 Jan 88 01:41:15 GMT References: <38384@sun.uucp> <2252@haddock.ISC.COM> <3421@ihlpf.ATT.COM> <2335@haddock.ISC.COM> Reply-To: nevin1@ihlpf.UUCP (00704A-Liber,N.) Organization: AT&T Bell Laboratories - Naperville, Illinois Lines: 87 In article <2335@haddock.ISC.COM> karl@haddock.ima.isc.com (Karl Heuer) writes: >In article <3421@ihlpf.ATT.COM> nevin1@ihlpf.UUCP (00704A-Liber,N.) writes: >>What about 'unary-expression ++'? Currently, this is on the same level as >>pre-incrementing with the distinction being associativity. > >That's the way it's usually described, but an equivalent formulation is that >postfix unary has a higher precedence than prefix unary (and there's no need >to talk about "associativity" of a unary operator). This is already in the >ANSI grammar at the "postfix-expression" level. Not entirely true. Expressions such as *--pointer still require knowledge of associativity to be correctly evaluated. But I think that you are right about being able to split up postfix and prefix (I'll have to think about it some more). > >>Would you split these two and put power between them? > >Yes. That's what I said in the grammar as well as the English text. Assuming this is your order (from highest to lowest): 1. (); []; .; -> (left to right) 2. postfix unary (right to left) 3. power (right to left) 4. prefix unary (right to left) 5. *; /; % (left to right) 6 and below. the same as K&R Then --a power --b cannot be done. It would be grouped as (-- ((a power --) b), which is illegal. If power were to be added (PLEASE DON'T), I believe it should have a precedence below the unaries but above {*; /; and %) and associate left to right; it would match closer to the mathematical definition this way. Also, notice that all the right to left operators are either assignment or unary; having an augmented assignment operator for an operator which groups right to left seems inconsistent (and if power is to be added the corresponding augmented operator should be added, too). Also , by your method (++a pow b) == ++(a pow b) and (a pow b++) == (a pow (b++)) are how groupings would be done. This does not look intuitive at all. > >>>I think 2 *^ -1 should be undefined (as is 1 << -1), rather than zero. >> >>Since we are defining the 'power' operator and not the 'integer power' >>operator, 2 *^ (-1) should return (double)0.5 and then be demoted >>to an integer class, if necessary. The power operator should not return two >>different numbers ... based on the types of the arguments. This is one of >>the major reasons I don't like the intro of the power operator into C. > >Actually, we are defining several operators which share a common symbol. Just >as the "/" symbol invokes the integer-divide operator when both operands are >integers (it most certainly does *not* invoke floating-point divide and then >truncate the result), "*^" should invoke the integer-power operator when both >operands are integers. I thought that was the whole point! Good point here. But, I would rather have (2 pow (-1)) evaluate to 0, (1 pow (-1)) evaluate to 1 than to just be 'undefined'; i.e., crash my program! >>way you defined your precedence; i.e., 2 *^ -1 groups as (2 *^ -) 1 (sort of). >I already thought of that problem. It's covered by the grammar. > >power-expression("2*^-1") >postfix-expression("2") *^ cast-expression("-1") > unary-expression("-1") > unary-operator("-") cast-expression("1") > Then you are not clearly defining the precedence, in English. Please describe precisely how you want evaluation to go. >>(BTW, maybe ANSI should define (1 << -1) as right-shifting by 1--any >>response to this out there?) > >Bad idea. It would require the compiler to generate extra code if the right >operand is nonconstant. What does it do now if the the right operand is negative and inside a variable or an expression? -- _ __ NEVIN J. LIBER ..!ihnp4!ihlpf!nevin1 (312) 510-6194 ' ) ) "The secret compartment of my ring I fill / / _ , __o ____ with an Underdog super-energy pill." / (_