...
Source file
src/math/erf.go
Documentation: math
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 func Erf(x float64) float64 {
189 if haveArchErf {
190 return archErf(x)
191 }
192 return erf(x)
193 }
194
195 func erf(x float64) float64 {
196 const (
197 VeryTiny = 2.848094538889218e-306
198 Small = 1.0 / (1 << 28)
199 )
200
201 switch {
202 case IsNaN(x):
203 return NaN()
204 case IsInf(x, 1):
205 return 1
206 case IsInf(x, -1):
207 return -1
208 }
209 sign := false
210 if x < 0 {
211 x = -x
212 sign = true
213 }
214 if x < 0.84375 {
215 var temp float64
216 if x < Small {
217 if x < VeryTiny {
218 temp = 0.125 * (8.0*x + efx8*x)
219 } else {
220 temp = x + efx*x
221 }
222 } else {
223 z := x * x
224 r := pp0 + z*(pp1+z*(pp2+z*(pp3+z*pp4)))
225 s := 1 + z*(qq1+z*(qq2+z*(qq3+z*(qq4+z*qq5))))
226 y := r / s
227 temp = x + x*y
228 }
229 if sign {
230 return -temp
231 }
232 return temp
233 }
234 if x < 1.25 {
235 s := x - 1
236 P := pa0 + s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6)))))
237 Q := 1 + s*(qa1+s*(qa2+s*(qa3+s*(qa4+s*(qa5+s*qa6)))))
238 if sign {
239 return -erx - P/Q
240 }
241 return erx + P/Q
242 }
243 if x >= 6 {
244 if sign {
245 return -1
246 }
247 return 1
248 }
249 s := 1 / (x * x)
250 var R, S float64
251 if x < 1/0.35 {
252 R = ra0 + s*(ra1+s*(ra2+s*(ra3+s*(ra4+s*(ra5+s*(ra6+s*ra7))))))
253 S = 1 + s*(sa1+s*(sa2+s*(sa3+s*(sa4+s*(sa5+s*(sa6+s*(sa7+s*sa8)))))))
254 } else {
255 R = rb0 + s*(rb1+s*(rb2+s*(rb3+s*(rb4+s*(rb5+s*rb6)))))
256 S = 1 + s*(sb1+s*(sb2+s*(sb3+s*(sb4+s*(sb5+s*(sb6+s*sb7))))))
257 }
258 z := Float64frombits(Float64bits(x) & 0xffffffff00000000)
259 r := Exp(-z*z-0.5625) * Exp((z-x)*(z+x)+R/S)
260 if sign {
261 return r/x - 1
262 }
263 return 1 - r/x
264 }
265
266
267
268
269
270
271
272 func Erfc(x float64) float64 {
273 if haveArchErfc {
274 return archErfc(x)
275 }
276 return erfc(x)
277 }
278
279 func erfc(x float64) float64 {
280 const Tiny = 1.0 / (1 << 56)
281
282 switch {
283 case IsNaN(x):
284 return NaN()
285 case IsInf(x, 1):
286 return 0
287 case IsInf(x, -1):
288 return 2
289 }
290 sign := false
291 if x < 0 {
292 x = -x
293 sign = true
294 }
295 if x < 0.84375 {
296 var temp float64
297 if x < Tiny {
298 temp = x
299 } else {
300 z := x * x
301 r := pp0 + z*(pp1+z*(pp2+z*(pp3+z*pp4)))
302 s := 1 + z*(qq1+z*(qq2+z*(qq3+z*(qq4+z*qq5))))
303 y := r / s
304 if x < 0.25 {
305 temp = x + x*y
306 } else {
307 temp = 0.5 + (x*y + (x - 0.5))
308 }
309 }
310 if sign {
311 return 1 + temp
312 }
313 return 1 - temp
314 }
315 if x < 1.25 {
316 s := x - 1
317 P := pa0 + s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6)))))
318 Q := 1 + s*(qa1+s*(qa2+s*(qa3+s*(qa4+s*(qa5+s*qa6)))))
319 if sign {
320 return 1 + erx + P/Q
321 }
322 return 1 - erx - P/Q
323
324 }
325 if x < 28 {
326 s := 1 / (x * x)
327 var R, S float64
328 if x < 1/0.35 {
329 R = ra0 + s*(ra1+s*(ra2+s*(ra3+s*(ra4+s*(ra5+s*(ra6+s*ra7))))))
330 S = 1 + s*(sa1+s*(sa2+s*(sa3+s*(sa4+s*(sa5+s*(sa6+s*(sa7+s*sa8)))))))
331 } else {
332 if sign && x > 6 {
333 return 2
334 }
335 R = rb0 + s*(rb1+s*(rb2+s*(rb3+s*(rb4+s*(rb5+s*rb6)))))
336 S = 1 + s*(sb1+s*(sb2+s*(sb3+s*(sb4+s*(sb5+s*(sb6+s*sb7))))))
337 }
338 z := Float64frombits(Float64bits(x) & 0xffffffff00000000)
339 r := Exp(-z*z-0.5625) * Exp((z-x)*(z+x)+R/S)
340 if sign {
341 return 2 - r/x
342 }
343 return r / x
344 }
345 if sign {
346 return 2
347 }
348 return 0
349 }
350
View as plain text