473,396 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

i need help in writing a power function..

29
hi
i need help in writing a function
integerPower(base,exponent) that returns the value base^exponent with assuming that the exponent is a positive, nonzero integer and that the base is an integer. the function integerPower should use for or while loop to control the calculations, without using any math library functions.
Jan 17 '07 #1
5 3919
Ganon11
3,652 Expert 2GB
Think about what an exponent is. If I want, say, 3^7, that's 3*3*3*3*3*3*3. Generalize this for x^y, and you'll have your algorithm.
Jan 17 '07 #2
macklin01
145 100+
The nice thing about this is that your function will be _much_ faster than using the built-in pow( double,double ) function. You'd be amazed at the improvement in performance in graphics code when changing from something like
Expand|Select|Wrap|Line Numbers
  1. pow( x,2 ) + pow( y,2 )
to something like this
Expand|Select|Wrap|Line Numbers
  1. intpow(x,2) + intpow(y,2)
or better yet, since x^2 is used so frequently, make an inline function just for squares:
Expand|Select|Wrap|Line Numbers
  1. square(x) + square(y)
In some of my graphics and scientific computations, I've found enormous performance gains just from not using the built-in pow() function. This makes sense, since they probably use a Taylor series expanstion to deal with the genera case of a decimal power. -- Paul
Jan 21 '07 #3
elsa
29
thx alot..i appreciate ur help..and thx for the hint too..it is a new piece of information..
Jan 21 '07 #4
Elsa,

I don't think your question was answered, and while having good intentions, these responses underestimate the complexity of an integer power function pow(dbl, int) that is faster than MSFT's pow(), and is general purpose enough to handle any integer exponent.

My suggestion would be to first benchmark your compiler's pow() by running it in a loop a million or 10 million times, as I tweaked this function a little and got about 160% of the performance of the release version of VS C++'s code.

The function below has a run-time that is largely proportional to the size of the exponent, but should best most pow() functions for exponents < 1,000. For RISK processors, which have no hardware support for transcendental functions, this code will be ~ 10-20X that of the compiler's pow() function.

CAVEAT: This function does not check for results beyond the range of Intel's 80 bit internal, or 64 bit external floating-point representations.

--- begin code listing ---
Expand|Select|Wrap|Line Numbers
  1. #define  dbl    double
  2.  
  3. /*  ------------------------------------------------------------------------    */
  4. dbl  IntPow(dbl st0, int x)
  5. {
  6.     unsigned int OrMask = UINT_MAX -1;
  7.     dbl  st1=1.0;
  8.  
  9.     /*  IntPow() computes pow(dbl y, int x) where x is an integer value.
  10.     IntPow() is about 2-3 times faster for the special case where x is an
  11.     integer than the general pow(dbl, dbl) function, where x is a double.
  12.     I expect that multiple may be more like    10 to 1 on RISC computers
  13.         as it avoids the use of transcendentals,
  14.         which RISC machines have no hardware support for.
  15.     */
  16.  
  17.         if(2==x) return (st0 * st0);
  18.     if(0==x) return st1;
  19.  
  20.     while(1)
  21.     {
  22.         if ( UINT_MAX == (x|OrMask) ){      /*  if LSB is 1...    */
  23.             st1 = st1 * st0;
  24.         }
  25.         x = x >> 1;            /*    shift x right 1 bit...    */
  26.         if(!x) return st1;      /*    if x is 0 get out...    */
  27.         st0 = st0 * st0;
  28.     }
  29. }
  30.  
Nov 30 '12 #5
donbock
2,426 Expert 2GB
@solidpoint: elsa said the exponent and base are both nonnegative integers. I suppose that leads to this prototype:
long intpow(unsigned int base, unsigned int exponent);
Nevertheless, your point is valid; it is not uncommon to work hard to optimize a function only to end up slowing it down.
Dec 2 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Mathew | last post by:
I am looking to write a program with Microsoft Visual Basic 6 that writes out to the com port. After the inital Connection. I will be connecting at 9600, 8 data bits, with hardware flow control. ...
10
by: Jeff Wagner | last post by:
I am in the process of learning Python (obsessively so). I've been through a few tutorials and read a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting...
2
by: TSK | last post by:
I cannot get my focus() fn to work in my validateInput(userInput) fn. It will not allow the user to go back and correct invalid input like it should. Instead it goes right on through with the...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
21
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code...
8
by: jasson118 | last post by:
calculate the power of 2 integers. For example, power(2,3)=8, power(3,2)=9,power(4,0)=1. Not allow use power math function.Two integers are read from input by scanf. The program must produce an...
19
by: Gerry Ford | last post by:
I've been pecking away at writing a program that will calculate the inner product of two double-width four-vectors. Larry thinks I'm well started with the following source to populate a vector:...
1
by: javabeginner123 | last post by:
i have a java prob, and i have to solve it fast, but i'm just getting to know it, so plz help me solve it with full code completed, thanks so much. the prob is to create a monter fight and there is...
12
by: Nezhate | last post by:
Hi There, I'm writing a program that takes a decimal number and returns it's binary equivalent. the problem is that I don't know how to return (from function) the binary value as an array of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.