Path: utzoo!utgpu!water!watmath!clyde!rutgers!labrea!decwrl!pyramid!voder!tolerant!vsi1!steve From: steve@vsi1.UUCP (Steve Maurer) Newsgroups: comp.lang.c Subject: While talking about useful additions, how about this one?? Summary: An addition which resolves exponentiation & other problems elegantly. Message-ID: <253@vsi1.UUCP> Date: 13 Jan 88 05:14:42 GMT Reply-To: steve@vsi1.UUCP (Steve Maurer) Organization: Vicom Systems Inc. San Jose, Cal. Lines: 45 References: [l] [line] [lineater] > > Why is everyone so interested in a power operator compared to, say, > > square root or logarithm? > > Basically, it's because exponentiation involving one or two integers > can usually be done much faster than through use of the floating-point > pow() function, and in the common case **2 can be highly optimized. While we are on the subject of useful additions to C, how about this one: pre-compiler switching operators '??' and '::', which ask for evaluation at compile time, instead of run time. For example, if a novice C user wished to create an optimized form of pow(), without knowing the true implementation of it he might write: #define POW(X,Y) (Y) == 0.5 ? sqrt(X) : ( (Y) == 0 ? 1 : ( (Y) == 1 ? X :\ ( (Y) == 2 ? X * X : pow(X,Y) ) ) ) intending to 'optimize' the resulting code. Unfortunately, because of the way these operators work, the resulting code is horrendous. Consider however, the efficiency gained if the operators are evaluated at compile time. Presuming of course that (Y) above is a CONSTANT, various in line efficiencies could be squeezed out of a machine without resorting to numerous additions to the language. The actual definitions of '??' and '::' would be: * if in the precompiler, the expression evaluates to TRUE, then the statement immediately following ?? is produced, else the statment following the :: is produced. If no :: is present, and the expression value cannot be determined in the precompiler, then no code is produced. So in the above example, with all "?" replaced by "??" and ":" replaced by "::", the expression POW(i, 20/10) would be replaced by i * i, with no comparisons in the resulting assembly. { Of course you would still have to deal with the macro incremental assignment problem - e.g. POW(i++,3) - but such is life } Ok, boys. Shoot it down....... Steve Maurer