Source file
src/math/erf.go
1
2
3
4
5 package math
6
7
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 const (
120 erx = 8.45062911510467529297e-01
121
122 efx = 1.28379167095512586316e-01
123 efx8 = 1.02703333676410069053e+00
124 pp0 = 1.28379167095512558561e-01
125 pp1 = -3.25042107247001499370e-01
126 pp2 = -2.84817495755985104766e-02
127 pp3 = -5.77027029648944159157e-03
128 pp4 = -2.37630166566501626084e-05
129 qq1 = 3.97917223959155352819e-01
130 qq2 = 6.50222499887672944485e-02
131 qq3 = 5.08130628187576562776e-03
132 qq4 = 1.32494738004321644526e-04
133 qq5 = -3.96022827877536812320e-06
134
135 pa0 = -2.36211856075265944077e-03
136 pa1 = 4.14856118683748331666e-01
137 pa2 = -3.72207876035701323847e-01
138 pa3 = 3.18346619901161753674e-01
139 pa4 = -1.10894694282396677476e-01
140 pa5 = 3.54783043256182359371e-02
141 pa6 = -2.16637559486879084300e-03
142 qa1 = 1.06420880400844228286e-01
143 qa2 = 5.40397917702171048937e-01
144 qa3 = 7.18286544141962662868e-02
145 qa4 = 1.26171219808761642112e-01
146 qa5 = 1.36370839120290507362e-02
147 qa6 = 1.19844998467991074170e-02
148
149 ra0 = -9.86494403484714822705e-03
150 ra1 = -6.93858572707181764372e-01
151 ra2 = -1.05586262253232909814e+01
152 ra3 = -6.23753324503260060396e+01
153 ra4 = -1.62396669462573470355e+02
154 ra5 = -1.84605092906711035994e+02
155 ra6 = -8.12874355063065934246e+01
156 ra7 = -9.81432934416914548592e+00
157 sa1 = 1.96512716674392571292e+01
158 sa2 = 1.37657754143519042600e+02
159 sa3 = 4.34565877475229228821e+02
160 sa4 = 6.45387271733267880336e+02
161 sa5 = 4.29008140027567833386e+02
162 sa6 = 1.08635005541779435134e+02
163 sa7 = 6.57024977031928170135e+00
164 sa8 = -6.04244152148580987438e-02
165
166 rb0 = -9.86494292470009928597e-03
167 rb1 = -7.99283237680523006574e-01
168 rb2 = -1.77579549177547519889e+01
169 rb3 = -1.60636384855821916062e+02
170 rb4 = -6.37566443368389627722e+02
171 rb5 = -1.02509513161107724954e+03
172 rb6 = -4.83519191608651397019e+02
173 sb1 = 3.03380607434824582924e+01
174 sb2 = 3.25792512996573918826e+02
175 sb3 = 1.53672958608443695994e+03
176 sb4 = 3.19985821950859553908e+03
177 sb5 = 2.55305040643316442583e+03
178 sb6 = 4.74528541206955367215e+02
179 sb7 = -2.24409524465858183362e+01
180 )
181
182
183
184
185
186
187
188
189 func Erf(x float64) float64 {
190 if haveArchErf {
191 return archErf(x)
192 }
193 return erf(x)
194 }
195
196 func erf(x float64) float64 {
197 const (
198 VeryTiny = 2.848094538889218e-306
199 Small = 1.0 / (1 << 28)
200 )
201
202 switch {
203 case IsNaN(x):
204 return NaN()
205 case IsInf(x, 1):
206 return 1
207 case IsInf(x, -1):
208 return -1
209 }
210 sign := false
211 if x < 0 {
212 x = -x
213 sign = true
214 }
215 if x < 0.84375 {
216 var temp float64
217 if x < Small {
218 if x < VeryTiny {
219 temp = 0.125 * (8.0*x + efx8*x)
220 } else {
221 temp = x + efx*x
222 }
223 } else {
224 z := x * x
225 r := pp0 + z*(pp1+z*(pp2+z*(pp3+z*pp4)))
226 s := 1 + z*(qq1+z*(qq2+z*(qq3+z*(qq4+z*qq5))))
227 y := r / s
228 temp = x + x*y
229 }
230 if sign {
231 return -temp
232 }
233 return temp
234 }
235 if x < 1.25 {
236 s := x - 1
237 P := pa0 + s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6)))))
238 Q := 1 + s*(qa1+s*(qa2+s*(qa3+s*(qa4+s*(qa5+s*qa6)))))
239 if sign {
240 return -erx - P/Q
241 }
242 return erx + P/Q
243 }
244 if x >= 6 {
245 if sign {
246 return -1
247 }
248 return 1
249 }
250 s := 1 / (x * x)
251 var R, S float64
252 if x < 1/0.35 {
253 R = ra0 + s*(ra1+s*(ra2+s*(ra3+s*(ra4+s*(ra5+s*(ra6+s*ra7))))))
254 S = 1 + s*(sa1+s*(sa2+s*(sa3+s*(sa4+s*(sa5+s*(sa6+s*(sa7+s*sa8)))))))
255 } else {
256 R = rb0 + s*(rb1+s*(rb2+s*(rb3+s*(rb4+s*(rb5+s*rb6)))))
257 S = 1 + s*(sb1+s*(sb2+s*(sb3+s*(sb4+s*(sb5+s*(sb6+s*sb7))))))
258 }
259 z := Float64frombits(Float64bits(x) & 0xffffffff00000000)
260 r := Exp(-z*z-0.5625) * Exp((z-x)*(z+x)+R/S)
261 if sign {
262 return r/x - 1
263 }
264 return 1 - r/x
265 }
266
267
268
269
270
271
272
273
274 func Erfc(x float64) float64 {
275 if haveArchErfc {
276 return archErfc(x)
277 }
278 return erfc(x)
279 }
280
281 func erfc(x float64) float64 {
282 const Tiny = 1.0 / (1 << 56)
283
284 switch {
285 case IsNaN(x):
286 return NaN()
287 case IsInf(x, 1):
288 return 0
289 case IsInf(x, -1):
290 return 2
291 }
292 sign := false
293 if x < 0 {
294 x = -x
295 sign = true
296 }
297 if x < 0.84375 {
298 var temp float64
299 if x < Tiny {
300 temp = x
301 } else {
302 z := x * x
303 r := pp0 + z*(pp1+z*(pp2+z*(pp3+z*pp4)))
304 s := 1 + z*(qq1+z*(qq2+z*(qq3+z*(qq4+z*qq5))))
305 y := r / s
306 if x < 0.25 {
307 temp = x + x*y
308 } else {
309 temp = 0.5 + (x*y + (x - 0.5))
310 }
311 }
312 if sign {
313 return 1 + temp
314 }
315 return 1 - temp
316 }
317 if x < 1.25 {
318 s := x - 1
319 P := pa0 + s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6)))))
320 Q := 1 + s*(qa1+s*(qa2+s*(qa3+s*(qa4+s*(qa5+s*qa6)))))
321 if sign {
322 return 1 + erx + P/Q
323 }
324 return 1 - erx - P/Q
325
326 }
327 if x < 28 {
328 s := 1 / (x * x)
329 var R, S float64
330 if x < 1/0.35 {
331 R = ra0 + s*(ra1+s*(ra2+s*(ra3+s*(ra4+s*(ra5+s*(ra6+s*ra7))))))
332 S = 1 + s*(sa1+s*(sa2+s*(sa3+s*(sa4+s*(sa5+s*(sa6+s*(sa7+s*sa8)))))))
333 } else {
334 if sign && x > 6 {
335 return 2
336 }
337 R = rb0 + s*(rb1+s*(rb2+s*(rb3+s*(rb4+s*(rb5+s*rb6)))))
338 S = 1 + s*(sb1+s*(sb2+s*(sb3+s*(sb4+s*(sb5+s*(sb6+s*sb7))))))
339 }
340 z := Float64frombits(Float64bits(x) & 0xffffffff00000000)
341 r := Exp(-z*z-0.5625) * Exp((z-x)*(z+x)+R/S)
342 if sign {
343 return 2 - r/x
344 }
345 return r / x
346 }
347 if sign {
348 return 2
349 }
350 return 0
351 }
352
View as plain text