log10f.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* log10f.c
  2. *
  3. * Common logarithm
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float x, y, log10f();
  10. *
  11. * y = log10f( x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Returns logarithm to the base 10 of x.
  18. *
  19. * The argument is separated into its exponent and fractional
  20. * parts. The logarithm of the fraction is approximated by
  21. *
  22. * log(1+x) = x - 0.5 x**2 + x**3 P(x).
  23. *
  24. *
  25. *
  26. * ACCURACY:
  27. *
  28. * Relative error:
  29. * arithmetic domain # trials peak rms
  30. * IEEE 0.5, 2.0 100000 1.3e-7 3.4e-8
  31. * IEEE 0, MAXNUMF 100000 1.3e-7 2.6e-8
  32. *
  33. * In the tests over the interval [0, MAXNUM], the logarithms
  34. * of the random arguments were uniformly distributed over
  35. * [-MAXL10, MAXL10].
  36. *
  37. * ERROR MESSAGES:
  38. *
  39. * log10f singularity: x = 0; returns -MAXL10
  40. * log10f domain: x < 0; returns -MAXL10
  41. * MAXL10 = 38.230809449325611792
  42. */
  43. /*
  44. Cephes Math Library Release 2.1: December, 1988
  45. Copyright 1984, 1987, 1988 by Stephen L. Moshier
  46. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  47. */
  48. #include <math.h>
  49. static char fname[] = {"log10"};
  50. /* Coefficients for log(1+x) = x - x**2/2 + x**3 P(x)/Q(x)
  51. * 1/sqrt(2) <= x < sqrt(2)
  52. */
  53. static float P[] = {
  54. 7.0376836292E-2,
  55. -1.1514610310E-1,
  56. 1.1676998740E-1,
  57. -1.2420140846E-1,
  58. 1.4249322787E-1,
  59. -1.6668057665E-1,
  60. 2.0000714765E-1,
  61. -2.4999993993E-1,
  62. 3.3333331174E-1
  63. };
  64. #define SQRTH 0.70710678118654752440
  65. #define L102A 3.0078125E-1
  66. #define L102B 2.48745663981195213739E-4
  67. #define L10EA 4.3359375E-1
  68. #define L10EB 7.00731903251827651129E-4
  69. static float MAXL10 = 38.230809449325611792;
  70. float frexpf(float, int *), polevlf(float, float *, int);
  71. float log10f(float xx)
  72. {
  73. float x, y, z;
  74. int e;
  75. x = xx;
  76. /* Test for domain */
  77. if( x <= 0.0 )
  78. {
  79. if( x == 0.0 )
  80. mtherr( fname, SING );
  81. else
  82. mtherr( fname, DOMAIN );
  83. return( -MAXL10 );
  84. }
  85. /* separate mantissa from exponent */
  86. x = frexpf( x, &e );
  87. /* logarithm using log(1+x) = x - .5x**2 + x**3 P(x) */
  88. if( x < SQRTH )
  89. {
  90. e -= 1;
  91. x = 2.0*x - 1.0;
  92. }
  93. else
  94. {
  95. x = x - 1.0;
  96. }
  97. /* rational form */
  98. z = x*x;
  99. y = x * ( z * polevlf( x, P, 8 ) );
  100. y = y - 0.5 * z; /* y - 0.5 * x**2 */
  101. /* multiply log of fraction by log10(e)
  102. * and base 2 exponent by log10(2)
  103. */
  104. z = (x + y) * L10EB; /* accumulate terms in order of size */
  105. z += y * L10EA;
  106. z += x * L10EA;
  107. x = e;
  108. z += x * L102B;
  109. z += x * L102A;
  110. return( z );
  111. }