v / vlib / math
Raw file | 327 loc (324 sloc) | 11.68 KB | Latest commit hash 9e68a03f9
1module math
2
3/*
4* x
5 * 2 |\
6 * erf(x) = --------- | exp(-t*t)dt
7 * sqrt(pi) \|
8 * 0
9 *
10 * erfc(x) = 1-erf(x)
11 * Note that
12 * erf(-x) = -erf(x)
13 * erfc(-x) = 2 - erfc(x)
14 *
15 * Method:
16 * 1. For |x| in [0, 0.84375]
17 * erf(x) = x + x*R(x**2)
18 * erfc(x) = 1 - erf(x) if x in [-.84375,0.25]
19 * = 0.5 + ((0.5-x)-x*R) if x in [0.25,0.84375]
20 * where R = P/Q where P is an odd poly of degree 8 and
21 * Q is an odd poly of degree 10.
22 * -57.90
23 * | R - (erf(x)-x)/x | <= 2
24 *
25 *
26 * Remark. The formula is derived by noting
27 * erf(x) = (2/sqrt(pi))*(x - x**3/3 + x**5/10 - x**7/42 + ....)
28 * and that
29 * 2/sqrt(pi) = 1.128379167095512573896158903121545171688
30 * is close to one. The interval is chosen because the fix
31 * point of erf(x) is near 0.6174 (i.e., erf(x)=x when x is
32 * near 0.6174), and by some experiment, 0.84375 is chosen to
33 * guarantee the error is less than one ulp for erf.
34 *
35 * 2. For |x| in [0.84375,1.25], let s_ = |x| - 1, and
36 * c = 0.84506291151 rounded to single (24 bits)
37 * erf(x) = sign(x) * (c + P1(s_)/Q1(s_))
38 * erfc(x) = (1-c) - P1(s_)/Q1(s_) if x > 0
39 * 1+(c+P1(s_)/Q1(s_)) if x < 0
40 * |P1/Q1 - (erf(|x|)-c)| <= 2**-59.06
41 * Remark: here we use the taylor series expansion at x=1.
42 * erf(1+s_) = erf(1) + s_*Poly(s_)
43 * = 0.845.. + P1(s_)/Q1(s_)
44 * That is, we use rational approximation to approximate
45 * erf(1+s_) - (c = (single)0.84506291151)
46 * Note that |P1/Q1|< 0.078 for x in [0.84375,1.25]
47 * where
48 * P1(s_) = degree 6 poly in s_
49 * Q1(s_) = degree 6 poly in s_
50 *
51 * 3. For x in [1.25,1/0.35(~2.857143)],
52 * erfc(x) = (1/x)*exp(-x*x-0.5625+R1/s1)
53 * erf(x) = 1 - erfc(x)
54 * where
55 * R1(z) = degree 7 poly in z, (z=1/x**2)
56 * s1(z) = degree 8 poly in z
57 *
58 * 4. For x in [1/0.35,28]
59 * erfc(x) = (1/x)*exp(-x*x-0.5625+R2/s2) if x > 0
60 * = 2.0 - (1/x)*exp(-x*x-0.5625+R2/s2) if -6<x<0
61 * = 2.0 - tiny (if x <= -6)
62 * erf(x) = sign(x)*(1.0 - erfc(x)) if x < 6, else
63 * erf(x) = sign(x)*(1.0 - tiny)
64 * where
65 * R2(z) = degree 6 poly in z, (z=1/x**2)
66 * s2(z) = degree 7 poly in z
67 *
68 * Note1:
69 * To compute exp(-x*x-0.5625+R/s), let s_ be a single
70 * precision number and s_ := x; then
71 * -x*x = -s_*s_ + (s_-x)*(s_+x)
72 * exp(-x*x-0.5626+R/s) =
73 * exp(-s_*s_-0.5625)*exp((s_-x)*(s_+x)+R/s);
74 * Note2:
75 * Here 4 and 5 make use of the asymptotic series
76 * exp(-x*x)
77 * erfc(x) ~ ---------- * ( 1 + Poly(1/x**2) )
78 * x*sqrt(pi)
79 * We use rational approximation to approximate
80 * g(s_)=f(1/x**2) = log(erfc(x)*x) - x*x + 0.5625
81 * Here is the error bound for R1/s1 and R2/s2
82 * |R1/s1 - f(x)| < 2**(-62.57)
83 * |R2/s2 - f(x)| < 2**(-61.52)
84 *
85 * 5. For inf > x >= 28
86 * erf(x) = sign(x) *(1 - tiny) (raise inexact)
87 * erfc(x) = tiny*tiny (raise underflow) if x > 0
88 * = 2 - tiny if x<0
89 *
90 * 7. special case:
91 * erf(0) = 0, erf(inf) = 1, erf(-inf) = -1,
92 * erfc(0) = 1, erfc(inf) = 0, erfc(-inf) = 2,
93 * erfc/erf(nan) is nan
94*/
95const (
96 erx = 8.45062911510467529297e-01 // 0x3FEB0AC160000000
97 // Coefficients for approximation to erf in [0, 0.84375]
98 efx = 1.28379167095512586316e-01 // 0x3FC06EBA8214DB69
99 efx8 = 1.02703333676410069053e+00 // 0x3FF06EBA8214DB69
100 pp0 = 1.28379167095512558561e-01 // 0x3FC06EBA8214DB68
101 pp1 = -3.25042107247001499370e-01 // 0xBFD4CD7D691CB913
102 pp2 = -2.84817495755985104766e-02 // 0xBF9D2A51DBD7194F
103 pp3 = -5.77027029648944159157e-03 // 0xBF77A291236668E4
104 pp4 = -2.37630166566501626084e-05 // 0xBEF8EAD6120016AC
105 qq1 = 3.97917223959155352819e-01 // 0x3FD97779CDDADC09
106 qq2 = 6.50222499887672944485e-02 // 0x3FB0A54C5536CEBA
107 qq3 = 5.08130628187576562776e-03 // 0x3F74D022C4D36B0F
108 qq4 = 1.32494738004321644526e-04 // 0x3F215DC9221C1A10
109 qq5 = -3.96022827877536812320e-06 // 0xBED09C4342A26120
110 // Coefficients for approximation to erf in [0.84375, 1.25]
111 pa0 = -2.36211856075265944077e-03 // 0xBF6359B8BEF77538
112 pa1 = 4.14856118683748331666e-01 // 0x3FDA8D00AD92B34D
113 pa2 = -3.72207876035701323847e-01 // 0xBFD7D240FBB8C3F1
114 pa3 = 3.18346619901161753674e-01 // 0x3FD45FCA805120E4
115 pa4 = -1.10894694282396677476e-01 // 0xBFBC63983D3E28EC
116 pa5 = 3.54783043256182359371e-02 // 0x3FA22A36599795EB
117 pa6 = -2.16637559486879084300e-03 // 0xBF61BF380A96073F
118 qa1 = 1.06420880400844228286e-01 // 0x3FBB3E6618EEE323
119 qa2 = 5.40397917702171048937e-01 // 0x3FE14AF092EB6F33
120 qa3 = 7.18286544141962662868e-02 // 0x3FB2635CD99FE9A7
121 qa4 = 1.26171219808761642112e-01 // 0x3FC02660E763351F
122 qa5 = 1.36370839120290507362e-02 // 0x3F8BEDC26B51DD1C
123 qa6 = 1.19844998467991074170e-02 // 0x3F888B545735151D
124 // Coefficients for approximation to erfc in [1.25, 1/0.35]
125 ra0 = -9.86494403484714822705e-03 // 0xBF843412600D6435
126 ra1 = -6.93858572707181764372e-01 // 0xBFE63416E4BA7360
127 ra2 = -1.05586262253232909814e+01 // 0xC0251E0441B0E726
128 ra3 = -6.23753324503260060396e+01 // 0xC04F300AE4CBA38D
129 ra4 = -1.62396669462573470355e+02 // 0xC0644CB184282266
130 ra5 = -1.84605092906711035994e+02 // 0xC067135CEBCCABB2
131 ra6 = -8.12874355063065934246e+01 // 0xC054526557E4D2F2
132 ra7 = -9.81432934416914548592e+00 // 0xC023A0EFC69AC25C
133 sa1 = 1.96512716674392571292e+01 // 0x4033A6B9BD707687
134 sa2 = 1.37657754143519042600e+02 // 0x4061350C526AE721
135 sa3 = 4.34565877475229228821e+02 // 0x407B290DD58A1A71
136 sa4 = 6.45387271733267880336e+02 // 0x40842B1921EC2868
137 sa5 = 4.29008140027567833386e+02 // 0x407AD02157700314
138 sa6 = 1.08635005541779435134e+02 // 0x405B28A3EE48AE2C
139 sa7 = 6.57024977031928170135e+00 // 0x401A47EF8E484A93
140 sa8 = -6.04244152148580987438e-02 // 0xBFAEEFF2EE749A62
141 // Coefficients for approximation to erfc in [1/.35, 28]
142 rb0 = -9.86494292470009928597e-03 // 0xBF84341239E86F4A
143 rb1 = -7.99283237680523006574e-01 // 0xBFE993BA70C285DE
144 rb2 = -1.77579549177547519889e+01 // 0xC031C209555F995A
145 rb3 = -1.60636384855821916062e+02 // 0xC064145D43C5ED98
146 rb4 = -6.37566443368389627722e+02 // 0xC083EC881375F228
147 rb5 = -1.02509513161107724954e+03 // 0xC09004616A2E5992
148 rb6 = -4.83519191608651397019e+02 // 0xC07E384E9BDC383F
149 sb1 = 3.03380607434824582924e+01 // 0x403E568B261D5190
150 sb2 = 3.25792512996573918826e+02 // 0x40745CAE221B9F0A
151 sb3 = 1.53672958608443695994e+03 // 0x409802EB189D5118
152 sb4 = 3.19985821950859553908e+03 // 0x40A8FFB7688C246A
153 sb5 = 2.55305040643316442583e+03 // 0x40A3F219CEDF3BE6
154 sb6 = 4.74528541206955367215e+02 // 0x407DA874E79FE763
155 sb7 = -2.24409524465858183362e+01 // 0xC03670E242712D62
156)
157
158// erf returns the error function of x.
159//
160// special cases are:
161// erf(+inf) = 1
162// erf(-inf) = -1
163// erf(nan) = nan
164pub fn erf(a f64) f64 {
165 mut x := a
166 very_tiny := 2.848094538889218e-306 // 0x0080000000000000
167 small_ := 1.0 / f64(u64(1) << 28) // 2**-28
168 if is_nan(x) {
169 return nan()
170 }
171 if is_inf(x, 1) {
172 return 1.0
173 }
174 if is_inf(x, -1) {
175 return f64(-1)
176 }
177 mut sign := false
178 if x < 0 {
179 x = -x
180 sign = true
181 }
182 if x < 0.84375 { // |x| < 0.84375
183 mut temp := 0.0
184 if x < small_ { // |x| < 2**-28
185 if x < very_tiny {
186 temp = 0.125 * (8.0 * x + math.efx8 * x) // avoid underflow
187 } else {
188 temp = x + math.efx * x
189 }
190 } else {
191 z := x * x
192 r := math.pp0 + z * (math.pp1 + z * (math.pp2 + z * (math.pp3 + z * math.pp4)))
193 s_ := 1.0 + z * (math.qq1 + z * (math.qq2 + z * (math.qq3 + z * (math.qq4 +
194 z * math.qq5))))
195 y := r / s_
196 temp = x + x * y
197 }
198 if sign {
199 return -temp
200 }
201 return temp
202 }
203 if x < 1.25 { // 0.84375 <= |x| < 1.25
204 s_ := x - 1
205 p := math.pa0 + s_ * (math.pa1 + s_ * (math.pa2 + s_ * (math.pa3 + s_ * (math.pa4 +
206 s_ * (math.pa5 + s_ * math.pa6)))))
207 q := 1.0 + s_ * (math.qa1 + s_ * (math.qa2 + s_ * (math.qa3 + s_ * (math.qa4 +
208 s_ * (math.qa5 + s_ * math.qa6)))))
209 if sign {
210 return -math.erx - p / q
211 }
212 return math.erx + p / q
213 }
214 if x >= 6 { // inf > |x| >= 6
215 if sign {
216 return -1
217 }
218 return 1.0
219 }
220 s_ := 1.0 / (x * x)
221 mut r := 0.0
222 mut s := 0.0
223 if x < 1.0 / 0.35 { // |x| < 1 / 0.35 ~ 2.857143
224 r = math.ra0 + s_ * (math.ra1 + s_ * (math.ra2 + s_ * (math.ra3 + s_ * (math.ra4 +
225 s_ * (math.ra5 + s_ * (math.ra6 + s_ * math.ra7))))))
226 s = 1.0 + s_ * (math.sa1 + s_ * (math.sa2 + s_ * (math.sa3 + s_ * (math.sa4 +
227 s_ * (math.sa5 + s_ * (math.sa6 + s_ * (math.sa7 + s_ * math.sa8)))))))
228 } else { // |x| >= 1 / 0.35 ~ 2.857143
229 r = math.rb0 + s_ * (math.rb1 + s_ * (math.rb2 + s_ * (math.rb3 + s_ * (math.rb4 +
230 s_ * (math.rb5 + s_ * math.rb6)))))
231 s = 1.0 + s_ * (math.sb1 + s_ * (math.sb2 + s_ * (math.sb3 + s_ * (math.sb4 +
232 s_ * (math.sb5 + s_ * (math.sb6 + s_ * math.sb7))))))
233 }
234 z := f64_from_bits(f64_bits(x) & 0xffffffff00000000) // pseudo-single (20-bit) precision x
235 r_ := exp(-z * z - 0.5625) * exp((z - x) * (z + x) + r / s)
236 if sign {
237 return r_ / x - 1.0
238 }
239 return 1.0 - r_ / x
240}
241
242// erfc returns the complementary error function of x.
243//
244// special cases are:
245// erfc(+inf) = 0
246// erfc(-inf) = 2
247// erfc(nan) = nan
248pub fn erfc(a f64) f64 {
249 mut x := a
250 tiny := 1.0 / f64(u64(1) << 56) // 2**-56
251 // special cases
252 if is_nan(x) {
253 return nan()
254 }
255 if is_inf(x, 1) {
256 return 0.0
257 }
258 if is_inf(x, -1) {
259 return 2.0
260 }
261 mut sign := false
262 if x < 0 {
263 x = -x
264 sign = true
265 }
266 if x < 0.84375 { // |x| < 0.84375
267 mut temp := 0.0
268 if x < tiny { // |x| < 2**-56
269 temp = x
270 } else {
271 z := x * x
272 r := math.pp0 + z * (math.pp1 + z * (math.pp2 + z * (math.pp3 + z * math.pp4)))
273 s_ := 1.0 + z * (math.qq1 + z * (math.qq2 + z * (math.qq3 + z * (math.qq4 +
274 z * math.qq5))))
275 y := r / s_
276 if x < 0.25 { // |x| < 1.0/4
277 temp = x + x * y
278 } else {
279 temp = 0.5 + (x * y + (x - 0.5))
280 }
281 }
282 if sign {
283 return 1.0 + temp
284 }
285 return 1.0 - temp
286 }
287 if x < 1.25 { // 0.84375 <= |x| < 1.25
288 s_ := x - 1
289 p := math.pa0 + s_ * (math.pa1 + s_ * (math.pa2 + s_ * (math.pa3 + s_ * (math.pa4 +
290 s_ * (math.pa5 + s_ * math.pa6)))))
291 q := 1.0 + s_ * (math.qa1 + s_ * (math.qa2 + s_ * (math.qa3 + s_ * (math.qa4 +
292 s_ * (math.qa5 + s_ * math.qa6)))))
293 if sign {
294 return 1.0 + math.erx + p / q
295 }
296 return 1.0 - math.erx - p / q
297 }
298 if x < 28 { // |x| < 28
299 s_ := 1.0 / (x * x)
300 mut r := 0.0
301 mut s := 0.0
302 if x < 1.0 / 0.35 { // |x| < 1 / 0.35 ~ 2.857143
303 r = math.ra0 + s_ * (math.ra1 + s_ * (math.ra2 + s_ * (math.ra3 + s_ * (math.ra4 +
304 s_ * (math.ra5 + s_ * (math.ra6 + s_ * math.ra7))))))
305 s = 1.0 + s_ * (math.sa1 + s_ * (math.sa2 + s_ * (math.sa3 + s_ * (math.sa4 +
306 s_ * (math.sa5 + s_ * (math.sa6 + s_ * (math.sa7 + s_ * math.sa8)))))))
307 } else { // |x| >= 1 / 0.35 ~ 2.857143
308 if sign && x > 6 {
309 return 2.0 // x < -6
310 }
311 r = math.rb0 + s_ * (math.rb1 + s_ * (math.rb2 + s_ * (math.rb3 + s_ * (math.rb4 +
312 s_ * (math.rb5 + s_ * math.rb6)))))
313 s = 1.0 + s_ * (math.sb1 + s_ * (math.sb2 + s_ * (math.sb3 + s_ * (math.sb4 +
314 s_ * (math.sb5 + s_ * (math.sb6 + s_ * math.sb7))))))
315 }
316 z := f64_from_bits(f64_bits(x) & 0xffffffff00000000) // pseudo-single (20-bit) precision x
317 r_ := exp(-z * z - 0.5625) * exp((z - x) * (z + x) + r / s)
318 if sign {
319 return 2.0 - r_ / x
320 }
321 return r_ / x
322 }
323 if sign {
324 return 2.0
325 }
326 return 0.0
327}