[sci.math.num-analysis] Power

be@dde.uucp (Bjorn Engsig) (01/18/88)

A short resume for sci.math.num-analysis readers:
-- A lot of people have written pro et contra for the inclusion
-- of a power (which power? most don't bother :-) operator in the
-- C programming language.  The discussion has been on which symbol
-- to use for power, where to put it in the operator hierarchy, and
-- wether it should have integer or real exponents.  C currently includes
-- a pow function with real exponents, defined for nonnegative mantissa
-- only.

Let me start to comment on power with integer exponents.

Pick up any first grade textbook on numerical analysis, and you will
be told NEVER to use a power function like that.  First, if you need to
calculate  x ** n (fortran notation), you will most often need
x ** (n-1)  etc. as well, and it should be calculated using recursion,
to optimize calculation time.  Second, you should bear in mind,
that using ordinary polynmials is often a bad practice in numerical 
computation, since they are higly unorthogonal (i.e. similar, try to
graph  x ** 9  and  x ** 10  for  x  between  0  and  1.).  If you are
doing computations with polynomials involved, you will normally pick a
set of orthogonal polynomials, such as the chebychef polynomials.
These kinds of polynomials are always calculated by recursion for speed
and accuracy.

Thus: Power with integer exponent is not needed!

Next, the power with real exponent, which we have as the pow() function,
is according to my knowledge not very heavily used.  If a lot of people
can come up with a lot of REAL application, where the call of pow()
really makes their code look ugly compared with an infix operator, one
MIGTH consider the inclusion of the operator.  However, a lot of non
numerics will definately use this operator for integer exponention, and
they will not understand why they can't square -2.0, and why their
progam is using that much CPU time.

Thus: Power with real exponent is not good!

-- 
Bjorn Engsig, E-mail:  ..!uunet!mcvax!diku!dde!be  or  be@dde.uucp
--
Hofstadters Law: It always take longer than you expect, even if you take into
		 account Hofstadters Law.

jmd@granite.dec.com (John Danskin) (01/20/88)

In article <302@Aragorn.dde.uucp> be@dde.uucp (Bjorn Engsig) writes:

>Let me start to comment on power with integer exponents.
>
>Pick up any first grade textbook on numerical analysis, and you will
>be told NEVER to use a power function like that.  First, if you need to
>calculate  x ** n (fortran notation), you will most often need
>x ** (n-1)  etc. as well, and it should be calculated using recursion,
....

Why do you think the compiler would generate n * log(x) for x^n where
n is an integer? The compiler knows that 'n' is an integer, and is
free to generate the most efficient code that it can -> including
iterative loops.


	x^n ==> {
		    tmp = x;
		    odd = 1;
		    if (n > 0) { 
			if (n & 1) {
			    odd = x;
			}
			while (n) {
			    tmp = tmp * tmp;
			    n = n >> 1;
			}
			return tmp * odd;
		    } else if (n < 0) {
			return (1/(x^-n));
		    } else { /* n == 0 */
			return 1.;
		    }
		}

this generates perfectly reasonable code (yes, it can be improved 8-)))
for x^n where n is an integer.

If n is known to be a constant, then the compiler can generate optimal code
for the integer power computation.

If you want x^r where r is a double or float, the compiler can generate
the appropriate log and exp calls....


So maybe you can elaborate on your difficulty with an exponentiation
operator in light of the surprising ability of compilers to generate
reasonable code.


John Danskin
decwrl!jmd

varol@cwi.nl (Varol Akman) (01/25/88)

In article <302@Aragorn.dde.uucp> be@dde.uucp (Bjorn Engsig) writes:
>
> [various points against power function]
>

A programmer likes to have anything that is conceptually easy to have
under his/her thumb.  To me any language without power function is only
a toy for wimps.