powil.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* powil.c
  2. *
  3. * Real raised to integer power, long double precision
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * long double x, y, powil();
  10. * int n;
  11. *
  12. * y = powil( 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 .001,1000 -1022,1023 50000 4.3e-17 7.8e-18
  32. * IEEE 1,2 -1022,1023 20000 3.9e-17 7.6e-18
  33. * IEEE .99,1.01 0,8700 10000 3.6e-16 7.2e-17
  34. *
  35. * Returns MAXNUM on overflow, zero on underflow.
  36. *
  37. */
  38. /* powil.c */
  39. /*
  40. Cephes Math Library Release 2.2: December, 1990
  41. Copyright 1984, 1990 by Stephen L. Moshier
  42. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  43. */
  44. #include <math.h>
  45. extern long double MAXNUML, MAXLOGL, MINLOGL;
  46. extern long double LOGE2L;
  47. #ifdef ANSIPROT
  48. extern long double frexpl ( long double, int * );
  49. #else
  50. long double frexpl();
  51. #endif
  52. long double powil( x, nn )
  53. long double x;
  54. int nn;
  55. {
  56. long double w, y;
  57. long double s;
  58. int n, e, sign, asign, lx;
  59. if( x == 0.0L )
  60. {
  61. if( nn == 0 )
  62. return( 1.0L );
  63. else if( nn < 0 )
  64. return( MAXNUML );
  65. else
  66. return( 0.0L );
  67. }
  68. if( nn == 0 )
  69. return( 1.0L );
  70. if( x < 0.0L )
  71. {
  72. asign = -1;
  73. x = -x;
  74. }
  75. else
  76. asign = 0;
  77. if( nn < 0 )
  78. {
  79. sign = -1;
  80. n = -nn;
  81. }
  82. else
  83. {
  84. sign = 1;
  85. n = nn;
  86. }
  87. /* Overflow detection */
  88. /* Calculate approximate logarithm of answer */
  89. s = x;
  90. s = frexpl( s, &lx );
  91. e = (lx - 1)*n;
  92. if( (e == 0) || (e > 64) || (e < -64) )
  93. {
  94. s = (s - 7.0710678118654752e-1L) / (s + 7.0710678118654752e-1L);
  95. s = (2.9142135623730950L * s - 0.5L + lx) * nn * LOGE2L;
  96. }
  97. else
  98. {
  99. s = LOGE2L * e;
  100. }
  101. if( s > MAXLOGL )
  102. {
  103. mtherr( "powil", OVERFLOW );
  104. y = MAXNUML;
  105. goto done;
  106. }
  107. if( s < MINLOGL )
  108. {
  109. mtherr( "powil", UNDERFLOW );
  110. return(0.0L);
  111. }
  112. /* Handle tiny denormal answer, but with less accuracy
  113. * since roundoff error in 1.0/x will be amplified.
  114. * The precise demarcation should be the gradual underflow threshold.
  115. */
  116. if( s < (-MAXLOGL+2.0L) )
  117. {
  118. x = 1.0L/x;
  119. sign = -sign;
  120. }
  121. /* First bit of the power */
  122. if( n & 1 )
  123. y = x;
  124. else
  125. {
  126. y = 1.0L;
  127. asign = 0;
  128. }
  129. w = x;
  130. n >>= 1;
  131. while( n )
  132. {
  133. w = w * w; /* arg to the 2-to-the-kth power */
  134. if( n & 1 ) /* if that bit is set, then include in product */
  135. y *= w;
  136. n >>= 1;
  137. }
  138. done:
  139. if( asign )
  140. y = -y; /* odd power of negative number */
  141. if( sign < 0 )
  142. y = 1.0L/y;
  143. return(y);
  144. }