exp.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 <math.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,
  80. 0036770,0033210,0063121,0061764,
  81. 0040200,0000000,0000000,0000000,
  82. };
  83. static unsigned short Q[] = {
  84. 0033511,0072665,0160662,0176377,
  85. 0036045,0070715,0124105,0132777,
  86. 0037550,0134114,0142077,0001637,
  87. 0040400,0000000,0000000,0000000,
  88. };
  89. static unsigned short sc1[] = {0040061,0071000,0000000,0000000};
  90. #define C1 (*(double *)sc1)
  91. static unsigned short sc2[] = {0033277,0137216,0075715,0057117};
  92. #define C2 (*(double *)sc2)
  93. #endif
  94. #ifdef IBMPC
  95. static unsigned short P[] = {
  96. 0x4be8,0xd5e4,0x89cd,0x3f20,
  97. 0x2c7e,0x0cca,0x06d1,0x3f9f,
  98. 0x0000,0x0000,0x0000,0x3ff0,
  99. };
  100. static unsigned short Q[] = {
  101. 0x5fa0,0xbc36,0x2eb6,0x3ec9,
  102. 0xb6c0,0xb508,0xae39,0x3f64,
  103. 0xe074,0x9887,0x1709,0x3fcd,
  104. 0x0000,0x0000,0x0000,0x4000,
  105. };
  106. static unsigned short sc1[] = {0x0000,0x0000,0x2e40,0x3fe6};
  107. #define C1 (*(double *)sc1)
  108. static unsigned short sc2[] = {0xabca,0xcf79,0xf7d1,0x3eb7};
  109. #define C2 (*(double *)sc2)
  110. #endif
  111. #ifdef MIEEE
  112. static unsigned short P[] = {
  113. 0x3f20,0x89cd,0xd5e4,0x4be8,
  114. 0x3f9f,0x06d1,0x0cca,0x2c7e,
  115. 0x3ff0,0x0000,0x0000,0x0000,
  116. };
  117. static unsigned short Q[] = {
  118. 0x3ec9,0x2eb6,0xbc36,0x5fa0,
  119. 0x3f64,0xae39,0xb508,0xb6c0,
  120. 0x3fcd,0x1709,0x9887,0xe074,
  121. 0x4000,0x0000,0x0000,0x0000,
  122. };
  123. static unsigned short sc1[] = {0x3fe6,0x2e40,0x0000,0x0000};
  124. #define C1 (*(double *)sc1)
  125. static unsigned short sc2[] = {0x3eb7,0xf7d1,0xcf79,0xabca};
  126. #define C2 (*(double *)sc2)
  127. #endif
  128. #ifdef ANSIPROT
  129. extern double polevl ( double, void *, int );
  130. extern double p1evl ( double, void *, int );
  131. extern double floor ( double );
  132. extern double ldexp ( double, int );
  133. extern int isnan ( double );
  134. extern int isfinite ( double );
  135. #else
  136. double polevl(), p1evl(), floor(), ldexp();
  137. int isnan(), isfinite();
  138. #endif
  139. extern double LOGE2, LOG2E, MAXLOG, MINLOG, MAXNUM;
  140. #ifdef INFINITIES
  141. extern double INFINITY;
  142. #endif
  143. double exp(x)
  144. double x;
  145. {
  146. double px, xx;
  147. int n;
  148. #ifdef NANS
  149. if( isnan(x) )
  150. return(x);
  151. #endif
  152. if( x > MAXLOG)
  153. {
  154. #ifdef INFINITIES
  155. return( INFINITY );
  156. #else
  157. mtherr( "exp", OVERFLOW );
  158. return( MAXNUM );
  159. #endif
  160. }
  161. if( x < MINLOG )
  162. {
  163. #ifndef INFINITIES
  164. mtherr( "exp", UNDERFLOW );
  165. #endif
  166. return(0.0);
  167. }
  168. /* Express e**x = e**g 2**n
  169. * = e**g e**( n loge(2) )
  170. * = e**( g + n loge(2) )
  171. */
  172. px = floor( LOG2E * x + 0.5 ); /* floor() truncates toward -infinity. */
  173. n = px;
  174. x -= px * C1;
  175. x -= px * C2;
  176. /* rational approximation for exponential
  177. * of the fractional part:
  178. * e**x = 1 + 2x P(x**2)/( Q(x**2) - P(x**2) )
  179. */
  180. xx = x * x;
  181. px = x * polevl( xx, P, 2 );
  182. x = px/( polevl( xx, Q, 3 ) - px );
  183. x = 1.0 + 2.0 * x;
  184. /* multiply by power of 2 */
  185. x = ldexp( x, n );
  186. return(x);
  187. }