powi.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. /* powi.c */
  39. /*
  40. Cephes Math Library Release 2.8: June, 2000
  41. Copyright 1984, 1995, 2000 by Stephen L. Moshier
  42. */
  43. #include "mconf.h"
  44. #ifdef ANSIPROT
  45. extern double log(double);
  46. extern double frexp(double, int *);
  47. extern int signbit(double);
  48. #else
  49. double log(), frexp();
  50. int signbit();
  51. #endif
  52. extern double NEGZERO, INFINITY, MAXNUM, MAXLOG, MINLOG, LOGE2;
  53. double powi(x, nn) double x;
  54. int nn;
  55. {
  56. int n, e, sign, asign, lx;
  57. double w, y, s;
  58. /* See pow.c for these tests. */
  59. if (x == 0.0) {
  60. if (nn == 0)
  61. return (1.0);
  62. else if (nn < 0)
  63. return (INFINITY);
  64. else {
  65. if (nn & 1)
  66. return (x);
  67. else
  68. return (0.0);
  69. }
  70. }
  71. if (nn == 0)
  72. return (1.0);
  73. if (nn == -1)
  74. return (1.0 / x);
  75. if (x < 0.0) {
  76. asign = -1;
  77. x = -x;
  78. } else
  79. asign = 0;
  80. if (nn < 0) {
  81. sign = -1;
  82. n = -nn;
  83. } else {
  84. sign = 1;
  85. n = nn;
  86. }
  87. /* Even power will be positive. */
  88. if ((n & 1) == 0)
  89. asign = 0;
  90. /* Overflow detection */
  91. /* Calculate approximate logarithm of answer */
  92. s = frexp(x, &lx);
  93. e = (lx - 1) * n;
  94. if ((e == 0) || (e > 64) || (e < -64)) {
  95. s = (s - 7.0710678118654752e-1) / (s + 7.0710678118654752e-1);
  96. s = (2.9142135623730950 * s - 0.5 + lx) * nn * LOGE2;
  97. } else {
  98. s = LOGE2 * e;
  99. }
  100. if (s > MAXLOG) {
  101. mtherr("powi", OVERFLOW);
  102. y = INFINITY;
  103. goto done;
  104. }
  105. #if DENORMAL
  106. if (s < MINLOG) {
  107. y = 0.0;
  108. goto done;
  109. }
  110. /* Handle tiny denormal answer, but with less accuracy
  111. * since roundoff error in 1.0/x will be amplified.
  112. * The precise demarcation should be the gradual underflow threshold.
  113. */
  114. if ((s < (-MAXLOG + 2.0)) && (sign < 0)) {
  115. x = 1.0 / x;
  116. sign = -sign;
  117. }
  118. #else
  119. /* do not produce denormal answer */
  120. if (s < -MAXLOG)
  121. return (0.0);
  122. #endif
  123. /* First bit of the power */
  124. if (n & 1)
  125. y = x;
  126. else
  127. y = 1.0;
  128. w = x;
  129. n >>= 1;
  130. while (n) {
  131. w = w * w; /* arg to the 2-to-the-kth power */
  132. if (n & 1) /* if that bit is set, then include in product */
  133. y *= w;
  134. n >>= 1;
  135. }
  136. if (sign < 0)
  137. y = 1.0 / y;
  138. done:
  139. if (asign) {
  140. /* odd power of negative number */
  141. if (y == 0.0)
  142. y = NEGZERO;
  143. else
  144. y = -y;
  145. }
  146. return (y);
  147. }