home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / math / cephes / ldouble / expl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-17  |  3.7 KB  |  166 lines

  1. /*                            expl.c
  2.  *
  3.  *    Exponential function, long double precision
  4.  *
  5.  *
  6.  *
  7.  * SYNOPSIS:
  8.  *
  9.  * long double x, y, expl();
  10.  *
  11.  * y = expl( 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 of degree 2/3 is used to approximate exp(f) - 1
  26.  * in the basic range [-0.5 ln 2, 0.5 ln 2].
  27.  *
  28.  *
  29.  * ACCURACY:
  30.  *
  31.  *                      Relative error:
  32.  * arithmetic   domain     # trials      peak         rms
  33.  *    IEEE      +-10000     50000       1.12e-19    2.81e-20
  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 long 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 < MINLOG         0.0
  51.  * exp overflow     x > MAXLOG         MAXNUM
  52.  *
  53.  */
  54.  
  55. /*
  56. Cephes Math Library Release 2.2:  December, 1990
  57. Copyright 1984, 1990 by Stephen L. Moshier
  58. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  59. */
  60.  
  61.  
  62. /*    Exponential function    */
  63.  
  64. #include "mconf.h"
  65. static char fname[] = {"expl"};
  66.  
  67. #ifdef UNK
  68. static long double P[3] = {
  69.  1.2617719307481059087798E-4L,
  70.  3.0299440770744196129956E-2L,
  71.  9.9999999999999999991025E-1L,
  72. };
  73. static long double Q[4] = {
  74.  3.0019850513866445504159E-6L,
  75.  2.5244834034968410419224E-3L,
  76.  2.2726554820815502876593E-1L,
  77.  2.0000000000000000000897E0L,
  78. };
  79. static long double C1 = 6.9314575195312500000000E-1;
  80. static long double C2 = 1.4286068203094172321215E-6;
  81. #endif
  82.  
  83. #ifdef DEC
  84. not supported in long double precision
  85. #endif
  86.  
  87. #ifdef IBMPC
  88. static short P[15] = {
  89. 0x424e,0x225f,0x6eaf,0x844e,0x3ff2,
  90. 0xf39e,0x5163,0x8866,0xf836,0x3ff9,
  91. 0xfffe,0xffff,0xffff,0xffff,0x3ffe,
  92. };
  93. static short Q[20] = {
  94. 0xff1e,0xb2fc,0xb5e1,0xc975,0x3fec,
  95. 0xff3e,0x45b5,0xcda8,0xa571,0x3ff6,
  96. 0x9ee1,0x3f03,0x4cc4,0xe8b8,0x3ffc,
  97. 0x0000,0x0000,0x0000,0x8000,0x4000,
  98. };
  99. static short sc1[] = {0x0000,0x0000,0x0000,0xb172,0x3ffe};
  100. #define C1 (*(long double *)sc1)
  101. static short sc2[] = {0x4f1e,0xcd5e,0x8e7b,0xbfbe,0x3feb};
  102. #define C2 (*(long double *)sc2)
  103. #endif
  104.  
  105. #ifdef MIEEE
  106. static long A[9] = {
  107. 0x3ff20000,0x844e6eaf,0x225f424e,
  108. 0x3ff90000,0xf8368866,0x5163f39e,
  109. 0x3ffe0000,0xffffffff,0xfffffffe,
  110. };
  111. static long B[12] = {
  112. 0x3fec0000,0xc975b5e1,0xb2fcff1e,
  113. 0x3ff60000,0xa571cda8,0x45b5ff3e,
  114. 0x3ffc0000,0xe8b84cc4,0x3f039ee1,
  115. 0x40000000,0x80000000,0x00000000,
  116. };
  117. static long sc1[] = {0x3ffe0000,0xb1720000,0x00000000};
  118. #define C1 (*(long double *)sc1)
  119. static long sc2[] = {0x3feb0000,0xbfbe8e7b,0xcd5e4f1e};
  120. #define C2 (*(long double *)sc2)
  121. #endif
  122.  
  123. extern long double LOG2EL, MAXLOGL, MINLOGL, MAXNUML;
  124.  
  125. long double expl(x)
  126. long double x;
  127. {
  128. long double px, xx;
  129. int n;
  130. long double polevll(), floorl(), ldexpl();
  131.  
  132. if( x > MAXLOGL)
  133.     {
  134.     mtherr( fname, OVERFLOW );
  135.     return( MAXNUML );
  136.     }
  137.  
  138. if( x < MINLOGL )
  139.     {
  140.     mtherr( fname, UNDERFLOW );
  141.     return(0.0L);
  142.     }
  143.  
  144. /* Express e**x = e**g 2**n
  145.  *   = e**g e**( n loge(2) )
  146.  *   = e**( g + n loge(2) )
  147.  */
  148. px = floorl( LOG2EL * x + 0.5L ); /* floor() truncates toward -infinity. */
  149. n = px;
  150. x -= px * C1;
  151. x -= px * C2;
  152.  
  153.  
  154. /* rational approximation for exponential
  155.  * of the fractional part:
  156.  * e**x =  1 + 2x P(x**2)/( Q(x**2) - P(x**2) )
  157.  */
  158. xx = x * x;
  159. px = x * polevll( xx, P, 2 );
  160. x =  px/( polevll( xx, Q, 3 ) - px );
  161. x = 1.0L + ldexpl( x, 1 );
  162.  
  163. x = ldexpl( x, n );
  164. return(x);
  165. }
  166.