exp.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* exp.c
  2. *
  3. * Exponential function
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * double x, y, exp();
  10. *
  11. * y = exp( x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Returns e (2.71828...) 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. *
  22. * x k f
  23. * e = 2 e.
  24. *
  25. * A Pade' form 1 + 2x P(x**2)/( Q(x**2) - P(x**2) )
  26. * of degree 2/3 is used to approximate exp(f) in the basic
  27. * interval [-0.5, 0.5].
  28. *
  29. *
  30. * ACCURACY:
  31. *
  32. * Relative error:
  33. * arithmetic domain # trials peak rms
  34. * DEC +- 88 50000 2.8e-17 7.0e-18
  35. * IEEE +- 708 40000 2.0e-16 5.6e-17
  36. *
  37. *
  38. * Error amplification in the exponential function can be
  39. * a serious matter. The error propagation involves
  40. * exp( X(1+delta) ) = exp(X) ( 1 + X*delta + ... ),
  41. * which shows that a 1 lsb error in representing X produces
  42. * a relative error of X times 1 lsb in the function.
  43. * While the routine gives an accurate result for arguments
  44. * that are exactly represented by a double precision
  45. * computer number, the result contains amplified roundoff
  46. * error for large arguments not exactly represented.
  47. *
  48. *
  49. * ERROR MESSAGES:
  50. *
  51. * message condition value returned
  52. * exp underflow x < MINLOG 0.0
  53. * exp overflow x > MAXLOG INFINITY
  54. *
  55. */
  56. /*
  57. Cephes Math Library Release 2.8: June, 2000
  58. Copyright 1984, 1995, 2000 by Stephen L. Moshier
  59. */
  60. /* Exponential function */
  61. #include "mconf.h"
  62. #ifdef UNK
  63. static double P[] = {
  64. 1.26177193074810590878E-4,
  65. 3.02994407707441961300E-2,
  66. 9.99999999999999999910E-1,
  67. };
  68. static double Q[] = {
  69. 3.00198505138664455042E-6,
  70. 2.52448340349684104192E-3,
  71. 2.27265548208155028766E-1,
  72. 2.00000000000000000009E0,
  73. };
  74. static double C1 = 6.93145751953125E-1;
  75. static double C2 = 1.42860682030941723212E-6;
  76. #endif
  77. #ifdef DEC
  78. static unsigned short P[] = {
  79. 0035004, 0047156, 0127442, 0057502, 0036770, 0033210,
  80. 0063121, 0061764, 0040200, 0000000, 0000000, 0000000,
  81. };
  82. static unsigned short Q[] = {
  83. 0033511, 0072665, 0160662, 0176377, 0036045, 0070715, 0124105, 0132777,
  84. 0037550, 0134114, 0142077, 0001637, 0040400, 0000000, 0000000, 0000000,
  85. };
  86. static unsigned short sc1[] = {0040061, 0071000, 0000000, 0000000};
  87. #define C1 (*(double *)sc1)
  88. static unsigned short sc2[] = {0033277, 0137216, 0075715, 0057117};
  89. #define C2 (*(double *)sc2)
  90. #endif
  91. #ifdef IBMPC
  92. static unsigned short P[] = {
  93. 0x4be8, 0xd5e4, 0x89cd, 0x3f20, 0x2c7e, 0x0cca,
  94. 0x06d1, 0x3f9f, 0x0000, 0x0000, 0x0000, 0x3ff0,
  95. };
  96. static unsigned short Q[] = {
  97. 0x5fa0, 0xbc36, 0x2eb6, 0x3ec9, 0xb6c0, 0xb508, 0xae39, 0x3f64,
  98. 0xe074, 0x9887, 0x1709, 0x3fcd, 0x0000, 0x0000, 0x0000, 0x4000,
  99. };
  100. static unsigned short sc1[] = {0x0000, 0x0000, 0x2e40, 0x3fe6};
  101. #define C1 (*(double *)sc1)
  102. static unsigned short sc2[] = {0xabca, 0xcf79, 0xf7d1, 0x3eb7};
  103. #define C2 (*(double *)sc2)
  104. #endif
  105. #ifdef MIEEE
  106. static unsigned short P[] = {
  107. 0x3f20, 0x89cd, 0xd5e4, 0x4be8, 0x3f9f, 0x06d1,
  108. 0x0cca, 0x2c7e, 0x3ff0, 0x0000, 0x0000, 0x0000,
  109. };
  110. static unsigned short Q[] = {
  111. 0x3ec9, 0x2eb6, 0xbc36, 0x5fa0, 0x3f64, 0xae39, 0xb508, 0xb6c0,
  112. 0x3fcd, 0x1709, 0x9887, 0xe074, 0x4000, 0x0000, 0x0000, 0x0000,
  113. };
  114. static unsigned short sc1[] = {0x3fe6, 0x2e40, 0x0000, 0x0000};
  115. #define C1 (*(double *)sc1)
  116. static unsigned short sc2[] = {0x3eb7, 0xf7d1, 0xcf79, 0xabca};
  117. #define C2 (*(double *)sc2)
  118. #endif
  119. #ifdef ANSIPROT
  120. extern double polevl(double, void *, int);
  121. extern double p1evl(double, void *, int);
  122. extern double floor(double);
  123. extern double ldexp(double, int);
  124. extern int isnan(double);
  125. extern int isfinite(double);
  126. #else
  127. double polevl(), p1evl(), floor(), ldexp();
  128. int isnan(), isfinite();
  129. #endif
  130. extern double LOGE2, LOG2E, MAXLOG, MINLOG, MAXNUM;
  131. #ifdef INFINITIES
  132. extern double INFINITY;
  133. #endif
  134. double exp(x) double x;
  135. {
  136. double px, xx;
  137. int n;
  138. #ifdef NANS
  139. if (isnan(x))
  140. return (x);
  141. #endif
  142. if (x > MAXLOG) {
  143. #ifdef INFINITIES
  144. return (INFINITY);
  145. #else
  146. mtherr("exp", OVERFLOW);
  147. return (MAXNUM);
  148. #endif
  149. }
  150. if (x < MINLOG) {
  151. #ifndef INFINITIES
  152. mtherr("exp", UNDERFLOW);
  153. #endif
  154. return (0.0);
  155. }
  156. /* Express e**x = e**g 2**n
  157. * = e**g e**( n loge(2) )
  158. * = e**( g + n loge(2) )
  159. */
  160. px = floor(LOG2E * x + 0.5); /* floor() truncates toward -infinity. */
  161. n = px;
  162. x -= px * C1;
  163. x -= px * C2;
  164. /* rational approximation for exponential
  165. * of the fractional part:
  166. * e**x = 1 + 2x P(x**2)/( Q(x**2) - P(x**2) )
  167. */
  168. xx = x * x;
  169. px = x * polevl(xx, P, 2);
  170. x = px / (polevl(xx, Q, 3) - px);
  171. x = 1.0 + 2.0 * x;
  172. /* multiply by power of 2 */
  173. x = ldexp(x, n);
  174. return (x);
  175. }