Implementation of pow(3m) function

Andrew Koenig ark at alice.UUCP
Sun Aug 5 00:25:17 AEST 1990


In article <46584 at brunix.UUCP>, gvr at cs.brown.edu (George V. Reilly) writes:

> There are many common special cases where calling pow is unwise:
> when y = 0.5, use sqrt instead; when y is a small positive or negative
> integer, just do the multiplications by hand; when y is of the form
> 1/k (i.e., you're trying to find the k-th root of x), use the
> Newton-Raphson method instead.

A lot depends on what you're trying to do.

Getting an accurate pow() function is very, very difficult.  Even if
your exp() and log() return results that are the most accurate they
can possibly be, I believe that evaluating exp(y*log(x)) for pow(x,y)
can lose up to 12 low-order bits of precision.

I have seen pow() functions that, in the interest of speed, check
for various special values of the second argument (0.5, integers,
and so on) and use fast algorithms for those cases.  If such an
implementation uses exp and log for the ordinary cases, it will
almost surely not be monotonic.  That is, it will be possible to
find positive x, y, and z such that y > z and pow(x,y) < pow(x,z).

I believe that if I were to set out to write a good pow() function
from scratch, it would take me at least several months.
-- 
				--Andrew Koenig
				  ark at europa.att.com



More information about the Comp.lang.c mailing list