log1p.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package math
  5. // The original C code, the long comment, and the constants
  6. // below are from FreeBSD's /usr/src/lib/msun/src/s_log1p.c
  7. // and came with this notice. The go code is a simplified
  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. //
  20. // double log1p(double x)
  21. //
  22. // Method :
  23. // 1. Argument Reduction: find k and f such that
  24. // 1+x = 2**k * (1+f),
  25. // where sqrt(2)/2 < 1+f < sqrt(2) .
  26. //
  27. // Note. If k=0, then f=x is exact. However, if k!=0, then f
  28. // may not be representable exactly. In that case, a correction
  29. // term is need. Let u=1+x rounded. Let c = (1+x)-u, then
  30. // log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
  31. // and add back the correction term c/u.
  32. // (Note: when x > 2**53, one can simply return log(x))
  33. //
  34. // 2. Approximation of log1p(f).
  35. // Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
  36. // = 2s + 2/3 s**3 + 2/5 s**5 + .....,
  37. // = 2s + s*R
  38. // We use a special Reme algorithm on [0,0.1716] to generate
  39. // a polynomial of degree 14 to approximate R The maximum error
  40. // of this polynomial approximation is bounded by 2**-58.45. In
  41. // other words,
  42. // 2 4 6 8 10 12 14
  43. // R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s
  44. // (the values of Lp1 to Lp7 are listed in the program)
  45. // and
  46. // | 2 14 | -58.45
  47. // | Lp1*s +...+Lp7*s - R(z) | <= 2
  48. // | |
  49. // Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
  50. // In order to guarantee error in log below 1ulp, we compute log
  51. // by
  52. // log1p(f) = f - (hfsq - s*(hfsq+R)).
  53. //
  54. // 3. Finally, log1p(x) = k*ln2 + log1p(f).
  55. // = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
  56. // Here ln2 is split into two floating point number:
  57. // ln2_hi + ln2_lo,
  58. // where n*ln2_hi is always exact for |n| < 2000.
  59. //
  60. // Special cases:
  61. // log1p(x) is NaN with signal if x < -1 (including -INF) ;
  62. // log1p(+INF) is +INF; log1p(-1) is -INF with signal;
  63. // log1p(NaN) is that NaN with no signal.
  64. //
  65. // Accuracy:
  66. // according to an error analysis, the error is always less than
  67. // 1 ulp (unit in the last place).
  68. //
  69. // Constants:
  70. // The hexadecimal values are the intended ones for the following
  71. // constants. The decimal values may be used, provided that the
  72. // compiler will convert from decimal to binary accurately enough
  73. // to produce the hexadecimal values shown.
  74. //
  75. // Note: Assuming log() return accurate answer, the following
  76. // algorithm can be used to compute log1p(x) to within a few ULP:
  77. //
  78. // u = 1+x;
  79. // if(u==1.0) return x ; else
  80. // return log(u)*(x/(u-1.0));
  81. //
  82. // See HP-15C Advanced Functions Handbook, p.193.
  83. // Log1p returns the natural logarithm of 1 plus its argument x.
  84. // It is more accurate than Log(1 + x) when x is near zero.
  85. //
  86. // Special cases are:
  87. // Log1p(+Inf) = +Inf
  88. // Log1p(±0) = ±0
  89. // Log1p(-1) = -Inf
  90. // Log1p(x < -1) = NaN
  91. // Log1p(NaN) = NaN
  92. //extern log1p
  93. func libc_log1p(float64) float64
  94. func Log1p(x float64) float64 {
  95. if x == 0 {
  96. return x
  97. }
  98. return libc_log1p(x)
  99. }
  100. func log1p(x float64) float64 {
  101. const (
  102. Sqrt2M1 = 4.142135623730950488017e-01 // Sqrt(2)-1 = 0x3fda827999fcef34
  103. Sqrt2HalfM1 = -2.928932188134524755992e-01 // Sqrt(2)/2-1 = 0xbfd2bec333018866
  104. Small = 1.0 / (1 << 29) // 2**-29 = 0x3e20000000000000
  105. Tiny = 1.0 / (1 << 54) // 2**-54
  106. Two53 = 1 << 53 // 2**53
  107. Ln2Hi = 6.93147180369123816490e-01 // 3fe62e42fee00000
  108. Ln2Lo = 1.90821492927058770002e-10 // 3dea39ef35793c76
  109. Lp1 = 6.666666666666735130e-01 // 3FE5555555555593
  110. Lp2 = 3.999999999940941908e-01 // 3FD999999997FA04
  111. Lp3 = 2.857142874366239149e-01 // 3FD2492494229359
  112. Lp4 = 2.222219843214978396e-01 // 3FCC71C51D8E78AF
  113. Lp5 = 1.818357216161805012e-01 // 3FC7466496CB03DE
  114. Lp6 = 1.531383769920937332e-01 // 3FC39A09D078C69F
  115. Lp7 = 1.479819860511658591e-01 // 3FC2F112DF3E5244
  116. )
  117. // special cases
  118. switch {
  119. case x < -1 || IsNaN(x): // includes -Inf
  120. return NaN()
  121. case x == -1:
  122. return Inf(-1)
  123. case IsInf(x, 1):
  124. return Inf(1)
  125. }
  126. absx := Abs(x)
  127. var f float64
  128. var iu uint64
  129. k := 1
  130. if absx < Sqrt2M1 { // |x| < Sqrt(2)-1
  131. if absx < Small { // |x| < 2**-29
  132. if absx < Tiny { // |x| < 2**-54
  133. return x
  134. }
  135. return x - x*x*0.5
  136. }
  137. if x > Sqrt2HalfM1 { // Sqrt(2)/2-1 < x
  138. // (Sqrt(2)/2-1) < x < (Sqrt(2)-1)
  139. k = 0
  140. f = x
  141. iu = 1
  142. }
  143. }
  144. var c float64
  145. if k != 0 {
  146. var u float64
  147. if absx < Two53 { // 1<<53
  148. u = 1.0 + x
  149. iu = Float64bits(u)
  150. k = int((iu >> 52) - 1023)
  151. // correction term
  152. if k > 0 {
  153. c = 1.0 - (u - x)
  154. } else {
  155. c = x - (u - 1.0)
  156. }
  157. c /= u
  158. } else {
  159. u = x
  160. iu = Float64bits(u)
  161. k = int((iu >> 52) - 1023)
  162. c = 0
  163. }
  164. iu &= 0x000fffffffffffff
  165. if iu < 0x0006a09e667f3bcd { // mantissa of Sqrt(2)
  166. u = Float64frombits(iu | 0x3ff0000000000000) // normalize u
  167. } else {
  168. k++
  169. u = Float64frombits(iu | 0x3fe0000000000000) // normalize u/2
  170. iu = (0x0010000000000000 - iu) >> 2
  171. }
  172. f = u - 1.0 // Sqrt(2)/2 < u < Sqrt(2)
  173. }
  174. hfsq := 0.5 * f * f
  175. var s, R, z float64
  176. if iu == 0 { // |f| < 2**-20
  177. if f == 0 {
  178. if k == 0 {
  179. return 0
  180. }
  181. c += float64(k) * Ln2Lo
  182. return float64(k)*Ln2Hi + c
  183. }
  184. R = hfsq * (1.0 - 0.66666666666666666*f) // avoid division
  185. if k == 0 {
  186. return f - R
  187. }
  188. return float64(k)*Ln2Hi - ((R - (float64(k)*Ln2Lo + c)) - f)
  189. }
  190. s = f / (2.0 + f)
  191. z = s * s
  192. R = z * (Lp1 + z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7))))))
  193. if k == 0 {
  194. return f - (hfsq - s*(hfsq+R))
  195. }
  196. return float64(k)*Ln2Hi - ((hfsq - (s*(hfsq+R) + (float64(k)*Ln2Lo + c))) - f)
  197. }