1
0

expm1l.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_expm1l.c */
  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. /*
  18. * Exponential function, minus 1
  19. * Long double precision
  20. *
  21. *
  22. * SYNOPSIS:
  23. *
  24. * long double x, y, expm1l();
  25. *
  26. * y = expm1l( x );
  27. *
  28. *
  29. * DESCRIPTION:
  30. *
  31. * Returns e (2.71828...) raised to the x power, minus 1.
  32. *
  33. * Range reduction is accomplished by separating the argument
  34. * into an integer k and fraction f such that
  35. *
  36. * x k f
  37. * e = 2 e.
  38. *
  39. * An expansion x + .5 x^2 + x^3 R(x) approximates exp(f) - 1
  40. * in the basic range [-0.5 ln 2, 0.5 ln 2].
  41. *
  42. *
  43. * ACCURACY:
  44. *
  45. * Relative error:
  46. * arithmetic domain # trials peak rms
  47. * IEEE -45,+MAXLOG 200,000 1.2e-19 2.5e-20
  48. *
  49. * ERROR MESSAGES:
  50. *
  51. * message condition value returned
  52. * expm1l overflow x > MAXLOG MAXNUM
  53. *
  54. */
  55. #include "libm.h"
  56. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  57. long double expm1l(long double x)
  58. {
  59. return expm1(x);
  60. }
  61. #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
  62. static const long double MAXLOGL = 1.1356523406294143949492E4L;
  63. /* exp(x) - 1 = x + 0.5 x^2 + x^3 P(x)/Q(x)
  64. -.5 ln 2 < x < .5 ln 2
  65. Theoretical peak relative error = 3.4e-22 */
  66. static const long double
  67. P0 = -1.586135578666346600772998894928250240826E4L,
  68. P1 = 2.642771505685952966904660652518429479531E3L,
  69. P2 = -3.423199068835684263987132888286791620673E2L,
  70. P3 = 1.800826371455042224581246202420972737840E1L,
  71. P4 = -5.238523121205561042771939008061958820811E-1L,
  72. Q0 = -9.516813471998079611319047060563358064497E4L,
  73. Q1 = 3.964866271411091674556850458227710004570E4L,
  74. Q2 = -7.207678383830091850230366618190187434796E3L,
  75. Q3 = 7.206038318724600171970199625081491823079E2L,
  76. Q4 = -4.002027679107076077238836622982900945173E1L,
  77. /* Q5 = 1.000000000000000000000000000000000000000E0 */
  78. /* C1 + C2 = ln 2 */
  79. C1 = 6.93145751953125E-1L,
  80. C2 = 1.428606820309417232121458176568075500134E-6L,
  81. /* ln 2^-65 */
  82. minarg = -4.5054566736396445112120088E1L,
  83. huge = 0x1p10000L;
  84. long double expm1l(long double x)
  85. {
  86. long double px, qx, xx;
  87. int k;
  88. /* Overflow. */
  89. if (x > MAXLOGL)
  90. return huge*huge; /* overflow */
  91. if (x == 0.0)
  92. return x;
  93. /* Minimum value.*/
  94. if (x < minarg)
  95. return -1.0;
  96. xx = C1 + C2;
  97. /* Express x = ln 2 (k + remainder), remainder not exceeding 1/2. */
  98. px = floorl(0.5 + x / xx);
  99. k = px;
  100. /* remainder times ln 2 */
  101. x -= px * C1;
  102. x -= px * C2;
  103. /* Approximate exp(remainder ln 2).*/
  104. px = (((( P4 * x + P3) * x + P2) * x + P1) * x + P0) * x;
  105. qx = (((( x + Q4) * x + Q3) * x + Q2) * x + Q1) * x + Q0;
  106. xx = x * x;
  107. qx = x + (0.5 * xx + xx * px / qx);
  108. /* exp(x) = exp(k ln 2) exp(remainder ln 2) = 2^k exp(remainder ln 2).
  109. We have qx = exp(remainder ln 2) - 1, so
  110. exp(x) - 1 = 2^k (qx + 1) - 1 = 2^k qx + 2^k - 1. */
  111. px = scalbnl(1.0, k);
  112. x = px * qx + (px - 1.0);
  113. return x;
  114. }
  115. #endif