?
Solved

Raising and integer to a power

Posted on 1997-12-31
4
Medium Priority
?
1,817 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 150 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

765 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