log1pl.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* origin: OpenBSD /usr/src/lib/libm/src/ld80/s_log1pl.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. * Relative error logarithm
  19. * Natural logarithm of 1+x, long double precision
  20. *
  21. *
  22. * SYNOPSIS:
  23. *
  24. * long double x, y, log1pl();
  25. *
  26. * y = log1pl( x );
  27. *
  28. *
  29. * DESCRIPTION:
  30. *
  31. * Returns the base e (2.718...) logarithm of 1+x.
  32. *
  33. * The argument 1+x is separated into its exponent and fractional
  34. * parts. If the exponent is between -1 and +1, the logarithm
  35. * of the fraction is approximated by
  36. *
  37. * log(1+x) = x - 0.5 x^2 + x^3 P(x)/Q(x).
  38. *
  39. * Otherwise, setting z = 2(x-1)/x+1),
  40. *
  41. * log(x) = z + z^3 P(z)/Q(z).
  42. *
  43. *
  44. * ACCURACY:
  45. *
  46. * Relative error:
  47. * arithmetic domain # trials peak rms
  48. * IEEE -1.0, 9.0 100000 8.2e-20 2.5e-20
  49. *
  50. * ERROR MESSAGES:
  51. *
  52. * log singularity: x-1 = 0; returns -INFINITY
  53. * log domain: x-1 < 0; returns NAN
  54. */
  55. #include "libm.h"
  56. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  57. long double log1pl(long double x)
  58. {
  59. return log1p(x);
  60. }
  61. #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
  62. /* Coefficients for log(1+x) = x - x^2 / 2 + x^3 P(x)/Q(x)
  63. * 1/sqrt(2) <= x < sqrt(2)
  64. * Theoretical peak relative error = 2.32e-20
  65. */
  66. static const long double P[] = {
  67. 4.5270000862445199635215E-5L,
  68. 4.9854102823193375972212E-1L,
  69. 6.5787325942061044846969E0L,
  70. 2.9911919328553073277375E1L,
  71. 6.0949667980987787057556E1L,
  72. 5.7112963590585538103336E1L,
  73. 2.0039553499201281259648E1L,
  74. };
  75. static const long double Q[] = {
  76. /* 1.0000000000000000000000E0,*/
  77. 1.5062909083469192043167E1L,
  78. 8.3047565967967209469434E1L,
  79. 2.2176239823732856465394E2L,
  80. 3.0909872225312059774938E2L,
  81. 2.1642788614495947685003E2L,
  82. 6.0118660497603843919306E1L,
  83. };
  84. /* Coefficients for log(x) = z + z^3 P(z^2)/Q(z^2),
  85. * where z = 2(x-1)/(x+1)
  86. * 1/sqrt(2) <= x < sqrt(2)
  87. * Theoretical peak relative error = 6.16e-22
  88. */
  89. static const long double R[4] = {
  90. 1.9757429581415468984296E-3L,
  91. -7.1990767473014147232598E-1L,
  92. 1.0777257190312272158094E1L,
  93. -3.5717684488096787370998E1L,
  94. };
  95. static const long double S[4] = {
  96. /* 1.00000000000000000000E0L,*/
  97. -2.6201045551331104417768E1L,
  98. 1.9361891836232102174846E2L,
  99. -4.2861221385716144629696E2L,
  100. };
  101. static const long double C1 = 6.9314575195312500000000E-1L;
  102. static const long double C2 = 1.4286068203094172321215E-6L;
  103. #define SQRTH 0.70710678118654752440L
  104. long double log1pl(long double xm1)
  105. {
  106. long double x, y, z;
  107. int e;
  108. if (isnan(xm1))
  109. return xm1;
  110. if (xm1 == INFINITY)
  111. return xm1;
  112. if (xm1 == 0.0)
  113. return xm1;
  114. x = xm1 + 1.0;
  115. /* Test for domain errors. */
  116. if (x <= 0.0) {
  117. if (x == 0.0)
  118. return -INFINITY;
  119. return NAN;
  120. }
  121. /* Separate mantissa from exponent.
  122. Use frexp so that denormal numbers will be handled properly. */
  123. x = frexpl(x, &e);
  124. /* logarithm using log(x) = z + z^3 P(z)/Q(z),
  125. where z = 2(x-1)/x+1) */
  126. if (e > 2 || e < -2) {
  127. if (x < SQRTH) { /* 2(2x-1)/(2x+1) */
  128. e -= 1;
  129. z = x - 0.5;
  130. y = 0.5 * z + 0.5;
  131. } else { /* 2 (x-1)/(x+1) */
  132. z = x - 0.5;
  133. z -= 0.5;
  134. y = 0.5 * x + 0.5;
  135. }
  136. x = z / y;
  137. z = x*x;
  138. z = x * (z * __polevll(z, R, 3) / __p1evll(z, S, 3));
  139. z = z + e * C2;
  140. z = z + x;
  141. z = z + e * C1;
  142. return z;
  143. }
  144. /* logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) */
  145. if (x < SQRTH) {
  146. e -= 1;
  147. if (e != 0)
  148. x = 2.0 * x - 1.0;
  149. else
  150. x = xm1;
  151. } else {
  152. if (e != 0)
  153. x = x - 1.0;
  154. else
  155. x = xm1;
  156. }
  157. z = x*x;
  158. y = x * (z * __polevll(x, P, 6) / __p1evll(x, Q, 6));
  159. y = y + e * C2;
  160. z = y - 0.5 * z;
  161. z = z + x;
  162. z = z + e * C1;
  163. return z;
  164. }
  165. #endif