logf.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* logf.c
  2. *
  3. * Natural logarithm
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float x, y, logf();
  10. *
  11. * y = logf( x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Returns the base e (2.718...) logarithm of x.
  18. *
  19. * The argument is separated into its exponent and fractional
  20. * parts. If the exponent is between -1 and +1, the logarithm
  21. * of the fraction is approximated by
  22. *
  23. * log(1+x) = x - 0.5 x**2 + x**3 P(x)
  24. *
  25. *
  26. *
  27. * ACCURACY:
  28. *
  29. * Relative error:
  30. * arithmetic domain # trials peak rms
  31. * IEEE 0.5, 2.0 100000 7.6e-8 2.7e-8
  32. * IEEE 1, MAXNUMF 100000 2.6e-8
  33. *
  34. * In the tests over the interval [1, MAXNUM], the logarithms
  35. * of the random arguments were uniformly distributed over
  36. * [0, MAXLOGF].
  37. *
  38. * ERROR MESSAGES:
  39. *
  40. * logf singularity: x = 0; returns MINLOG
  41. * logf domain: x < 0; returns MINLOG
  42. */
  43. /*
  44. Cephes Math Library Release 2.2: June, 1992
  45. Copyright 1984, 1987, 1988, 1992 by Stephen L. Moshier
  46. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  47. */
  48. /* Single precision natural logarithm
  49. * test interval: [sqrt(2)/2, sqrt(2)]
  50. * trials: 10000
  51. * peak relative error: 7.1e-8
  52. * rms relative error: 2.7e-8
  53. */
  54. #include <math.h>
  55. extern float MINLOGF, SQRTHF;
  56. float frexpf( float, int * );
  57. float logf( float xx )
  58. {
  59. register float y;
  60. float x, z, fe;
  61. int e;
  62. x = xx;
  63. fe = 0.0;
  64. /* Test for domain */
  65. if( x <= 0.0 )
  66. {
  67. if( x == 0.0 )
  68. mtherr( "logf", SING );
  69. else
  70. mtherr( "logf", DOMAIN );
  71. return( MINLOGF );
  72. }
  73. x = frexpf( x, &e );
  74. if( x < SQRTHF )
  75. {
  76. e -= 1;
  77. x = x + x - 1.0; /* 2x - 1 */
  78. }
  79. else
  80. {
  81. x = x - 1.0;
  82. }
  83. z = x * x;
  84. /* 3.4e-9 */
  85. /*
  86. p = logfcof;
  87. y = *p++ * x;
  88. for( i=0; i<8; i++ )
  89. {
  90. y += *p++;
  91. y *= x;
  92. }
  93. y *= z;
  94. */
  95. y =
  96. (((((((( 7.0376836292E-2 * x
  97. - 1.1514610310E-1) * x
  98. + 1.1676998740E-1) * x
  99. - 1.2420140846E-1) * x
  100. + 1.4249322787E-1) * x
  101. - 1.6668057665E-1) * x
  102. + 2.0000714765E-1) * x
  103. - 2.4999993993E-1) * x
  104. + 3.3333331174E-1) * x * z;
  105. if( e )
  106. {
  107. fe = e;
  108. y += -2.12194440e-4 * fe;
  109. }
  110. y += -0.5 * z; /* y - 0.5 x^2 */
  111. z = x + y; /* ... + x */
  112. if( e )
  113. z += 0.693359375 * fe;
  114. return( z );
  115. }