home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / math / cephes / cmath / powi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-17  |  2.6 KB  |  164 lines

  1. /*                            powi.c
  2.  *
  3.  *    Real raised to integer power
  4.  *
  5.  *
  6.  *
  7.  * SYNOPSIS:
  8.  *
  9.  * double x, y, powi();
  10.  * int n;
  11.  *
  12.  * y = powi( x, n );
  13.  *
  14.  *
  15.  *
  16.  * DESCRIPTION:
  17.  *
  18.  * Returns argument x raised to the nth power.
  19.  * The routine efficiently decomposes n as a sum of powers of
  20.  * two. The desired power is a product of two-to-the-kth
  21.  * powers of x.  Thus to compute the 32767 power of x requires
  22.  * 28 multiplications instead of 32767 multiplications.
  23.  *
  24.  *
  25.  *
  26.  * ACCURACY:
  27.  *
  28.  *
  29.  *                      Relative error:
  30.  * arithmetic   x domain   n domain  # trials      peak         rms
  31.  *    DEC       .04,26     -26,26    100000       2.7e-16     4.3e-17
  32.  *    IEEE      .04,26     -26,26     50000       2.0e-15     3.8e-16
  33.  *    IEEE        1,2    -1022,1023   50000       8.6e-14     1.6e-14
  34.  *
  35.  * Returns MAXNUM on overflow, zero on underflow.
  36.  *
  37.  */
  38.  
  39. /*                            powi.c    */
  40.  
  41. /*
  42. Cephes Math Library Release 2.1:  January, 1989
  43. Copyright 1984, 1987, 1989 by Stephen L. Moshier
  44. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  45. */
  46.  
  47. #include "mconf.h"
  48. extern double MAXNUM, MAXLOG, MINLOG, LOGE2;
  49.  
  50. double powi( x, nn )
  51. double x;
  52. int nn;
  53. {
  54. int n, e, sign, asign, lx;
  55. short *p;
  56. double w, y, s;
  57. double log(), frexp();
  58.  
  59. if( x == 0.0 )
  60.     {
  61.     if( nn == 0 )
  62.         return( 1.0 );
  63.     else if( nn < 0 )
  64.         return( MAXNUM );
  65.     else
  66.         return( 0.0 );
  67.     }
  68.  
  69. if( nn == 0 )
  70.     return( 1.0 );
  71.  
  72.  
  73. if( x < 0.0 )
  74.     {
  75.     asign = -1;
  76.     x = -x;
  77.     }
  78. else
  79.     asign = 0;
  80.  
  81.  
  82. if( nn < 0 )
  83.     {
  84.     sign = -1;
  85.     n = -nn;
  86.     }
  87. else
  88.     {
  89.     sign = 1;
  90.     n = nn;
  91.     }
  92.  
  93. /* Overflow detection */
  94.  
  95. /* Calculate approximate logarithm of answer */
  96. s = frexp( x, &lx );
  97. e = (lx - 1)*n;
  98. if( (e == 0) || (e > 64) || (e < -64) )
  99.     {
  100.     s = (s - 7.0710678118654752e-1) / (s +  7.0710678118654752e-1);
  101.     s = (2.9142135623730950 * s - 0.5 + lx) * nn * LOGE2;
  102.     }
  103. else
  104.     {
  105.     s = LOGE2 * e;
  106.     }
  107.  
  108. if( s > MAXLOG )
  109.     {
  110.     mtherr( "powi", OVERFLOW );
  111.     y = MAXNUM;
  112.     goto done;
  113.     }
  114.  
  115. #if 0
  116. if( s < MINLOG )
  117.     return(0.0);
  118.  
  119. /* Handle tiny denormal answer, but with less accuracy
  120.  * since roundoff error in 1.0/x will be amplified.
  121.  * The precise demarcation should be the gradual underflow threshold.
  122.  */
  123. if( (s < (-MAXLOG+2.0)) && (sign < 0) )
  124.     {
  125.     x = 1.0/x;
  126.     sign = -sign;
  127.     }
  128. #else
  129. /* do not produce denormal answer */
  130. if( s < -MAXLOG )
  131.     return(0.0);
  132. #endif
  133.  
  134.  
  135. /* First bit of the power */
  136. if( n & 1 )
  137.     y = x;
  138.         
  139. else
  140.     {
  141.     y = 1.0;
  142.     asign = 0;
  143.     }
  144.  
  145. w = x;
  146. n >>= 1;
  147. while( n )
  148.     {
  149.     w = w * w;    /* arg to the 2-to-the-kth power */
  150.     if( n & 1 )    /* if that bit is set, then include in product */
  151.         y *= w;
  152.     n >>= 1;
  153.     }
  154.  
  155.  
  156. done:
  157.  
  158. if( asign )
  159.     y = -y; /* odd power of negative number */
  160. if( sign < 0 )
  161.     y = 1.0/y;
  162. return(y);
  163. }
  164.