i need help in writing a power function..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • elsa
    New Member
    • Jan 2007
    • 29

    i need help in writing a power function..

    hi
    i need help in writing a function
    integerPower(ba se,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.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    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.

    Comment

    • macklin01
      New Member
      • Aug 2005
      • 145

      #3
      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
      Code:
      pow( x,2 ) + pow( y,2 )
      to something like this
      Code:
      intpow(x,2) + intpow(y,2)
      or better yet, since x^2 is used so frequently, make an inline function just for squares:
      Code:
      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

      Comment

      • elsa
        New Member
        • Jan 2007
        • 29

        #4
        thx alot..i appreciate ur help..and thx for the hint too..it is a new piece of information..

        Comment

        • solidpoint
          New Member
          • Nov 2012
          • 5

          #5
          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 ---
          Code:
          #define  dbl	double
           
          /*  ------------------------------------------------------------------------	*/
          dbl  IntPow(dbl st0, int x)
          {
          	unsigned int OrMask = UINT_MAX -1;
          	dbl  st1=1.0;
          
          	/*  IntPow() computes pow(dbl y, int x) where x is an integer value.
          	IntPow() is about 2-3 times faster for the special case where x is an
          	integer than the general pow(dbl, dbl) function, where x is a double.
          	I expect that multiple may be more like	10 to 1 on RISC computers
                  as it avoids the use of transcendentals,
                  which RISC machines have no hardware support for.
          	*/
          	
                  if(2==x) return (st0 * st0);
          	if(0==x) return st1;
          
          	while(1)
          	{
          		if ( UINT_MAX == (x|OrMask) ){	  /*  if LSB is 1...	*/
          			st1 = st1 * st0;
          		}
          		x = x >> 1;	    	/*	shift x right 1 bit...	*/
          		if(!x) return st1;  	/*	if x is 0 get out...	*/
          		st0 = st0 * st0;
          	}
          }
          Last edited by solidpoint; Nov 30 '12, 11:09 PM. Reason: formatting

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            @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.

            Comment

            Working...