expm1.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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_expm1.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. // expm1(x)
  20. // Returns exp(x)-1, the exponential of x minus 1.
  21. //
  22. // Method
  23. // 1. Argument reduction:
  24. // Given x, find r and integer k such that
  25. //
  26. // x = k*ln2 + r, |r| <= 0.5*ln2 ~ 0.34658
  27. //
  28. // Here a correction term c will be computed to compensate
  29. // the error in r when rounded to a floating-point number.
  30. //
  31. // 2. Approximating expm1(r) by a special rational function on
  32. // the interval [0,0.34658]:
  33. // Since
  34. // r*(exp(r)+1)/(exp(r)-1) = 2+ r**2/6 - r**4/360 + ...
  35. // we define R1(r*r) by
  36. // r*(exp(r)+1)/(exp(r)-1) = 2+ r**2/6 * R1(r*r)
  37. // That is,
  38. // R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
  39. // = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
  40. // = 1 - r**2/60 + r**4/2520 - r**6/100800 + ...
  41. // We use a special Reme algorithm on [0,0.347] to generate
  42. // a polynomial of degree 5 in r*r to approximate R1. The
  43. // maximum error of this polynomial approximation is bounded
  44. // by 2**-61. In other words,
  45. // R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
  46. // where Q1 = -1.6666666666666567384E-2,
  47. // Q2 = 3.9682539681370365873E-4,
  48. // Q3 = -9.9206344733435987357E-6,
  49. // Q4 = 2.5051361420808517002E-7,
  50. // Q5 = -6.2843505682382617102E-9;
  51. // (where z=r*r, and the values of Q1 to Q5 are listed below)
  52. // with error bounded by
  53. // | 5 | -61
  54. // | 1.0+Q1*z+...+Q5*z - R1(z) | <= 2
  55. // | |
  56. //
  57. // expm1(r) = exp(r)-1 is then computed by the following
  58. // specific way which minimize the accumulation rounding error:
  59. // 2 3
  60. // r r [ 3 - (R1 + R1*r/2) ]
  61. // expm1(r) = r + --- + --- * [--------------------]
  62. // 2 2 [ 6 - r*(3 - R1*r/2) ]
  63. //
  64. // To compensate the error in the argument reduction, we use
  65. // expm1(r+c) = expm1(r) + c + expm1(r)*c
  66. // ~ expm1(r) + c + r*c
  67. // Thus c+r*c will be added in as the correction terms for
  68. // expm1(r+c). Now rearrange the term to avoid optimization
  69. // screw up:
  70. // ( 2 2 )
  71. // ({ ( r [ R1 - (3 - R1*r/2) ] ) } r )
  72. // expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
  73. // ({ ( 2 [ 6 - r*(3 - R1*r/2) ] ) } 2 )
  74. // ( )
  75. //
  76. // = r - E
  77. // 3. Scale back to obtain expm1(x):
  78. // From step 1, we have
  79. // expm1(x) = either 2**k*[expm1(r)+1] - 1
  80. // = or 2**k*[expm1(r) + (1-2**-k)]
  81. // 4. Implementation notes:
  82. // (A). To save one multiplication, we scale the coefficient Qi
  83. // to Qi*2**i, and replace z by (x**2)/2.
  84. // (B). To achieve maximum accuracy, we compute expm1(x) by
  85. // (i) if x < -56*ln2, return -1.0, (raise inexact if x!=inf)
  86. // (ii) if k=0, return r-E
  87. // (iii) if k=-1, return 0.5*(r-E)-0.5
  88. // (iv) if k=1 if r < -0.25, return 2*((r+0.5)- E)
  89. // else return 1.0+2.0*(r-E);
  90. // (v) if (k<-2||k>56) return 2**k(1-(E-r)) - 1 (or exp(x)-1)
  91. // (vi) if k <= 20, return 2**k((1-2**-k)-(E-r)), else
  92. // (vii) return 2**k(1-((E+2**-k)-r))
  93. //
  94. // Special cases:
  95. // expm1(INF) is INF, expm1(NaN) is NaN;
  96. // expm1(-INF) is -1, and
  97. // for finite argument, only expm1(0)=0 is exact.
  98. //
  99. // Accuracy:
  100. // according to an error analysis, the error is always less than
  101. // 1 ulp (unit in the last place).
  102. //
  103. // Misc. info.
  104. // For IEEE double
  105. // if x > 7.09782712893383973096e+02 then expm1(x) overflow
  106. //
  107. // Constants:
  108. // The hexadecimal values are the intended ones for the following
  109. // constants. The decimal values may be used, provided that the
  110. // compiler will convert from decimal to binary accurately enough
  111. // to produce the hexadecimal values shown.
  112. //
  113. // Expm1 returns e**x - 1, the base-e exponential of x minus 1.
  114. // It is more accurate than Exp(x) - 1 when x is near zero.
  115. //
  116. // Special cases are:
  117. // Expm1(+Inf) = +Inf
  118. // Expm1(-Inf) = -1
  119. // Expm1(NaN) = NaN
  120. // Very large values overflow to -1 or +Inf.
  121. //extern expm1
  122. func libc_expm1(float64) float64
  123. func Expm1(x float64) float64 {
  124. return libc_expm1(x)
  125. }
  126. func expm1(x float64) float64 {
  127. const (
  128. Othreshold = 7.09782712893383973096e+02 // 0x40862E42FEFA39EF
  129. Ln2X56 = 3.88162421113569373274e+01 // 0x4043687a9f1af2b1
  130. Ln2HalfX3 = 1.03972077083991796413e+00 // 0x3ff0a2b23f3bab73
  131. Ln2Half = 3.46573590279972654709e-01 // 0x3fd62e42fefa39ef
  132. Ln2Hi = 6.93147180369123816490e-01 // 0x3fe62e42fee00000
  133. Ln2Lo = 1.90821492927058770002e-10 // 0x3dea39ef35793c76
  134. InvLn2 = 1.44269504088896338700e+00 // 0x3ff71547652b82fe
  135. Tiny = 1.0 / (1 << 54) // 2**-54 = 0x3c90000000000000
  136. // scaled coefficients related to expm1
  137. Q1 = -3.33333333333331316428e-02 // 0xBFA11111111110F4
  138. Q2 = 1.58730158725481460165e-03 // 0x3F5A01A019FE5585
  139. Q3 = -7.93650757867487942473e-05 // 0xBF14CE199EAADBB7
  140. Q4 = 4.00821782732936239552e-06 // 0x3ED0CFCA86E65239
  141. Q5 = -2.01099218183624371326e-07 // 0xBE8AFDB76E09C32D
  142. )
  143. // special cases
  144. switch {
  145. case IsInf(x, 1) || IsNaN(x):
  146. return x
  147. case IsInf(x, -1):
  148. return -1
  149. }
  150. absx := x
  151. sign := false
  152. if x < 0 {
  153. absx = -absx
  154. sign = true
  155. }
  156. // filter out huge argument
  157. if absx >= Ln2X56 { // if |x| >= 56 * ln2
  158. if absx >= Othreshold { // if |x| >= 709.78...
  159. return Inf(1) // overflow
  160. }
  161. if sign {
  162. return -1 // x < -56*ln2, return -1.0
  163. }
  164. }
  165. // argument reduction
  166. var c float64
  167. var k int
  168. if absx > Ln2Half { // if |x| > 0.5 * ln2
  169. var hi, lo float64
  170. if absx < Ln2HalfX3 { // and |x| < 1.5 * ln2
  171. if !sign {
  172. hi = x - Ln2Hi
  173. lo = Ln2Lo
  174. k = 1
  175. } else {
  176. hi = x + Ln2Hi
  177. lo = -Ln2Lo
  178. k = -1
  179. }
  180. } else {
  181. if !sign {
  182. k = int(InvLn2*x + 0.5)
  183. } else {
  184. k = int(InvLn2*x - 0.5)
  185. }
  186. t := float64(k)
  187. hi = x - t*Ln2Hi // t * Ln2Hi is exact here
  188. lo = t * Ln2Lo
  189. }
  190. x = hi - lo
  191. c = (hi - x) - lo
  192. } else if absx < Tiny { // when |x| < 2**-54, return x
  193. return x
  194. } else {
  195. k = 0
  196. }
  197. // x is now in primary range
  198. hfx := 0.5 * x
  199. hxs := x * hfx
  200. r1 := 1 + hxs*(Q1+hxs*(Q2+hxs*(Q3+hxs*(Q4+hxs*Q5))))
  201. t := 3 - r1*hfx
  202. e := hxs * ((r1 - t) / (6.0 - x*t))
  203. if k != 0 {
  204. e = (x*(e-c) - c)
  205. e -= hxs
  206. switch {
  207. case k == -1:
  208. return 0.5*(x-e) - 0.5
  209. case k == 1:
  210. if x < -0.25 {
  211. return -2 * (e - (x + 0.5))
  212. }
  213. return 1 + 2*(x-e)
  214. case k <= -2 || k > 56: // suffice to return exp(x)-1
  215. y := 1 - (e - x)
  216. y = Float64frombits(Float64bits(y) + uint64(k)<<52) // add k to y's exponent
  217. return y - 1
  218. }
  219. if k < 20 {
  220. t := Float64frombits(0x3ff0000000000000 - (0x20000000000000 >> uint(k))) // t=1-2**-k
  221. y := t - (e - x)
  222. y = Float64frombits(Float64bits(y) + uint64(k)<<52) // add k to y's exponent
  223. return y
  224. }
  225. t := Float64frombits(uint64((0x3ff - k) << 52)) // 2**-k
  226. y := x - (e + t)
  227. y += 1
  228. y = Float64frombits(Float64bits(y) + uint64(k)<<52) // add k to y's exponent
  229. return y
  230. }
  231. return x - (x*e - hxs) // c is 0
  232. }