Path: utzoo!utgpu!water!watmath!clyde!rutgers!gatech!hao!ames!oliveb!sun!dgh!dgh From: dgh%dgh@Sun.COM (David Hough) Newsgroups: comp.lang.c Subject: Power proposal for ANSI C Keywords: power exponentiation ** Message-ID: <38384@sun.uucp> Date: 10 Jan 88 02:12:09 GMT Sender: news@sun.uucp Lines: 91 I didn't think X3J11 would ever go for it, but now Doug Gwyn at least says he might support adding a power operator IF ANSI C has to go through a third public review period for some OTHER reason. With that degree of encouragement (coupled with the recent outpouring on comp.lang.c on this subject) I've added the following item to my list of 25 suggestions to be submitted to X3J11 at the next public review. Interestingly enough, although over 100 mathematical software people have looked at previous versions of my commentary, none of them so far has made a big deal about a power operator. Like me, they may not have thought it would ever be accepted. Comment #9, Section 3.3.4: add power operator for ------- - ------- - - - --- ----- -------- --- integral exponents -------- --------- C does not provide a power or exponentiation operator, like Pascal (which however provides a squaring operator), but unlike Fortran ** or Basic ^ . C's original system programming motivation does not require such an operator, and C's general approach is that operations that can not be implemented in a few machine instructions should be invoked as functions. Based on Fortran experience, this is excellent design; it discourages the common Fortran codings "x**0.5" and "x**2.0". Conversely, omitting an easy way to say "x**2" for complicated expressions x is a C oversight that adds gratuitous complexity to code. The complexity of general exponentiation to a floating-point power is sufficiently attested by the fact that such an operator was dropped from the final MC68881 architecture due to lack of microcode space in the initial implementation. Therefore I don't advocate that C add an operator notation for floating-point powers (although later -------- I advocate that pow() be made an operator in functional ---------- notation). I do advocate that an operator notation for exponentiation to integral powers be added. That some convenient syntax be standardized is more important than the specific syntax, and X3J11 is probably best qualified to choose from various alternative candidates such as **, ^^, ->, etc. Anybody who has written x**y or x ** y in old C code, intending x * *y, has probably done some other things that won't pass an ANSI C compiler. But ANSI C could be defined to accept x**y in its old meaning as well as x**y in its new since y must be a pointer in the first case and an integral expression in the second. But I doubt that's worthwhile. To best correspond to the usage of mathematics and other computer languages, the precedence of ** must be greater than that of a multiplicative or additive operator. 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. If n == 0, then the result is 1 cast to the type of b. If n > 0, then the result is the usual: the value of b is multiplied together n-1 times. If n < 0, then the result is the reciprocal of b ** (-n), which is 0 if b is an integral expression >= 2. Note that unlike other operators, the "usual arithmetic conversions" are not made to b and n; the type of the result is always that of b. The Rationale should encourage compilers to be clever when n is constant -1, 0, 1, or 2, but not so clever with other n that the intermediate results overflow or underflow unnecessarily. The Rationale should also encourage compilers to compute, wherever possible, "x * b ** n" by a shift instruction when b is the base of integer arithmetic, and by ldexp(x,n) when b is the base of floating-point arithmetic. David Hough ARPA: dhough@sun.com UUCP: {ucbvax,decvax,allegra,decwrl,cbosgd,ihnp4,seismo}!sun!dhough