Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!VAX1.CC.UAKRON.EDU!mcs.kent.edu!usenet.ins.cwru.edu!ncoast!catfood From: catfood@NCoast.ORG (Mark W. Schumann) Newsgroups: comp.lang.c Subject: Re: # to the nth power Message-ID: <1990Nov1.232830.17131@NCoast.ORG> Date: 1 Nov 90 23:28:30 GMT References: <90305.005050CJH101@psuvm.psu.edu-> <15984@mentor.cc.purdue.edu-> <9750@helios.TAMU.EDU> Organization: North Coast Public Access *NIX, Cleveland, OH Lines: 46 In article <9750@helios.TAMU.EDU> randy@cs.tamu.edu (Randy Hutson) writes: >In article <15984@mentor.cc.purdue.edu-> edgincd2@mentor.cc.purdue.edu (Chris Edgington *Computer Science Major*) writes: >->In article <90305.005050CJH101@psuvm.psu.edu->, CJH101@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@ncoast.org UUCP: ...!mailrus!usenet.ins.cwru.edu!ncoast!catfood ============================================================