s_log1pl.c 4.1 KB

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