powif.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* powif.c
  2. *
  3. * Real raised to integer power
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float x, y, powif();
  10. * int n;
  11. *
  12. * y = powif( 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. * IEEE .04,26 -26,26 100000 1.1e-6 2.0e-7
  32. * IEEE 1,2 -128,128 100000 1.1e-5 1.0e-6
  33. *
  34. * Returns MAXNUMF on overflow, zero on underflow.
  35. *
  36. */
  37. /* powi.c */
  38. /*
  39. Cephes Math Library Release 2.2: June, 1992
  40. Copyright 1984, 1987, 1989 by Stephen L. Moshier
  41. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  42. */
  43. #include <math.h>
  44. extern float MAXNUMF, MAXLOGF, MINLOGF, LOGE2F;
  45. float frexpf( float, int * );
  46. float powif( float x, int nn )
  47. {
  48. int n, e, sign, asign, lx;
  49. float w, y, s;
  50. if( x == 0.0 )
  51. {
  52. if( nn == 0 )
  53. return( 1.0 );
  54. else if( nn < 0 )
  55. return( MAXNUMF );
  56. else
  57. return( 0.0 );
  58. }
  59. if( nn == 0 )
  60. return( 1.0 );
  61. if( x < 0.0 )
  62. {
  63. asign = -1;
  64. x = -x;
  65. }
  66. else
  67. asign = 0;
  68. if( nn < 0 )
  69. {
  70. sign = -1;
  71. n = -nn;
  72. /*
  73. x = 1.0/x;
  74. */
  75. }
  76. else
  77. {
  78. sign = 0;
  79. n = nn;
  80. }
  81. /* Overflow detection */
  82. /* Calculate approximate logarithm of answer */
  83. s = frexpf( x, &lx );
  84. e = (lx - 1)*n;
  85. if( (e == 0) || (e > 64) || (e < -64) )
  86. {
  87. s = (s - 7.0710678118654752e-1) / (s + 7.0710678118654752e-1);
  88. s = (2.9142135623730950 * s - 0.5 + lx) * nn * LOGE2F;
  89. }
  90. else
  91. {
  92. s = LOGE2F * e;
  93. }
  94. if( s > MAXLOGF )
  95. {
  96. mtherr( "powi", OVERFLOW );
  97. y = MAXNUMF;
  98. goto done;
  99. }
  100. if( s < MINLOGF )
  101. return(0.0);
  102. /* Handle tiny denormal answer, but with less accuracy
  103. * since roundoff error in 1.0/x will be amplified.
  104. * The precise demarcation should be the gradual underflow threshold.
  105. */
  106. if( s < (-MAXLOGF+2.0) )
  107. {
  108. x = 1.0/x;
  109. sign = 0;
  110. }
  111. /* First bit of the power */
  112. if( n & 1 )
  113. y = x;
  114. else
  115. {
  116. y = 1.0;
  117. asign = 0;
  118. }
  119. w = x;
  120. n >>= 1;
  121. while( n )
  122. {
  123. w = w * w; /* arg to the 2-to-the-kth power */
  124. if( n & 1 ) /* if that bit is set, then include in product */
  125. y *= w;
  126. n >>= 1;
  127. }
  128. done:
  129. if( asign )
  130. y = -y; /* odd power of negative number */
  131. if( sign )
  132. y = 1.0/y;
  133. return(y);
  134. }