# to the nth power
Mark W. Schumann
catfood at NCoast.ORG
Fri Nov 2 10:28:30 AEST 1990
In article <9750 at helios.TAMU.EDU> randy at cs.tamu.edu (Randy Hutson) writes:
>In article <15984 at mentor.cc.purdue.edu-> edgincd2 at mentor.cc.purdue.edu (Chris Edgington *Computer Science Major*) writes:
>->In article <90305.005050CJH101 at psuvm.psu.edu->, CJH101 at psuvm.psu.edu (Carl J. Hixon) writes:
>->-> I appologize for bothering you computer wizards with such an elementary
>->-> question but, I'm floundering. Why am I unable to find an opperator which
>->-> raises a number to a power. (The fortran equivalent of x**n) Is there such
>->
>->I don't know if this is what you are looking for, but this is a neat little
>->trick to take a number to a power.
>->
>-> Answer = exp(ln(Root)*Exponent);
>->
>->Chris Edgington
>
>I use an even neater trick!
>
> Answer = pow(Root, Exponent);
Randy's answer presumes you have the 'pow' function in your library.
It *is* part of the ANSI standard, so if your compiler is ANSI, you're
groovin'.
If you are interested in an
integer version of the same function, try this:
int powi (int root, int exponent)
{
int i;
int result = 1;
for (i = exponent; i > 1; i--) result *= root;
return result;
}
Now you've gotta be careful about overflow here or use long ints.
If you needed an integer version I hope this helps.
--
============================================================
Mark W. Schumann 3111 Mapledale Avenue, Cleveland 44109 USA
Domain: catfood at ncoast.org
UUCP: ...!mailrus!usenet.ins.cwru.edu!ncoast!catfood
============================================================
More information about the Comp.lang.c
mailing list