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