e_log10l.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* $OpenBSD: e_log10l.c,v 1.2 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. /* log10l.c
  18. *
  19. * Common logarithm, long double precision
  20. *
  21. *
  22. *
  23. * SYNOPSIS:
  24. *
  25. * long double x, y, log10l();
  26. *
  27. * y = log10l( x );
  28. *
  29. *
  30. *
  31. * DESCRIPTION:
  32. *
  33. * Returns the base 10 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 30000 9.0e-20 2.6e-20
  52. * IEEE exp(+-10000) 30000 6.0e-20 2.3e-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 MINLOG
  61. * log domain: x < 0; returns MINLOG
  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 = 6.2e-22
  68. */
  69. static long double P[] = {
  70. 4.9962495940332550844739E-1L,
  71. 1.0767376367209449010438E1L,
  72. 7.7671073698359539859595E1L,
  73. 2.5620629828144409632571E2L,
  74. 4.2401812743503691187826E2L,
  75. 3.4258224542413922935104E2L,
  76. 1.0747524399916215149070E2L,
  77. };
  78. static long double Q[] = {
  79. /* 1.0000000000000000000000E0,*/
  80. 2.3479774160285863271658E1L,
  81. 1.9444210022760132894510E2L,
  82. 7.7952888181207260646090E2L,
  83. 1.6911722418503949084863E3L,
  84. 2.0307734695595183428202E3L,
  85. 1.2695660352705325274404E3L,
  86. 3.2242573199748645407652E2L,
  87. };
  88. /* Coefficients for log(x) = z + z^3 P(z^2)/Q(z^2),
  89. * where z = 2(x-1)/(x+1)
  90. * 1/sqrt(2) <= x < sqrt(2)
  91. * Theoretical peak relative error = 6.16e-22
  92. */
  93. static long double R[4] = {
  94. 1.9757429581415468984296E-3L,
  95. -7.1990767473014147232598E-1L,
  96. 1.0777257190312272158094E1L,
  97. -3.5717684488096787370998E1L,
  98. };
  99. static long double S[4] = {
  100. /* 1.00000000000000000000E0L,*/
  101. -2.6201045551331104417768E1L,
  102. 1.9361891836232102174846E2L,
  103. -4.2861221385716144629696E2L,
  104. };
  105. /* log10(2) */
  106. #define L102A 0.3125L
  107. #define L102B -1.1470004336018804786261e-2L
  108. /* log10(e) */
  109. #define L10EA 0.5L
  110. #define L10EB -6.5705518096748172348871e-2L
  111. #define SQRTH 0.70710678118654752440L
  112. long double
  113. log10l(long double x)
  114. {
  115. long double y;
  116. volatile long double z;
  117. int e;
  118. if( isnan(x) )
  119. return(x);
  120. /* Test for domain */
  121. if( x <= 0.0L )
  122. {
  123. if( x == 0.0L )
  124. return (-1.0L / (x - x));
  125. else
  126. return (x - x) / (x - x);
  127. }
  128. if( x == INFINITY )
  129. return(INFINITY);
  130. /* separate mantissa from exponent */
  131. /* Note, frexp is used so that denormal numbers
  132. * will be handled properly.
  133. */
  134. x = frexpl( x, &e );
  135. /* logarithm using log(x) = z + z**3 P(z)/Q(z),
  136. * where z = 2(x-1)/x+1)
  137. */
  138. if( (e > 2) || (e < -2) )
  139. {
  140. if( x < SQRTH )
  141. { /* 2( 2x-1 )/( 2x+1 ) */
  142. e -= 1;
  143. z = x - 0.5L;
  144. y = 0.5L * z + 0.5L;
  145. }
  146. else
  147. { /* 2 (x-1)/(x+1) */
  148. z = x - 0.5L;
  149. z -= 0.5L;
  150. y = 0.5L * x + 0.5L;
  151. }
  152. x = z / y;
  153. z = x*x;
  154. y = x * ( z * __polevll( z, R, 3 ) / __p1evll( z, S, 3 ) );
  155. goto done;
  156. }
  157. /* logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) */
  158. if( x < SQRTH )
  159. {
  160. e -= 1;
  161. x = ldexpl( x, 1 ) - 1.0L; /* 2x - 1 */
  162. }
  163. else
  164. {
  165. x = x - 1.0L;
  166. }
  167. z = x*x;
  168. y = x * ( z * __polevll( x, P, 6 ) / __p1evll( x, Q, 7 ) );
  169. y = y - ldexpl( z, -1 ); /* -0.5x^2 + ... */
  170. done:
  171. /* Multiply log of fraction by log10(e)
  172. * and base 2 exponent by log10(2).
  173. *
  174. * ***CAUTION***
  175. *
  176. * This sequence of operations is critical and it may
  177. * be horribly defeated by some compiler optimizers.
  178. */
  179. z = y * (L10EB);
  180. z += x * (L10EB);
  181. z += e * (L102B);
  182. z += y * (L10EA);
  183. z += x * (L10EA);
  184. z += e * (L102A);
  185. return( z );
  186. }