# to the nth power

Karl Heuer karl at ima.isc.com
Sun Nov 11 09:49:39 AEST 1990


In article <4233 at goanna.cs.rmit.oz.au> ok at goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
>In article <1990Nov9.152921 at lotus.lotus.com>, rnewman at lotus.lotus.com (Ron Newman ) writes:
>> Unfortunately, many implementations of pow(), such as in Unix System V
>> for PC's, don't produce acceptable results for integral exponents.
>
>SVID release 2, vol 1, p 170: [Specs for pow().]  If the answer was
>significantly in error (compared with the code in Cody & Waite, say) it would
>have been appropriate to complain to the vendor.

How much is "significantly"?  An error of a single ulp is enough to cause
(int)pow(2.0, 3.0) to return 7 instead of 8 (which it does, on several current
implementations).  So unless the standards bodies (*not* the individual
vendors) are willing to guarantee exactness for this function, using pow() is
an unportable solution to the problem of integer-to-integer exponentiation.

(It's still possible to use
	#define ipow(i,j) ((int)(pow((double)i, (double)j)+0.5))
for positive numbers, and a more complicated adjustment to handle negatives as
well, but now it's more work than writing an ipow() function from scratch.)

Karl W. Z. Heuer (karl at ima.isc.com or uunet!ima!karl), The Walking Lint



More information about the Comp.lang.c mailing list