Exponentiation in C (was: How not to write a loop)

Bill Gibbons bgibbons at Apple.COM
Wed Jun 29 03:37:12 AEST 1988


In article <4778 at haddock.ISC.COM> karl at haddock.ima.isc.com (Karl Heuer) writes:
>In article <808 at l.cc.purdue.edu> cik at l.cc.purdue.edu (Herman Rubin) writes:
>>In article <12784 at apple.Apple.COM>, bgibbons at Apple.COM (Bill Gibbons) writes:
>>>... raising a floating-point value to an integer (typed as integer) power is
>>>always done with repeated multiplies (in about log2(exponent) time),
>That's a strange comment to be posting to a C newsgroup.

Actually, I meant this from an algorithmic point of view: if the implementor
knows that the exponent is an integer (without checking, e.g. it was typed
that way) the library code should use repeated multiplies.  This is trivial to
do in C++ by overloading pow().  In C, you have to name the library routine
something else, of course.  Or, as has been noted, change the language.

Bill Gibbons
bgibbons at apple.apple.com (contractor)



More information about the Comp.lang.c mailing list