log.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package math32
  2. /*
  3. Floating-point logarithm.
  4. */
  5. // The original C code, the long comment, and the constants
  6. // below are from FreeBSD's /usr/src/lib/msun/src/e_log.c
  7. // and came with this notice. The go code is a simpler
  8. // version of the original C.
  9. //
  10. // ====================================================
  11. // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  12. //
  13. // Developed at SunPro, a Sun Microsystems, Inc. business.
  14. // Permission to use, copy, modify, and distribute this
  15. // software is freely granted, provided that this notice
  16. // is preserved.
  17. // ====================================================
  18. //
  19. // __ieee754_log(x)
  20. // Return the logarithm of x
  21. //
  22. // Method :
  23. // 1. Argument Reduction: find k and f such that
  24. // x = 2**k * (1+f),
  25. // where sqrt(2)/2 < 1+f < sqrt(2) .
  26. //
  27. // 2. Approximation of log(1+f).
  28. // Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
  29. // = 2s + 2/3 s**3 + 2/5 s**5 + .....,
  30. // = 2s + s*R
  31. // We use a special Reme algorithm on [0,0.1716] to generate
  32. // a polynomial of degree 14 to approximate R. The maximum error
  33. // of this polynomial approximation is bounded by 2**-58.45. In
  34. // other words,
  35. // 2 4 6 8 10 12 14
  36. // R(z) ~ L1*s +L2*s +L3*s +L4*s +L5*s +L6*s +L7*s
  37. // (the values of L1 to L7 are listed in the program) and
  38. // | 2 14 | -58.45
  39. // | L1*s +...+L7*s - R(z) | <= 2
  40. // | |
  41. // Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
  42. // In order to guarantee error in log below 1ulp, we compute log by
  43. // log(1+f) = f - s*(f - R) (if f is not too large)
  44. // log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy)
  45. //
  46. // 3. Finally, log(x) = k*Ln2 + log(1+f).
  47. // = k*Ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*Ln2_lo)))
  48. // Here Ln2 is split into two floating point number:
  49. // Ln2_hi + Ln2_lo,
  50. // where n*Ln2_hi is always exact for |n| < 2000.
  51. //
  52. // Special cases:
  53. // log(x) is NaN with signal if x < 0 (including -INF) ;
  54. // log(+INF) is +INF; log(0) is -INF with signal;
  55. // log(NaN) is that NaN with no signal.
  56. //
  57. // Accuracy:
  58. // according to an error analysis, the error is always less than
  59. // 1 ulp (unit in the last place).
  60. //
  61. // Constants:
  62. // The hexadecimal values are the intended ones for the following
  63. // constants. The decimal values may be used, provided that the
  64. // compiler will convert from decimal to binary accurately enough
  65. // to produce the hexadecimal values shown.
  66. // Log returns the natural logarithm of x.
  67. //
  68. // Special cases are:
  69. // Log(+Inf) = +Inf
  70. // Log(0) = -Inf
  71. // Log(x < 0) = NaN
  72. // Log(NaN) = NaN
  73. func Log(x float32) float32 {
  74. if haveArchLog {
  75. return archLog(x)
  76. }
  77. return log(x)
  78. }
  79. func log(x float32) float32 {
  80. const (
  81. Ln2Hi = 6.9313812256e-01 /* 0x3f317180 */
  82. Ln2Lo = 9.0580006145e-06 /* 0x3717f7d1 */
  83. L1 = 6.6666668653e-01 /* 0x3f2aaaab */
  84. L2 = 4.0000000596e-01 /* 0x3ecccccd */
  85. L3 = 2.8571429849e-01 /* 0x3e924925 */
  86. L4 = 2.2222198546e-01 /* 0x3e638e29 */
  87. L5 = 1.8183572590e-01 /* 0x3e3a3325 */
  88. L6 = 1.5313838422e-01 /* 0x3e1cd04f */
  89. L7 = 1.4798198640e-01 /* 0x3e178897 */
  90. )
  91. // special cases
  92. switch {
  93. case IsNaN(x) || IsInf(x, 1):
  94. return x
  95. case x < 0:
  96. return NaN()
  97. case x == 0:
  98. return Inf(-1)
  99. }
  100. // reduce
  101. f1, ki := Frexp(x)
  102. if f1 < Sqrt2/2 {
  103. f1 *= 2
  104. ki--
  105. }
  106. f := f1 - 1
  107. k := float32(ki)
  108. // compute
  109. s := f / (2 + f)
  110. s2 := s * s
  111. s4 := s2 * s2
  112. t1 := s2 * (L1 + s4*(L3+s4*(L5+s4*L7)))
  113. t2 := s4 * (L2 + s4*(L4+s4*L6))
  114. R := t1 + t2
  115. hfsq := 0.5 * f * f
  116. return k*Ln2Hi - ((hfsq - (s*(hfsq+R) + k*Ln2Lo)) - f)
  117. }