delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/20/04:18:10

From: mert0407 AT sable DOT ox DOT ac DOT uk (George Foot)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: ' ^ '
Date: 17 Oct 1997 10:25:16 GMT
Organization: Oxford University, England
Lines: 38
Message-ID: <627eec$2ct$1@news.ox.ac.uk>
References: <199710160103 DOT SAA04693 AT adit DOT ap DOT net>
NNTP-Posting-Host: sable.ox.ac.uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019