exp2.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* exp2.c
  2. *
  3. * Base 2 exponential function
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * double x, y, exp2();
  10. *
  11. * y = exp2( x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Returns 2 raised to the x power.
  18. *
  19. * Range reduction is accomplished by separating the argument
  20. * into an integer k and fraction f such that
  21. * x k f
  22. * 2 = 2 2.
  23. *
  24. * A Pade' form
  25. *
  26. * 1 + 2x P(x**2) / (Q(x**2) - x P(x**2) )
  27. *
  28. * approximates 2**x in the basic range [-0.5, 0.5].
  29. *
  30. *
  31. * ACCURACY:
  32. *
  33. * Relative error:
  34. * arithmetic domain # trials peak rms
  35. * IEEE -1022,+1024 30000 1.8e-16 5.4e-17
  36. *
  37. *
  38. * See exp.c for comments on error amplification.
  39. *
  40. *
  41. * ERROR MESSAGES:
  42. *
  43. * message condition value returned
  44. * exp underflow x < -MAXL2 0.0
  45. * exp overflow x > MAXL2 MAXNUM
  46. *
  47. * For DEC arithmetic, MAXL2 = 127.
  48. * For IEEE arithmetic, MAXL2 = 1024.
  49. */
  50. /*
  51. Cephes Math Library Release 2.8: June, 2000
  52. Copyright 1984, 1995, 2000 by Stephen L. Moshier
  53. */
  54. #include <math.h>
  55. #ifdef UNK
  56. static double P[] = {
  57. 2.30933477057345225087E-2,
  58. 2.02020656693165307700E1,
  59. 1.51390680115615096133E3,
  60. };
  61. static double Q[] = {
  62. /* 1.00000000000000000000E0,*/
  63. 2.33184211722314911771E2,
  64. 4.36821166879210612817E3,
  65. };
  66. #define MAXL2 1024.0
  67. #define MINL2 -1024.0
  68. #endif
  69. #ifdef DEC
  70. static unsigned short P[] = {
  71. 0036675,0027102,0122327,0053227,
  72. 0041241,0116724,0115412,0157355,
  73. 0042675,0036404,0101733,0132226,
  74. };
  75. static unsigned short Q[] = {
  76. /*0040200,0000000,0000000,0000000,*/
  77. 0042151,0027450,0077732,0160744,
  78. 0043210,0100661,0077550,0056560,
  79. };
  80. #define MAXL2 127.0
  81. #define MINL2 -127.0
  82. #endif
  83. #ifdef IBMPC
  84. static unsigned short P[] = {
  85. 0xead3,0x549a,0xa5c8,0x3f97,
  86. 0x5bde,0x9361,0x33ba,0x4034,
  87. 0x7693,0x907b,0xa7a0,0x4097,
  88. };
  89. static unsigned short Q[] = {
  90. /*0x0000,0x0000,0x0000,0x3ff0,*/
  91. 0x5c3c,0x0ffb,0x25e5,0x406d,
  92. 0x0bae,0x2fed,0x1036,0x40b1,
  93. };
  94. #define MAXL2 1024.0
  95. #define MINL2 -1022.0
  96. #endif
  97. #ifdef MIEEE
  98. static unsigned short P[] = {
  99. 0x3f97,0xa5c8,0x549a,0xead3,
  100. 0x4034,0x33ba,0x9361,0x5bde,
  101. 0x4097,0xa7a0,0x907b,0x7693,
  102. };
  103. static unsigned short Q[] = {
  104. /*0x3ff0,0x0000,0x0000,0x0000,*/
  105. 0x406d,0x25e5,0x0ffb,0x5c3c,
  106. 0x40b1,0x1036,0x2fed,0x0bae,
  107. };
  108. #define MAXL2 1024.0
  109. #define MINL2 -1022.0
  110. #endif
  111. #ifdef ANSIPROT
  112. extern double polevl ( double, void *, int );
  113. extern double p1evl ( double, void *, int );
  114. extern double floor ( double );
  115. extern double ldexp ( double, int );
  116. extern int isnan ( double );
  117. extern int isfinite ( double );
  118. #else
  119. double polevl(), p1evl(), floor(), ldexp();
  120. int isnan(), isfinite();
  121. #endif
  122. #ifdef INFINITIES
  123. extern double INFINITY;
  124. #endif
  125. extern double MAXNUM;
  126. double exp2(x)
  127. double x;
  128. {
  129. double px, xx;
  130. short n;
  131. #ifdef NANS
  132. if( isnan(x) )
  133. return(x);
  134. #endif
  135. if( x > MAXL2)
  136. {
  137. #ifdef INFINITIES
  138. return( INFINITY );
  139. #else
  140. mtherr( "exp2", OVERFLOW );
  141. return( MAXNUM );
  142. #endif
  143. }
  144. if( x < MINL2 )
  145. {
  146. #ifndef INFINITIES
  147. mtherr( "exp2", UNDERFLOW );
  148. #endif
  149. return(0.0);
  150. }
  151. xx = x; /* save x */
  152. /* separate into integer and fractional parts */
  153. px = floor(x+0.5);
  154. n = px;
  155. x = x - px;
  156. /* rational approximation
  157. * exp2(x) = 1 + 2xP(xx)/(Q(xx) - P(xx))
  158. * where xx = x**2
  159. */
  160. xx = x * x;
  161. px = x * polevl( xx, P, 2 );
  162. x = px / ( p1evl( xx, Q, 2 ) - px );
  163. x = 1.0 + ldexp( x, 1 );
  164. /* scale by power of 2 */
  165. x = ldexp( x, n );
  166. return(x);
  167. }