home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 June / SIMTEL_0692.cdr / msdos / c / cephes.arc / EXP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-20  |  5.0 KB  |  192 lines

  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.  *     x    k  f
  22.  *    e  = 2  e.
  23.  *
  24.  * A Pade' form of degree 2/3 is used to approximate exp(f) - 1
  25.  * in the basic range [-0.5 ln 2, 0.5 ln 2].
  26.  *
  27.  *
  28.  * ACCURACY:
  29.  *
  30.  *                      Relative error:
  31.  * arithmetic   range      # trials      peak         rms
  32.  *    DEC       0, MAXLOG   38000       3.0e-17     6.2e-18
  33.  *    IEEE      +- MAXLOG   10000       2.1e-16     5.5e-17
  34.  *
  35.  *
  36.  * Error amplification in the exponential function can be
  37.  * a serious matter.  The error propagation involves
  38.  * exp( X(1+delta) ) = exp(X) ( 1 + X*delta + ... ),
  39.  * which shows that a 1 lsb error in representing X produces
  40.  * a relative error of X times 1 lsb in the function.
  41.  * While the routine gives an accurate result for arguments
  42.  * that are exactly represented by a double precision
  43.  * computer number, the result contains amplified roundoff
  44.  * error for large arguments not exactly represented.
  45.  *
  46.  *
  47.  * ERROR MESSAGES:
  48.  *
  49.  *   message         condition      value returned
  50.  * exp underflow    x < -MAXLOG        0.0
  51.  * exp overflow     x > MAXLOG         MAXNUM
  52.  *
  53.  */
  54.  
  55. /*
  56. Cephes Math Library Release 2.0:  April, 1987
  57. Copyright 1984, 1987 by Stephen L. Moshier
  58. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  59. */
  60.  
  61. /*    Exponential function    */
  62.  
  63. #include "mconf.h"
  64. static char fname[] = {"exp"};
  65.  
  66. #ifdef UNK
  67.  
  68. static double P[] = {
  69.  1.26183092834458542160E-4,
  70.  3.02996887658430129200E-2,
  71.  1.00000000000000000000E0
  72. };
  73. static double Q[] = {
  74. 3.00227947279887615146E-6,
  75. 2.52453653553222894311E-3,
  76. 2.27266044198352679519E-1,
  77. 2.00000000000000000005E0
  78. };
  79. static double C1 = 6.9335937500000000000E-1;
  80. static double C2 = 2.1219444005469058277E-4;
  81. #endif
  82.  
  83. #ifdef DEC
  84. static short P[] = {
  85. 0035004,0050004,0016315,0134545,
  86. 0036770,0033415,0105201,0034462,
  87. 0040200,0000000,0000000,0000000
  88. };
  89. static short Q[] = {
  90. 0033511,0075304,0141275,0061006,
  91. 0036045,0071261,0155620,0021143,
  92. 0037550,0134156,0006512,0174363,
  93. 0040400,0000000,0000000,0000000
  94. };
  95. static short sc1[] = {0040061,0100000,0000000,0000000};
  96. #define C1 (*(double *)sc1)
  97. static short sc2[] = {0035136,0100202,0161410,0062503};
  98. #define C2 (*(double *)sc2)
  99. #endif
  100.  
  101. #ifdef IBMPC
  102. static short P[] = {
  103. 0xb72d,0x8399,0x8a00,0x3f20,
  104. 0x2726,0xb150,0x06e1,0x3f9f,
  105. 0x0000,0x0000,0x0000,0x3ff0
  106. };
  107. static short Q[] = {
  108. 0xac41,0x9857,0x2f58,0x3ec9,
  109. 0x044c,0x3b72,0xae56,0x3f64,
  110. 0x5f1e,0xc1a9,0x170d,0x3fcd,
  111. 0x0000,0x0000,0x0000,0x4000
  112. };
  113. static short sc1[] = {0x0000,0x0000,0x3000,0x3fe6};
  114. #define C1 (*(double *)sc1)
  115. static short sc2[] = {0x0ca8,0x5c61,0xd010,0x3f2b};
  116. #define C2 (*(double *)sc2)
  117. #endif
  118.  
  119. #ifdef MIEEE
  120. static short P[] = {
  121. 0x3f20,0x8a00,0x8399,0xb72d,
  122. 0x3f9f,0x06e1,0xb150,0x2726,
  123. 0x3ff0,0x0000,0x0000,0x0000
  124. };
  125. static short Q[] = {
  126. 0x3ec9,0x2f58,0x9857,0xac41,
  127. 0x3f64,0xae56,0x3b72,0x044c,
  128. 0x3fcd,0x170d,0xc1a9,0x5f1e,
  129. 0x4000,0x0000,0x0000,0x0000
  130. };
  131. static short sc1[] = {
  132. 0x3fe6,0x3000,0x0000,0x0000
  133. };
  134. #define C1 (*(double *)sc1)
  135. static short sc2[] = {
  136. 0x3f2b,0xd010,0x5c61,0x0ca8
  137. };
  138. #define C2 (*(double *)sc2)
  139. #endif
  140.  
  141. extern double LOGE2, LOG2E, MAXLOG, MINLOG, MAXNUM;
  142.  
  143. double exp(x)
  144. double x;
  145. {
  146. double px, qx, xx;
  147. int n;
  148. double polevl();
  149. double floor(), frexp(), ldexp();
  150.  
  151. if( x > MAXLOG)
  152.     {
  153.     mtherr( fname, OVERFLOW );
  154.     return( MAXNUM );
  155.     }
  156.  
  157. if( x < -MAXLOG )
  158.     {
  159.     mtherr( fname, UNDERFLOW );
  160.     return(0.0);
  161.     }
  162.  
  163. /* The following is necessary because range reduction blows up: */
  164. /*
  165. if( x == 0 )
  166.     return(1.0);
  167. */
  168.  
  169. /* Express e**x = e**g 2**n
  170.  *   = e**g e**( n loge(2) )
  171.  *   = e**( g + n loge(2) )
  172.  */
  173. px = x * LOG2E;
  174. qx = floor( px + 0.5 ); /* floor() truncates toward -infinity. */
  175. n = qx;
  176. x -= qx * C1;
  177. x += qx * C2;
  178.  
  179.  
  180. /* rational approximation for exponential
  181.  * of the fractional part:
  182.  * e**x - 1  =  2x P(x**2)/( Q(x**2) - P(x**2) )
  183.  */
  184. xx = x * x;
  185. px = x * polevl( xx, P, 2 );
  186. x =  px/( polevl( xx, Q, 3 ) - px );
  187. x = ldexp( x, 1 );
  188. x =  x + 1.0;
  189. x = ldexp( x, n );
  190. return(x);
  191. }
  192.