Solved

Raising and integer to a power

Posted on 1997-12-31
4
1,801 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
Comment Utility
  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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

763 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