Solved

Raising and integer to a power

Posted on 1997-12-31
4
1,811 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
  • 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Specific format 21 196
rhino JavaScript import, load 25 82
Powershell Script need assistance to make some changes 4 79
Python 3.5.2 32 virtualenv problems 3 34
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

813 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now