e_expl.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* $OpenBSD: e_expl.c,v 1.3 2013/11/12 20:35:19 martynas Exp $ */
  2. /*
  3. * Copyright (c) 2008 Stephen L. Moshier <[email protected]>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* expl.c
  18. *
  19. * Exponential function, long double precision
  20. *
  21. *
  22. *
  23. * SYNOPSIS:
  24. *
  25. * long double x, y, expl();
  26. *
  27. * y = expl( x );
  28. *
  29. *
  30. *
  31. * DESCRIPTION:
  32. *
  33. * Returns e (2.71828...) raised to the x power.
  34. *
  35. * Range reduction is accomplished by separating the argument
  36. * into an integer k and fraction f such that
  37. *
  38. * x k f
  39. * e = 2 e.
  40. *
  41. * A Pade' form of degree 2/3 is used to approximate exp(f) - 1
  42. * in the basic range [-0.5 ln 2, 0.5 ln 2].
  43. *
  44. *
  45. * ACCURACY:
  46. *
  47. * Relative error:
  48. * arithmetic domain # trials peak rms
  49. * IEEE +-10000 50000 1.12e-19 2.81e-20
  50. *
  51. *
  52. * Error amplification in the exponential function can be
  53. * a serious matter. The error propagation involves
  54. * exp( X(1+delta) ) = exp(X) ( 1 + X*delta + ... ),
  55. * which shows that a 1 lsb error in representing X produces
  56. * a relative error of X times 1 lsb in the function.
  57. * While the routine gives an accurate result for arguments
  58. * that are exactly represented by a long double precision
  59. * computer number, the result contains amplified roundoff
  60. * error for large arguments not exactly represented.
  61. *
  62. *
  63. * ERROR MESSAGES:
  64. *
  65. * message condition value returned
  66. * exp underflow x < MINLOG 0.0
  67. * exp overflow x > MAXLOG MAXNUM
  68. *
  69. */
  70. /* Exponential function */
  71. #include <openlibm_math.h>
  72. #include "math_private.h"
  73. static long double P[3] = {
  74. 1.2617719307481059087798E-4L,
  75. 3.0299440770744196129956E-2L,
  76. 9.9999999999999999991025E-1L,
  77. };
  78. static long double Q[4] = {
  79. 3.0019850513866445504159E-6L,
  80. 2.5244834034968410419224E-3L,
  81. 2.2726554820815502876593E-1L,
  82. 2.0000000000000000000897E0L,
  83. };
  84. static const long double C1 = 6.9314575195312500000000E-1L;
  85. static const long double C2 = 1.4286068203094172321215E-6L;
  86. static const long double MAXLOGL = 1.1356523406294143949492E4L;
  87. static const long double MINLOGL = -1.13994985314888605586758E4L;
  88. static const long double LOG2EL = 1.4426950408889634073599E0L;
  89. long double
  90. expl(long double x)
  91. {
  92. long double px, xx;
  93. int n;
  94. if( isnan(x) )
  95. return(x);
  96. if( x > MAXLOGL)
  97. return( INFINITY );
  98. if( x < MINLOGL )
  99. return(0.0L);
  100. /* Express e**x = e**g 2**n
  101. * = e**g e**( n loge(2) )
  102. * = e**( g + n loge(2) )
  103. */
  104. px = floorl( LOG2EL * x + 0.5L ); /* floor() truncates toward -infinity. */
  105. n = px;
  106. x -= px * C1;
  107. x -= px * C2;
  108. /* rational approximation for exponential
  109. * of the fractional part:
  110. * e**x = 1 + 2x P(x**2)/( Q(x**2) - P(x**2) )
  111. */
  112. xx = x * x;
  113. px = x * __polevll( xx, P, 2 );
  114. x = px/( __polevll( xx, Q, 3 ) - px );
  115. x = 1.0L + ldexpl( x, 1 );
  116. x = ldexpl( x, n );
  117. return(x);
  118. }