Solved

Raising and integer to a power

Posted on 1997-12-31
4
1,816 Views
Last Modified: 2013-11-08
What is the common way in C to raise an integer value to a power. ie. FORTRAN = INT**3 which will raise the value 'INT' to the power of 3.
0
Comment
Question by:cummings
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1256881
  FORTRAN = INT*INT*INT;
or
   #include <math.h>
   FORTRAN = pow(INT,3.0); /* which actually does a float power */
or
   for( p=3,FORTRAN=1; p>0; p-=1 ){ FORTRAN *= INT; }
or
   for( p=3,FORTRAN=1; p>0; p>>=1 ){
     FORTRAN *= FORTRAN;
     if( p&1 ){ FORTRAN *= INT; }
   }
   
0
 

Accepted Solution

by:
rjh107 earned 50 total points
ID: 1256882
If you are not concerned with speed you could use the library function z = pow(x,y) (z=x^y) which involves converting integers to doubles and back again.

A somewhat faster method is included in snippets(http://www.snippets.org/) called IPOW.C which raises a double to an integer power and I'm sure you could convert this to an integer to an integral power

OR if you want ultimate speed, have lots of memory and only need to raise to one power set up a lookup table, ie a big array containing pre calculated values using one of above methods. So to find 43^7 you would set up an array called Pow3 and use z=Pow7(43)
0
 
LVL 84

Expert Comment

by:ozo
ID: 1256883
oops, got that last example backwards.
(it works for 3, but not if your power changes when reversed)

for( p=3,i=INT,FORTRAN=1; p>0; p>>=1 ){
   if( p&1 ){ FORTRAN *= i; }
   i *= i;
}  
0
 

Author Comment

by:cummings
ID: 1256884
I ended up by writing my own function as it only involves a couple of checks and a 'for' loop. Thay you for your response.

John
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question