expf.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* expf.c
  2. *
  3. * Exponential function
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float x, y, expf();
  10. *
  11. * y = expf( 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 polynomial is used to approximate exp(f)
  26. * in the basic range [-0.5, 0.5].
  27. *
  28. *
  29. * ACCURACY:
  30. *
  31. * Relative error:
  32. * arithmetic domain # trials peak rms
  33. * IEEE +- MAXLOG 100000 1.7e-7 2.8e-8
  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. * expf underflow x < MINLOGF 0.0
  51. * expf overflow x > MAXLOGF MAXNUMF
  52. *
  53. */
  54. /*
  55. Cephes Math Library Release 2.2: June, 1992
  56. Copyright 1984, 1987, 1989 by Stephen L. Moshier
  57. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  58. */
  59. /* Single precision exponential function.
  60. * test interval: [-0.5, +0.5]
  61. * trials: 80000
  62. * peak relative error: 7.6e-8
  63. * rms relative error: 2.8e-8
  64. */
  65. #include <math.h>
  66. extern float LOG2EF, MAXLOGF, MINLOGF, MAXNUMF;
  67. static float C1 = 0.693359375;
  68. static float C2 = -2.12194440e-4;
  69. float floorf( float ), ldexpf( float, int );
  70. float expf( float xx )
  71. {
  72. float x, z;
  73. int n;
  74. x = xx;
  75. if( x > MAXLOGF)
  76. {
  77. mtherr( "expf", OVERFLOW );
  78. return( MAXNUMF );
  79. }
  80. if( x < MINLOGF )
  81. {
  82. mtherr( "expf", UNDERFLOW );
  83. return(0.0);
  84. }
  85. /* Express e**x = e**g 2**n
  86. * = e**g e**( n loge(2) )
  87. * = e**( g + n loge(2) )
  88. */
  89. z = floorf( LOG2EF * x + 0.5 ); /* floor() truncates toward -infinity. */
  90. x -= z * C1;
  91. x -= z * C2;
  92. n = z;
  93. z = x * x;
  94. /* Theoretical peak relative error in [-0.5, +0.5] is 4.2e-9. */
  95. z =
  96. ((((( 1.9875691500E-4 * x
  97. + 1.3981999507E-3) * x
  98. + 8.3334519073E-3) * x
  99. + 4.1665795894E-2) * x
  100. + 1.6666665459E-1) * x
  101. + 5.0000001201E-1) * z
  102. + x
  103. + 1.0;
  104. /* multiply by power of 2 */
  105. x = ldexpf( z, n );
  106. return( x );
  107. }