e_logl.c 4.4 KB

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