1*4887Schin #include "FEATURE/uwin"
2*4887Schin
3*4887Schin #if !_UWIN || _lib_erf
4*4887Schin
_STUB_erf()5*4887Schin void _STUB_erf(){}
6*4887Schin
7*4887Schin #else
8*4887Schin
9*4887Schin /*-
10*4887Schin * Copyright (c) 1992, 1993
11*4887Schin * The Regents of the University of California. All rights reserved.
12*4887Schin *
13*4887Schin * Redistribution and use in source and binary forms, with or without
14*4887Schin * modification, are permitted provided that the following conditions
15*4887Schin * are met:
16*4887Schin * 1. Redistributions of source code must retain the above copyright
17*4887Schin * notice, this list of conditions and the following disclaimer.
18*4887Schin * 2. Redistributions in binary form must reproduce the above copyright
19*4887Schin * notice, this list of conditions and the following disclaimer in the
20*4887Schin * documentation and/or other materials provided with the distribution.
21*4887Schin * 3. Neither the name of the University nor the names of its contributors
22*4887Schin * may be used to endorse or promote products derived from this software
23*4887Schin * without specific prior written permission.
24*4887Schin *
25*4887Schin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26*4887Schin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27*4887Schin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28*4887Schin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29*4887Schin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30*4887Schin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31*4887Schin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32*4887Schin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33*4887Schin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34*4887Schin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35*4887Schin * SUCH DAMAGE.
36*4887Schin */
37*4887Schin
38*4887Schin #ifndef lint
39*4887Schin static char sccsid[] = "@(#)erf.c 8.1 (Berkeley) 6/4/93";
40*4887Schin #endif /* not lint */
41*4887Schin
42*4887Schin /* Modified Nov 30, 1992 P. McILROY:
43*4887Schin * Replaced expansions for x >= 1.25 (error 1.7ulp vs ~6ulp)
44*4887Schin * Replaced even+odd with direct calculation for x < .84375,
45*4887Schin * to avoid destructive cancellation.
46*4887Schin *
47*4887Schin * Performance of erfc(x):
48*4887Schin * In 300000 trials in the range [.83, .84375] the
49*4887Schin * maximum observed error was 3.6ulp.
50*4887Schin *
51*4887Schin * In [.84735,1.25] the maximum observed error was <2.5ulp in
52*4887Schin * 100000 runs in the range [1.2, 1.25].
53*4887Schin *
54*4887Schin * In [1.25,26] (Not including subnormal results)
55*4887Schin * the error is < 1.7ulp.
56*4887Schin */
57*4887Schin
58*4887Schin /* double erf(double x)
59*4887Schin * double erfc(double x)
60*4887Schin * x
61*4887Schin * 2 |\
62*4887Schin * erf(x) = --------- | exp(-t*t)dt
63*4887Schin * sqrt(pi) \|
64*4887Schin * 0
65*4887Schin *
66*4887Schin * erfc(x) = 1-erf(x)
67*4887Schin *
68*4887Schin * Method:
69*4887Schin * 1. Reduce x to |x| by erf(-x) = -erf(x)
70*4887Schin * 2. For x in [0, 0.84375]
71*4887Schin * erf(x) = x + x*P(x^2)
72*4887Schin * erfc(x) = 1 - erf(x) if x<=0.25
73*4887Schin * = 0.5 + ((0.5-x)-x*P) if x in [0.25,0.84375]
74*4887Schin * where
75*4887Schin * 2 2 4 20
76*4887Schin * P = P(x ) = (p0 + p1 * x + p2 * x + ... + p10 * x )
77*4887Schin * is an approximation to (erf(x)-x)/x with precision
78*4887Schin *
79*4887Schin * -56.45
80*4887Schin * | P - (erf(x)-x)/x | <= 2
81*4887Schin *
82*4887Schin *
83*4887Schin * Remark. The formula is derived by noting
84*4887Schin * erf(x) = (2/sqrt(pi))*(x - x^3/3 + x^5/10 - x^7/42 + ....)
85*4887Schin * and that
86*4887Schin * 2/sqrt(pi) = 1.128379167095512573896158903121545171688
87*4887Schin * is close to one. The interval is chosen because the fixed
88*4887Schin * point of erf(x) is near 0.6174 (i.e., erf(x)=x when x is
89*4887Schin * near 0.6174), and by some experiment, 0.84375 is chosen to
90*4887Schin * guarantee the error is less than one ulp for erf.
91*4887Schin *
92*4887Schin * 3. For x in [0.84375,1.25], let s = x - 1, and
93*4887Schin * c = 0.84506291151 rounded to single (24 bits)
94*4887Schin * erf(x) = c + P1(s)/Q1(s)
95*4887Schin * erfc(x) = (1-c) - P1(s)/Q1(s)
96*4887Schin * |P1/Q1 - (erf(x)-c)| <= 2**-59.06
97*4887Schin * Remark: here we use the taylor series expansion at x=1.
98*4887Schin * erf(1+s) = erf(1) + s*Poly(s)
99*4887Schin * = 0.845.. + P1(s)/Q1(s)
100*4887Schin * That is, we use rational approximation to approximate
101*4887Schin * erf(1+s) - (c = (single)0.84506291151)
102*4887Schin * Note that |P1/Q1|< 0.078 for x in [0.84375,1.25]
103*4887Schin * where
104*4887Schin * P1(s) = degree 6 poly in s
105*4887Schin * Q1(s) = degree 6 poly in s
106*4887Schin *
107*4887Schin * 4. For x in [1.25, 2]; [2, 4]
108*4887Schin * erf(x) = 1.0 - tiny
109*4887Schin * erfc(x) = (1/x)exp(-x*x-(.5*log(pi) -.5z + R(z)/S(z))
110*4887Schin *
111*4887Schin * Where z = 1/(x*x), R is degree 9, and S is degree 3;
112*4887Schin *
113*4887Schin * 5. For x in [4,28]
114*4887Schin * erf(x) = 1.0 - tiny
115*4887Schin * erfc(x) = (1/x)exp(-x*x-(.5*log(pi)+eps + zP(z))
116*4887Schin *
117*4887Schin * Where P is degree 14 polynomial in 1/(x*x).
118*4887Schin *
119*4887Schin * Notes:
120*4887Schin * Here 4 and 5 make use of the asymptotic series
121*4887Schin * exp(-x*x)
122*4887Schin * erfc(x) ~ ---------- * ( 1 + Poly(1/x^2) );
123*4887Schin * x*sqrt(pi)
124*4887Schin *
125*4887Schin * where for z = 1/(x*x)
126*4887Schin * P(z) ~ z/2*(-1 + z*3/2*(1 + z*5/2*(-1 + z*7/2*(1 +...))))
127*4887Schin *
128*4887Schin * Thus we use rational approximation to approximate
129*4887Schin * erfc*x*exp(x*x) ~ 1/sqrt(pi);
130*4887Schin *
131*4887Schin * The error bound for the target function, G(z) for
132*4887Schin * the interval
133*4887Schin * [4, 28]:
134*4887Schin * |eps + 1/(z)P(z) - G(z)| < 2**(-56.61)
135*4887Schin * for [2, 4]:
136*4887Schin * |R(z)/S(z) - G(z)| < 2**(-58.24)
137*4887Schin * for [1.25, 2]:
138*4887Schin * |R(z)/S(z) - G(z)| < 2**(-58.12)
139*4887Schin *
140*4887Schin * 6. For inf > x >= 28
141*4887Schin * erf(x) = 1 - tiny (raise inexact)
142*4887Schin * erfc(x) = tiny*tiny (raise underflow)
143*4887Schin *
144*4887Schin * 7. Special cases:
145*4887Schin * erf(0) = 0, erf(inf) = 1, erf(-inf) = -1,
146*4887Schin * erfc(0) = 1, erfc(inf) = 0, erfc(-inf) = 2,
147*4887Schin * erfc/erf(NaN) is NaN
148*4887Schin */
149*4887Schin
150*4887Schin #if defined(vax) || defined(tahoe)
151*4887Schin #define _IEEE 0
152*4887Schin #define TRUNC(x) (double) (float) (x)
153*4887Schin #else
154*4887Schin #define _IEEE 1
155*4887Schin #define TRUNC(x) *(((int *) &x) + 1) &= 0xf8000000
156*4887Schin #define infnan(x) 0.0
157*4887Schin #endif
158*4887Schin
159*4887Schin #ifdef _IEEE_LIBM
160*4887Schin /*
161*4887Schin * redefining "___function" to "function" in _IEEE_LIBM mode
162*4887Schin */
163*4887Schin #include "ieee_libm.h"
164*4887Schin #endif
165*4887Schin #include "mathimpl.h"
166*4887Schin
167*4887Schin static double
168*4887Schin tiny = 1e-300,
169*4887Schin half = 0.5,
170*4887Schin one = 1.0,
171*4887Schin two = 2.0,
172*4887Schin c = 8.45062911510467529297e-01, /* (float)0.84506291151 */
173*4887Schin /*
174*4887Schin * Coefficients for approximation to erf in [0,0.84375]
175*4887Schin */
176*4887Schin p0t8 = 1.02703333676410051049867154944018394163280,
177*4887Schin p0 = 1.283791670955125638123339436800229927041e-0001,
178*4887Schin p1 = -3.761263890318340796574473028946097022260e-0001,
179*4887Schin p2 = 1.128379167093567004871858633779992337238e-0001,
180*4887Schin p3 = -2.686617064084433642889526516177508374437e-0002,
181*4887Schin p4 = 5.223977576966219409445780927846432273191e-0003,
182*4887Schin p5 = -8.548323822001639515038738961618255438422e-0004,
183*4887Schin p6 = 1.205520092530505090384383082516403772317e-0004,
184*4887Schin p7 = -1.492214100762529635365672665955239554276e-0005,
185*4887Schin p8 = 1.640186161764254363152286358441771740838e-0006,
186*4887Schin p9 = -1.571599331700515057841960987689515895479e-0007,
187*4887Schin p10= 1.073087585213621540635426191486561494058e-0008;
188*4887Schin /*
189*4887Schin * Coefficients for approximation to erf in [0.84375,1.25]
190*4887Schin */
191*4887Schin static double
192*4887Schin pa0 = -2.362118560752659485957248365514511540287e-0003,
193*4887Schin pa1 = 4.148561186837483359654781492060070469522e-0001,
194*4887Schin pa2 = -3.722078760357013107593507594535478633044e-0001,
195*4887Schin pa3 = 3.183466199011617316853636418691420262160e-0001,
196*4887Schin pa4 = -1.108946942823966771253985510891237782544e-0001,
197*4887Schin pa5 = 3.547830432561823343969797140537411825179e-0002,
198*4887Schin pa6 = -2.166375594868790886906539848893221184820e-0003,
199*4887Schin qa1 = 1.064208804008442270765369280952419863524e-0001,
200*4887Schin qa2 = 5.403979177021710663441167681878575087235e-0001,
201*4887Schin qa3 = 7.182865441419627066207655332170665812023e-0002,
202*4887Schin qa4 = 1.261712198087616469108438860983447773726e-0001,
203*4887Schin qa5 = 1.363708391202905087876983523620537833157e-0002,
204*4887Schin qa6 = 1.198449984679910764099772682882189711364e-0002;
205*4887Schin /*
206*4887Schin * log(sqrt(pi)) for large x expansions.
207*4887Schin * The tail (lsqrtPI_lo) is included in the rational
208*4887Schin * approximations.
209*4887Schin */
210*4887Schin static double
211*4887Schin lsqrtPI_hi = .5723649429247000819387380943226;
212*4887Schin /*
213*4887Schin * lsqrtPI_lo = .000000000000000005132975581353913;
214*4887Schin *
215*4887Schin * Coefficients for approximation to erfc in [2, 4]
216*4887Schin */
217*4887Schin static double
218*4887Schin rb0 = -1.5306508387410807582e-010, /* includes lsqrtPI_lo */
219*4887Schin rb1 = 2.15592846101742183841910806188e-008,
220*4887Schin rb2 = 6.24998557732436510470108714799e-001,
221*4887Schin rb3 = 8.24849222231141787631258921465e+000,
222*4887Schin rb4 = 2.63974967372233173534823436057e+001,
223*4887Schin rb5 = 9.86383092541570505318304640241e+000,
224*4887Schin rb6 = -7.28024154841991322228977878694e+000,
225*4887Schin rb7 = 5.96303287280680116566600190708e+000,
226*4887Schin rb8 = -4.40070358507372993983608466806e+000,
227*4887Schin rb9 = 2.39923700182518073731330332521e+000,
228*4887Schin rb10 = -6.89257464785841156285073338950e-001,
229*4887Schin sb1 = 1.56641558965626774835300238919e+001,
230*4887Schin sb2 = 7.20522741000949622502957936376e+001,
231*4887Schin sb3 = 9.60121069770492994166488642804e+001;
232*4887Schin /*
233*4887Schin * Coefficients for approximation to erfc in [1.25, 2]
234*4887Schin */
235*4887Schin static double
236*4887Schin rc0 = -2.47925334685189288817e-007, /* includes lsqrtPI_lo */
237*4887Schin rc1 = 1.28735722546372485255126993930e-005,
238*4887Schin rc2 = 6.24664954087883916855616917019e-001,
239*4887Schin rc3 = 4.69798884785807402408863708843e+000,
240*4887Schin rc4 = 7.61618295853929705430118701770e+000,
241*4887Schin rc5 = 9.15640208659364240872946538730e-001,
242*4887Schin rc6 = -3.59753040425048631334448145935e-001,
243*4887Schin rc7 = 1.42862267989304403403849619281e-001,
244*4887Schin rc8 = -4.74392758811439801958087514322e-002,
245*4887Schin rc9 = 1.09964787987580810135757047874e-002,
246*4887Schin rc10 = -1.28856240494889325194638463046e-003,
247*4887Schin sc1 = 9.97395106984001955652274773456e+000,
248*4887Schin sc2 = 2.80952153365721279953959310660e+001,
249*4887Schin sc3 = 2.19826478142545234106819407316e+001;
250*4887Schin /*
251*4887Schin * Coefficients for approximation to erfc in [4,28]
252*4887Schin */
253*4887Schin static double
254*4887Schin rd0 = -2.1491361969012978677e-016, /* includes lsqrtPI_lo */
255*4887Schin rd1 = -4.99999999999640086151350330820e-001,
256*4887Schin rd2 = 6.24999999772906433825880867516e-001,
257*4887Schin rd3 = -1.54166659428052432723177389562e+000,
258*4887Schin rd4 = 5.51561147405411844601985649206e+000,
259*4887Schin rd5 = -2.55046307982949826964613748714e+001,
260*4887Schin rd6 = 1.43631424382843846387913799845e+002,
261*4887Schin rd7 = -9.45789244999420134263345971704e+002,
262*4887Schin rd8 = 6.94834146607051206956384703517e+003,
263*4887Schin rd9 = -5.27176414235983393155038356781e+004,
264*4887Schin rd10 = 3.68530281128672766499221324921e+005,
265*4887Schin rd11 = -2.06466642800404317677021026611e+006,
266*4887Schin rd12 = 7.78293889471135381609201431274e+006,
267*4887Schin rd13 = -1.42821001129434127360582351685e+007;
268*4887Schin
269*4887Schin extern double erf(x)
270*4887Schin double x;
271*4887Schin {
272*4887Schin double R,S,P,Q,ax,s,y,z,r,fabs(),exp();
273*4887Schin if(!finite(x)) { /* erf(nan)=nan */
274*4887Schin if (isnan(x))
275*4887Schin return(x);
276*4887Schin return (x > 0 ? one : -one); /* erf(+/-inf)= +/-1 */
277*4887Schin }
278*4887Schin if ((ax = x) < 0)
279*4887Schin ax = - ax;
280*4887Schin if (ax < .84375) {
281*4887Schin if (ax < 3.7e-09) {
282*4887Schin if (ax < 1.0e-308)
283*4887Schin return 0.125*(8.0*x+p0t8*x); /*avoid underflow */
284*4887Schin return x + p0*x;
285*4887Schin }
286*4887Schin y = x*x;
287*4887Schin r = y*(p1+y*(p2+y*(p3+y*(p4+y*(p5+
288*4887Schin y*(p6+y*(p7+y*(p8+y*(p9+y*p10)))))))));
289*4887Schin return x + x*(p0+r);
290*4887Schin }
291*4887Schin if (ax < 1.25) { /* 0.84375 <= |x| < 1.25 */
292*4887Schin s = fabs(x)-one;
293*4887Schin P = pa0+s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6)))));
294*4887Schin Q = one+s*(qa1+s*(qa2+s*(qa3+s*(qa4+s*(qa5+s*qa6)))));
295*4887Schin if (x>=0)
296*4887Schin return (c + P/Q);
297*4887Schin else
298*4887Schin return (-c - P/Q);
299*4887Schin }
300*4887Schin if (ax >= 6.0) { /* inf>|x|>=6 */
301*4887Schin if (x >= 0.0)
302*4887Schin return (one-tiny);
303*4887Schin else
304*4887Schin return (tiny-one);
305*4887Schin }
306*4887Schin /* 1.25 <= |x| < 6 */
307*4887Schin z = -ax*ax;
308*4887Schin s = -one/z;
309*4887Schin if (ax < 2.0) {
310*4887Schin R = rc0+s*(rc1+s*(rc2+s*(rc3+s*(rc4+s*(rc5+
311*4887Schin s*(rc6+s*(rc7+s*(rc8+s*(rc9+s*rc10)))))))));
312*4887Schin S = one+s*(sc1+s*(sc2+s*sc3));
313*4887Schin } else {
314*4887Schin R = rb0+s*(rb1+s*(rb2+s*(rb3+s*(rb4+s*(rb5+
315*4887Schin s*(rb6+s*(rb7+s*(rb8+s*(rb9+s*rb10)))))))));
316*4887Schin S = one+s*(sb1+s*(sb2+s*sb3));
317*4887Schin }
318*4887Schin y = (R/S -.5*s) - lsqrtPI_hi;
319*4887Schin z += y;
320*4887Schin z = exp(z)/ax;
321*4887Schin if (x >= 0)
322*4887Schin return (one-z);
323*4887Schin else
324*4887Schin return (z-one);
325*4887Schin }
326*4887Schin
327*4887Schin extern double erfc(x)
328*4887Schin double x;
329*4887Schin {
330*4887Schin double R,S,P,Q,s,ax,y,z,r,fabs(),__exp__D();
331*4887Schin if (!finite(x)) {
332*4887Schin if (isnan(x)) /* erfc(NaN) = NaN */
333*4887Schin return(x);
334*4887Schin else if (x > 0) /* erfc(+-inf)=0,2 */
335*4887Schin return 0.0;
336*4887Schin else
337*4887Schin return 2.0;
338*4887Schin }
339*4887Schin if ((ax = x) < 0)
340*4887Schin ax = -ax;
341*4887Schin if (ax < .84375) { /* |x|<0.84375 */
342*4887Schin if (ax < 1.38777878078144568e-17) /* |x|<2**-56 */
343*4887Schin return one-x;
344*4887Schin y = x*x;
345*4887Schin r = y*(p1+y*(p2+y*(p3+y*(p4+y*(p5+
346*4887Schin y*(p6+y*(p7+y*(p8+y*(p9+y*p10)))))))));
347*4887Schin if (ax < .0625) { /* |x|<2**-4 */
348*4887Schin return (one-(x+x*(p0+r)));
349*4887Schin } else {
350*4887Schin r = x*(p0+r);
351*4887Schin r += (x-half);
352*4887Schin return (half - r);
353*4887Schin }
354*4887Schin }
355*4887Schin if (ax < 1.25) { /* 0.84375 <= |x| < 1.25 */
356*4887Schin s = ax-one;
357*4887Schin P = pa0+s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6)))));
358*4887Schin Q = one+s*(qa1+s*(qa2+s*(qa3+s*(qa4+s*(qa5+s*qa6)))));
359*4887Schin if (x>=0) {
360*4887Schin z = one-c; return z - P/Q;
361*4887Schin } else {
362*4887Schin z = c+P/Q; return one+z;
363*4887Schin }
364*4887Schin }
365*4887Schin if (ax >= 28) /* Out of range */
366*4887Schin if (x>0)
367*4887Schin return (tiny*tiny);
368*4887Schin else
369*4887Schin return (two-tiny);
370*4887Schin z = ax;
371*4887Schin TRUNC(z);
372*4887Schin y = z - ax; y *= (ax+z);
373*4887Schin z *= -z; /* Here z + y = -x^2 */
374*4887Schin s = one/(-z-y); /* 1/(x*x) */
375*4887Schin if (ax >= 4) { /* 6 <= ax */
376*4887Schin R = s*(rd1+s*(rd2+s*(rd3+s*(rd4+s*(rd5+
377*4887Schin s*(rd6+s*(rd7+s*(rd8+s*(rd9+s*(rd10
378*4887Schin +s*(rd11+s*(rd12+s*rd13))))))))))));
379*4887Schin y += rd0;
380*4887Schin } else if (ax >= 2) {
381*4887Schin R = rb0+s*(rb1+s*(rb2+s*(rb3+s*(rb4+s*(rb5+
382*4887Schin s*(rb6+s*(rb7+s*(rb8+s*(rb9+s*rb10)))))))));
383*4887Schin S = one+s*(sb1+s*(sb2+s*sb3));
384*4887Schin y += R/S;
385*4887Schin R = -.5*s;
386*4887Schin } else {
387*4887Schin R = rc0+s*(rc1+s*(rc2+s*(rc3+s*(rc4+s*(rc5+
388*4887Schin s*(rc6+s*(rc7+s*(rc8+s*(rc9+s*rc10)))))))));
389*4887Schin S = one+s*(sc1+s*(sc2+s*sc3));
390*4887Schin y += R/S;
391*4887Schin R = -.5*s;
392*4887Schin }
393*4887Schin /* return exp(-x^2 - lsqrtPI_hi + R + y)/x; */
394*4887Schin s = ((R + y) - lsqrtPI_hi) + z;
395*4887Schin y = (((z-s) - lsqrtPI_hi) + R) + y;
396*4887Schin r = __exp__D(s, y)/x;
397*4887Schin if (x>0)
398*4887Schin return r;
399*4887Schin else
400*4887Schin return two-r;
401*4887Schin }
402*4887Schin
403*4887Schin #endif
404