Path: utzoo!utgpu!water!watmath!clyde!rutgers!sdcsvax!sdcc6!ix426 From: ix426@sdcc6.ucsd.EDU (tom stockfish) Newsgroups: comp.lang.c Subject: Re: Power proposal for ANSI C Keywords: power exponentiation ** Message-ID: <3573@sdcc6.ucsd.EDU> Date: 11 Jan 88 05:52:00 GMT References: <38384@sun.uucp> Reply-To: ix426@sdcc6.ucsd.edu.UUCP (tom stockfish) Organization: University of California, San Diego Lines: 63 In article <38384@sun.uucp> dgh%dgh@Sun.COM (David Hough) writes: > Recommendation: Add a power operator **, with the > -------------- >precedence of a cast operator, so that a cast expression can >be > > cast-expression ** cast-expression > >In b ** n, the base expression b may be an integral or >floating expression, but the exponent expression n must be >an integral expression. > >David Hough This proposal will never fly as it stands. 1. It is not in the spirit of C. Unlike any other C operator, a lexical analyzer will not be able to recognize it, since it is indistinguishable from very common pointer declarations, e.g., "char **argv". 2. The meaning of C expressions is almost always independent of white space. The one exception being a+++b versus a+ ++b. Even this one, however, is disambiguated by the "the longest possible token is the next one" rule. You would have to invent something else to disambiguate x ** y from x * (*y). 3. I'm not sure what precedence/associativity you have chosen for "**". From your posting I assume you mean that (double)2 ** 3 should group as ( (double)2 ) ** 3. This is disastrous. Since unary minus has the same precedence as casts, -x**2 + x - 10 would have to group as (-x) ** 2 + x - 10 which is equivalent to x**2 + x - 10. (The arbitrary precision language bc(1) actually has this precedence! Has any other numeric user used bc for serious work? I'd like to swap bug lists with you.) What associativity did you plan for "**"? Left to right, or right to left? All other binary operators are left to right, but this would be trivial for "**" since it would just collapse the exponents together: (x**y)**z == x**(y*z). Right to left associativity would be more useful, but would break the general rule for other C operators. You could make it non-associative like fortran originally did, but then it would be C's only non-associative operator. I think a good proposal would give the power operator a precedence above unary and below primary, with right to left associativity (I believe this is how fortran currently has it). The only thing left to decide is how it interacts with pre- or post-fix ++,--, and I'm not sure how that should be done. This should be well thought ought, and preferably implemented in some compiler, before a proposal is submitted to X3J11. -- || Tom Stockfisch, UCSD Chemistry tps@chem.ucsd.edu