1 |
greg |
3.1 |
#ifndef lint |
2 |
|
|
static const char RCSid[] = "$Id$"; |
3 |
|
|
#endif |
4 |
|
|
#ifndef lint |
5 |
|
|
static char sccsid[] = "@(#)erf.c 1.1 87/12/21 SMI"; /* from UCB 4.1 12/25/82 */ |
6 |
|
|
#endif |
7 |
|
|
|
8 |
|
|
/* |
9 |
|
|
C program for floating point error function |
10 |
|
|
|
11 |
|
|
erf(x) returns the error function of its argument |
12 |
|
|
erfc(x) returns 1.0-erf(x) |
13 |
|
|
|
14 |
|
|
erf(x) is defined by |
15 |
|
|
${2 over sqrt(pi)} int from 0 to x e sup {-t sup 2} dt$ |
16 |
|
|
|
17 |
|
|
the entry for erfc is provided because of the |
18 |
|
|
extreme loss of relative accuracy if erf(x) is |
19 |
|
|
called for large x and the result subtracted |
20 |
|
|
from 1. (e.g. for x= 10, 12 places are lost). |
21 |
|
|
|
22 |
|
|
There are no error returns. |
23 |
|
|
|
24 |
|
|
Calls exp. |
25 |
|
|
|
26 |
|
|
Coefficients for large x are #5667 from Hart & Cheney (18.72D). |
27 |
|
|
*/ |
28 |
|
|
|
29 |
|
|
#define M 7 |
30 |
|
|
#define N 9 |
31 |
|
|
static double torp = 1.1283791670955125738961589031; |
32 |
|
|
static double p1[] = { |
33 |
|
|
0.804373630960840172832162e5, |
34 |
|
|
0.740407142710151470082064e4, |
35 |
|
|
0.301782788536507577809226e4, |
36 |
|
|
0.380140318123903008244444e2, |
37 |
|
|
0.143383842191748205576712e2, |
38 |
|
|
-.288805137207594084924010e0, |
39 |
|
|
0.007547728033418631287834e0, |
40 |
|
|
}; |
41 |
|
|
static double q1[] = { |
42 |
|
|
0.804373630960840172826266e5, |
43 |
|
|
0.342165257924628539769006e5, |
44 |
|
|
0.637960017324428279487120e4, |
45 |
|
|
0.658070155459240506326937e3, |
46 |
|
|
0.380190713951939403753468e2, |
47 |
|
|
0.100000000000000000000000e1, |
48 |
|
|
0.0, |
49 |
|
|
}; |
50 |
|
|
static double p2[] = { |
51 |
|
|
0.18263348842295112592168999e4, |
52 |
|
|
0.28980293292167655611275846e4, |
53 |
|
|
0.2320439590251635247384768711e4, |
54 |
|
|
0.1143262070703886173606073338e4, |
55 |
|
|
0.3685196154710010637133875746e3, |
56 |
|
|
0.7708161730368428609781633646e2, |
57 |
|
|
0.9675807882987265400604202961e1, |
58 |
|
|
0.5641877825507397413087057563e0, |
59 |
|
|
0.0, |
60 |
|
|
}; |
61 |
|
|
static double q2[] = { |
62 |
|
|
0.18263348842295112595576438e4, |
63 |
|
|
0.495882756472114071495438422e4, |
64 |
|
|
0.60895424232724435504633068e4, |
65 |
|
|
0.4429612803883682726711528526e4, |
66 |
|
|
0.2094384367789539593790281779e4, |
67 |
|
|
0.6617361207107653469211984771e3, |
68 |
|
|
0.1371255960500622202878443578e3, |
69 |
|
|
0.1714980943627607849376131193e2, |
70 |
|
|
1.0, |
71 |
|
|
}; |
72 |
|
|
|
73 |
|
|
double |
74 |
|
|
erf(arg) double arg;{ |
75 |
|
|
double erfc(); |
76 |
|
|
int sign; |
77 |
|
|
double argsq; |
78 |
|
|
double d, n; |
79 |
|
|
int i; |
80 |
|
|
|
81 |
|
|
sign = 1; |
82 |
|
|
if(arg < 0.){ |
83 |
|
|
arg = -arg; |
84 |
|
|
sign = -1; |
85 |
|
|
} |
86 |
|
|
if(arg < 0.5){ |
87 |
|
|
argsq = arg*arg; |
88 |
|
|
for(n=0,d=0,i=M-1; i>=0; i--){ |
89 |
|
|
n = n*argsq + p1[i]; |
90 |
|
|
d = d*argsq + q1[i]; |
91 |
|
|
} |
92 |
|
|
return(sign*torp*arg*n/d); |
93 |
|
|
} |
94 |
|
|
if(arg >= 10.) |
95 |
|
|
return(sign*1.); |
96 |
|
|
return(sign*(1. - erfc(arg))); |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
double |
100 |
|
|
erfc(arg) double arg;{ |
101 |
|
|
double erf(); |
102 |
|
|
double exp(); |
103 |
|
|
double n, d; |
104 |
|
|
int i; |
105 |
|
|
|
106 |
|
|
if(arg < 0.) |
107 |
|
|
return(2. - erfc(-arg)); |
108 |
|
|
/* |
109 |
|
|
if(arg < 0.5) |
110 |
|
|
return(1. - erf(arg)); |
111 |
|
|
*/ |
112 |
|
|
if(arg >= 10.) |
113 |
|
|
return(0.); |
114 |
|
|
|
115 |
|
|
for(n=0,d=0,i=N-1; i>=0; i--){ |
116 |
|
|
n = n*arg + p2[i]; |
117 |
|
|
d = d*arg + q2[i]; |
118 |
|
|
} |
119 |
|
|
return(exp(-arg*arg)*n/d); |
120 |
|
|
} |