Mail Archives: djgpp/1997/10/20/04:18:10
On Thu, 16 Oct 1997 01:03:02 GMT in comp.os.msdos.djgpp Nate Eldredge
(eldredge AT ap DOT net) wrote:
: floating-point function. For integers you can do something like:
: int int_pow(int x, int y)
: {
: int i;
: int t = x;
: for (i=1; i < y; i++) t *= x;
: return t;
: }
Or better (shorter, faster, less variables, and also allows zero powers ;)):
int int_pow (int x, int y) {
int a = 1;
while (y--) a*=x;
return a;
}
or faster for larger indices:
int int_pow (int x, int y) {
int a = 1;
if (y<0) return 0;
while (y) {
if (y&1) a*=x;
x*=x;
y>>=1;
}
return a;
}
hth :)
--
George Foot <mert0407 AT sable DOT ox DOT ac DOT uk>
Merton College, Oxford
- Raw text -