xref: /csrg-svn/lib/libm/common_source/erf.c (revision 61285)
148402Sbostic /*-
2*61285Sbostic  * Copyright (c) 1992, 1993
3*61285Sbostic  *	The Regents of the University of California.  All rights reserved.
448402Sbostic  *
556950Sbostic  * %sccs.include.redist.c%
634121Sbostic  */
724592Szliu 
834121Sbostic #ifndef lint
9*61285Sbostic static char sccsid[] = "@(#)erf.c	8.1 (Berkeley) 06/04/93";
1034121Sbostic #endif /* not lint */
1134121Sbostic 
1257130Smcilroy /* Modified Nov 30, 1992 P. McILROY:
1357130Smcilroy  *	Replaced expansions for x >= 1.25 (error 1.7ulp vs ~6ulp)
1457130Smcilroy  * Replaced even+odd with direct calculation for x < .84375,
1557130Smcilroy  * to avoid destructive cancellation.
1656950Sbostic  *
1757130Smcilroy  * Performance of erfc(x):
1857130Smcilroy  * In 300000 trials in the range [.83, .84375] the
1957130Smcilroy  * maximum observed error was 3.6ulp.
2056950Sbostic  *
2157130Smcilroy  * In [.84735,1.25] the maximum observed error was <2.5ulp in
2257130Smcilroy  * 100000 runs in the range [1.2, 1.25].
2356950Sbostic  *
2457130Smcilroy  * In [1.25,26] (Not including subnormal results)
2557130Smcilroy  * the error is < 1.7ulp.
2656950Sbostic  */
2724592Szliu 
2856950Sbostic /* double erf(double x)
2956950Sbostic  * double erfc(double x)
3056950Sbostic  *			     x
3156950Sbostic  *		      2      |\
3256950Sbostic  *     erf(x)  =  ---------  | exp(-t*t)dt
3356950Sbostic  *		   sqrt(pi) \|
3456950Sbostic  *			     0
3556950Sbostic  *
3656950Sbostic  *     erfc(x) =  1-erf(x)
3756950Sbostic  *
3856950Sbostic  * Method:
3956950Sbostic  *      1. Reduce x to |x| by erf(-x) = -erf(x)
4056950Sbostic  *	2. For x in [0, 0.84375]
4156950Sbostic  *	    erf(x)  = x + x*P(x^2)
4256950Sbostic  *          erfc(x) = 1 - erf(x)           if x<=0.25
4356950Sbostic  *                  = 0.5 + ((0.5-x)-x*P)  if x in [0.25,0.84375]
4456950Sbostic  *	   where
4556950Sbostic  *			2		 2	  4		  20
4656950Sbostic  *              P =  P(x ) = (p0 + p1 * x + p2 * x + ... + p10 * x  )
4756950Sbostic  * 	   is an approximation to (erf(x)-x)/x with precision
4856950Sbostic  *
4956950Sbostic  *						 -56.45
5056950Sbostic  *			| P - (erf(x)-x)/x | <= 2
5156950Sbostic  *
5256950Sbostic  *
5356950Sbostic  *	   Remark. The formula is derived by noting
5456950Sbostic  *          erf(x) = (2/sqrt(pi))*(x - x^3/3 + x^5/10 - x^7/42 + ....)
5556950Sbostic  *	   and that
5656950Sbostic  *          2/sqrt(pi) = 1.128379167095512573896158903121545171688
5757174Smcilroy  *	   is close to one. The interval is chosen because the fixed
5856950Sbostic  *	   point of erf(x) is near 0.6174 (i.e., erf(x)=x when x is
5956950Sbostic  *	   near 0.6174), and by some experiment, 0.84375 is chosen to
6056950Sbostic  * 	   guarantee the error is less than one ulp for erf.
6156950Sbostic  *
6256950Sbostic  *      3. For x in [0.84375,1.25], let s = x - 1, and
6356950Sbostic  *         c = 0.84506291151 rounded to single (24 bits)
6456950Sbostic  *         	erf(x)  = c  + P1(s)/Q1(s)
6556950Sbostic  *         	erfc(x) = (1-c)  - P1(s)/Q1(s)
6656950Sbostic  *         	|P1/Q1 - (erf(x)-c)| <= 2**-59.06
6756950Sbostic  *	   Remark: here we use the taylor series expansion at x=1.
6856950Sbostic  *		erf(1+s) = erf(1) + s*Poly(s)
6956950Sbostic  *			 = 0.845.. + P1(s)/Q1(s)
7056950Sbostic  *	   That is, we use rational approximation to approximate
7156950Sbostic  *			erf(1+s) - (c = (single)0.84506291151)
7256950Sbostic  *	   Note that |P1/Q1|< 0.078 for x in [0.84375,1.25]
7356950Sbostic  *	   where
7457174Smcilroy  *		P1(s) = degree 6 poly in s
7557174Smcilroy  *		Q1(s) = degree 6 poly in s
7656950Sbostic  *
7757130Smcilroy  *	4. For x in [1.25, 2]; [2, 4]
7857130Smcilroy  *         	erf(x)  = 1.0 - tiny
7957130Smcilroy  *		erfc(x)	= (1/x)exp(-x*x-(.5*log(pi) -.5z + R(z)/S(z))
8056950Sbostic  *
8157130Smcilroy  *	Where z = 1/(x*x), R is degree 9, and S is degree 3;
8257130Smcilroy  *
8357130Smcilroy  *      5. For x in [4,28]
8456950Sbostic  *         	erf(x)  = 1.0 - tiny
8557130Smcilroy  *		erfc(x)	= (1/x)exp(-x*x-(.5*log(pi)+eps + zP(z))
8656950Sbostic  *
8757130Smcilroy  *	Where P is degree 14 polynomial in 1/(x*x).
8856950Sbostic  *
8956950Sbostic  *      Notes:
9056950Sbostic  *	   Here 4 and 5 make use of the asymptotic series
9156950Sbostic  *			  exp(-x*x)
9256950Sbostic  *		erfc(x) ~ ---------- * ( 1 + Poly(1/x^2) );
9356950Sbostic  *			  x*sqrt(pi)
9456950Sbostic  *
9556950Sbostic  *		where for z = 1/(x*x)
9656950Sbostic  *		P(z) ~ z/2*(-1 + z*3/2*(1 + z*5/2*(-1 + z*7/2*(1 +...))))
9756950Sbostic  *
9856950Sbostic  *	   Thus we use rational approximation to approximate
9957130Smcilroy  *              erfc*x*exp(x*x) ~ 1/sqrt(pi);
10056950Sbostic  *
10156950Sbostic  *		The error bound for the target function, G(z) for
10257130Smcilroy  *		the interval
10357130Smcilroy  *		[4, 28]:
10457130Smcilroy  * 		|eps + 1/(z)P(z) - G(z)| < 2**(-56.61)
10557130Smcilroy  *		for [2, 4]:
10657130Smcilroy  *      	|R(z)/S(z) - G(z)|	 < 2**(-58.24)
10757130Smcilroy  *		for [1.25, 2]:
10857130Smcilroy  *		|R(z)/S(z) - G(z)|	 < 2**(-58.12)
10956950Sbostic  *
11056950Sbostic  *      6. For inf > x >= 28
11156950Sbostic  *         	erf(x)  = 1 - tiny  (raise inexact)
11256950Sbostic  *         	erfc(x) = tiny*tiny (raise underflow)
11356950Sbostic  *
11456950Sbostic  *      7. Special cases:
11556950Sbostic  *         	erf(0)  = 0, erf(inf)  = 1, erf(-inf) = -1,
11656950Sbostic  *         	erfc(0) = 1, erfc(inf) = 0, erfc(-inf) = 2,
11756950Sbostic  *	   	erfc/erf(NaN) is NaN
11856950Sbostic  */
11924592Szliu 
12056950Sbostic #if defined(vax) || defined(tahoe)
12156950Sbostic #define _IEEE	0
12256950Sbostic #define TRUNC(x) (double) (float) (x)
12356950Sbostic #else
12456950Sbostic #define _IEEE	1
12556950Sbostic #define TRUNC(x) *(((int *) &x) + 1) &= 0xf8000000
12656950Sbostic #define infnan(x) 0.0
12756950Sbostic #endif
12824592Szliu 
12956950Sbostic #ifdef _IEEE_LIBM
13056950Sbostic /*
13156950Sbostic  * redefining "___function" to "function" in _IEEE_LIBM mode
13256950Sbostic  */
13356950Sbostic #include "ieee_libm.h"
13456950Sbostic #endif
13524592Szliu 
13656950Sbostic static double
13756950Sbostic tiny	    = 1e-300,
13856950Sbostic half	    = 0.5,
13956950Sbostic one	    = 1.0,
14056950Sbostic two	    = 2.0,
14156950Sbostic c 	    = 8.45062911510467529297e-01, /* (float)0.84506291151 */
14256950Sbostic /*
14357130Smcilroy  * Coefficients for approximation to erf in [0,0.84375]
14456950Sbostic  */
14556950Sbostic p0t8 = 1.02703333676410051049867154944018394163280,
14656950Sbostic p0 =   1.283791670955125638123339436800229927041e-0001,
14756950Sbostic p1 =  -3.761263890318340796574473028946097022260e-0001,
14856950Sbostic p2 =   1.128379167093567004871858633779992337238e-0001,
14956950Sbostic p3 =  -2.686617064084433642889526516177508374437e-0002,
15056950Sbostic p4 =   5.223977576966219409445780927846432273191e-0003,
15156950Sbostic p5 =  -8.548323822001639515038738961618255438422e-0004,
15256950Sbostic p6 =   1.205520092530505090384383082516403772317e-0004,
15356950Sbostic p7 =  -1.492214100762529635365672665955239554276e-0005,
15456950Sbostic p8 =   1.640186161764254363152286358441771740838e-0006,
15556950Sbostic p9 =  -1.571599331700515057841960987689515895479e-0007,
15657130Smcilroy p10=   1.073087585213621540635426191486561494058e-0008;
15756950Sbostic /*
15857130Smcilroy  * Coefficients for approximation to erf in [0.84375,1.25]
15956950Sbostic  */
16057130Smcilroy static double
16156950Sbostic pa0 =  -2.362118560752659485957248365514511540287e-0003,
16256950Sbostic pa1 =   4.148561186837483359654781492060070469522e-0001,
16356950Sbostic pa2 =  -3.722078760357013107593507594535478633044e-0001,
16456950Sbostic pa3 =   3.183466199011617316853636418691420262160e-0001,
16556950Sbostic pa4 =  -1.108946942823966771253985510891237782544e-0001,
16656950Sbostic pa5 =   3.547830432561823343969797140537411825179e-0002,
16756950Sbostic pa6 =  -2.166375594868790886906539848893221184820e-0003,
16856950Sbostic qa1 =   1.064208804008442270765369280952419863524e-0001,
16956950Sbostic qa2 =   5.403979177021710663441167681878575087235e-0001,
17056950Sbostic qa3 =   7.182865441419627066207655332170665812023e-0002,
17156950Sbostic qa4 =   1.261712198087616469108438860983447773726e-0001,
17256950Sbostic qa5 =   1.363708391202905087876983523620537833157e-0002,
17357130Smcilroy qa6 =   1.198449984679910764099772682882189711364e-0002;
17456950Sbostic /*
17557130Smcilroy  * log(sqrt(pi)) for large x expansions.
17657130Smcilroy  * The tail (lsqrtPI_lo) is included in the rational
17757130Smcilroy  * approximations.
17857130Smcilroy */
17957130Smcilroy static double
18057130Smcilroy    lsqrtPI_hi = .5723649429247000819387380943226;
18156950Sbostic /*
18257130Smcilroy  * lsqrtPI_lo = .000000000000000005132975581353913;
18357130Smcilroy  *
18457130Smcilroy  * Coefficients for approximation to erfc in [2, 4]
18557130Smcilroy */
18657130Smcilroy static double
18757130Smcilroy rb0  =	-1.5306508387410807582e-010,	/* includes lsqrtPI_lo */
18857130Smcilroy rb1  =	 2.15592846101742183841910806188e-008,
18957130Smcilroy rb2  =	 6.24998557732436510470108714799e-001,
19057130Smcilroy rb3  =	 8.24849222231141787631258921465e+000,
19157130Smcilroy rb4  =	 2.63974967372233173534823436057e+001,
19257130Smcilroy rb5  =	 9.86383092541570505318304640241e+000,
19357130Smcilroy rb6  =	-7.28024154841991322228977878694e+000,
19457130Smcilroy rb7  =	 5.96303287280680116566600190708e+000,
19557130Smcilroy rb8  =	-4.40070358507372993983608466806e+000,
19657130Smcilroy rb9  =	 2.39923700182518073731330332521e+000,
19757130Smcilroy rb10 =	-6.89257464785841156285073338950e-001,
19857130Smcilroy sb1  =	 1.56641558965626774835300238919e+001,
19957130Smcilroy sb2  =	 7.20522741000949622502957936376e+001,
20057130Smcilroy sb3  =	 9.60121069770492994166488642804e+001;
20157130Smcilroy /*
20257130Smcilroy  * Coefficients for approximation to erfc in [1.25, 2]
20357130Smcilroy */
20457130Smcilroy static double
20557130Smcilroy rc0  =	-2.47925334685189288817e-007,	/* includes lsqrtPI_lo */
20657130Smcilroy rc1  =	 1.28735722546372485255126993930e-005,
20757130Smcilroy rc2  =	 6.24664954087883916855616917019e-001,
20857130Smcilroy rc3  =	 4.69798884785807402408863708843e+000,
20957130Smcilroy rc4  =	 7.61618295853929705430118701770e+000,
21057130Smcilroy rc5  =	 9.15640208659364240872946538730e-001,
21157130Smcilroy rc6  =	-3.59753040425048631334448145935e-001,
21257130Smcilroy rc7  =	 1.42862267989304403403849619281e-001,
21357130Smcilroy rc8  =	-4.74392758811439801958087514322e-002,
21457130Smcilroy rc9  =	 1.09964787987580810135757047874e-002,
21557130Smcilroy rc10 =	-1.28856240494889325194638463046e-003,
21657130Smcilroy sc1  =	 9.97395106984001955652274773456e+000,
21757130Smcilroy sc2  =	 2.80952153365721279953959310660e+001,
21857130Smcilroy sc3  =	 2.19826478142545234106819407316e+001;
21957130Smcilroy /*
22057130Smcilroy  * Coefficients for approximation to  erfc in [4,28]
22156950Sbostic  */
22257130Smcilroy static double
22357130Smcilroy rd0  =	-2.1491361969012978677e-016,	/* includes lsqrtPI_lo */
22457130Smcilroy rd1  =	-4.99999999999640086151350330820e-001,
22557130Smcilroy rd2  =	 6.24999999772906433825880867516e-001,
22657130Smcilroy rd3  =	-1.54166659428052432723177389562e+000,
22757130Smcilroy rd4  =	 5.51561147405411844601985649206e+000,
22857130Smcilroy rd5  =	-2.55046307982949826964613748714e+001,
22957130Smcilroy rd6  =	 1.43631424382843846387913799845e+002,
23057130Smcilroy rd7  =	-9.45789244999420134263345971704e+002,
23157130Smcilroy rd8  =	 6.94834146607051206956384703517e+003,
23257130Smcilroy rd9  =	-5.27176414235983393155038356781e+004,
23357130Smcilroy rd10 =	 3.68530281128672766499221324921e+005,
23457130Smcilroy rd11 =	-2.06466642800404317677021026611e+006,
23557130Smcilroy rd12 =	 7.78293889471135381609201431274e+006,
23657130Smcilroy rd13 =	-1.42821001129434127360582351685e+007;
23724592Szliu 
erf(x)23857131Smcilroy double erf(x)
23956950Sbostic 	double x;
24056950Sbostic {
24157130Smcilroy 	double R,S,P,Q,ax,s,y,z,r,fabs(),exp();
24256950Sbostic 	if(!finite(x)) {		/* erf(nan)=nan */
24356950Sbostic 	    if (isnan(x))
24456950Sbostic 		return(x);
24556950Sbostic 	    return (x > 0 ? one : -one); /* erf(+/-inf)= +/-1 */
24624592Szliu 	}
24756950Sbostic 	if ((ax = x) < 0)
24856950Sbostic 		ax = - ax;
24956950Sbostic 	if (ax < .84375) {
25056950Sbostic 	    if (ax < 3.7e-09) {
25156950Sbostic 		if (ax < 1.0e-308)
25256950Sbostic 		    return 0.125*(8.0*x+p0t8*x);  /*avoid underflow */
25356950Sbostic 		return x + p0*x;
25456950Sbostic 	    }
25556950Sbostic 	    y = x*x;
25657130Smcilroy 	    r = y*(p1+y*(p2+y*(p3+y*(p4+y*(p5+
25757130Smcilroy 			y*(p6+y*(p7+y*(p8+y*(p9+y*p10)))))))));
25856950Sbostic 	    return x + x*(p0+r);
25924592Szliu 	}
26056950Sbostic 	if (ax < 1.25) {		/* 0.84375 <= |x| < 1.25 */
26156950Sbostic 	    s = fabs(x)-one;
26256950Sbostic 	    P = pa0+s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6)))));
26356950Sbostic 	    Q = one+s*(qa1+s*(qa2+s*(qa3+s*(qa4+s*(qa5+s*qa6)))));
26456950Sbostic 	    if (x>=0)
26556950Sbostic 		return (c + P/Q);
26656950Sbostic 	    else
26756950Sbostic 		return (-c - P/Q);
26856950Sbostic 	}
26956950Sbostic 	if (ax >= 6.0) {		/* inf>|x|>=6 */
27056950Sbostic 	    if (x >= 0.0)
27156950Sbostic 		return (one-tiny);
27256950Sbostic 	    else
27356950Sbostic 		return (tiny-one);
27456950Sbostic 	}
27556950Sbostic     /* 1.25 <= |x| < 6 */
27657130Smcilroy 	z = -ax*ax;
27757130Smcilroy 	s = -one/z;
27857130Smcilroy 	if (ax < 2.0) {
27957130Smcilroy 		R = rc0+s*(rc1+s*(rc2+s*(rc3+s*(rc4+s*(rc5+
28057130Smcilroy 			s*(rc6+s*(rc7+s*(rc8+s*(rc9+s*rc10)))))))));
28157130Smcilroy 		S = one+s*(sc1+s*(sc2+s*sc3));
28257130Smcilroy 	} else {
28357130Smcilroy 		R = rb0+s*(rb1+s*(rb2+s*(rb3+s*(rb4+s*(rb5+
28457130Smcilroy 			s*(rb6+s*(rb7+s*(rb8+s*(rb9+s*rb10)))))))));
28557130Smcilroy 		S = one+s*(sb1+s*(sb2+s*sb3));
28657130Smcilroy 	}
28757130Smcilroy 	y = (R/S -.5*s) - lsqrtPI_hi;
28857130Smcilroy 	z += y;
28957130Smcilroy 	z = exp(z)/ax;
29056950Sbostic 	if (x >= 0)
29156950Sbostic 		return (one-z);
29256950Sbostic 	else
29356950Sbostic 		return (z-one);
29424592Szliu }
29524592Szliu 
erfc(x)29657131Smcilroy double erfc(x)
29756950Sbostic 	double x;
29856950Sbostic {
29957452Sbostic 	double R,S,P,Q,s,ax,y,z,r,fabs(),__exp__D();
30056950Sbostic 	if (!finite(x)) {
30156950Sbostic 		if (isnan(x))		/* erfc(NaN) = NaN */
30256950Sbostic 			return(x);
30356950Sbostic 		else if (x > 0)		/* erfc(+-inf)=0,2 */
30456950Sbostic 			return 0.0;
30556950Sbostic 		else
30656950Sbostic 			return 2.0;
30724592Szliu 	}
30856950Sbostic 	if ((ax = x) < 0)
30956950Sbostic 		ax = -ax;
31056950Sbostic 	if (ax < .84375) {			/* |x|<0.84375 */
31156950Sbostic 	    if (ax < 1.38777878078144568e-17)  	/* |x|<2**-56 */
31256950Sbostic 		return one-x;
31356950Sbostic 	    y = x*x;
31457130Smcilroy 	    r = y*(p1+y*(p2+y*(p3+y*(p4+y*(p5+
31557130Smcilroy 			y*(p6+y*(p7+y*(p8+y*(p9+y*p10)))))))));
31656950Sbostic 	    if (ax < .0625) {  	/* |x|<2**-4 */
31756950Sbostic 		return (one-(x+x*(p0+r)));
31856950Sbostic 	    } else {
31956950Sbostic 		r = x*(p0+r);
32056950Sbostic 		r += (x-half);
32156950Sbostic 	        return (half - r);
32256950Sbostic 	    }
32356950Sbostic 	}
32456950Sbostic 	if (ax < 1.25) {		/* 0.84375 <= |x| < 1.25 */
32556950Sbostic 	    s = ax-one;
32656950Sbostic 	    P = pa0+s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6)))));
32756950Sbostic 	    Q = one+s*(qa1+s*(qa2+s*(qa3+s*(qa4+s*(qa5+s*qa6)))));
32856950Sbostic 	    if (x>=0) {
32956950Sbostic 	        z  = one-c; return z - P/Q;
33056950Sbostic 	    } else {
33156950Sbostic 		z = c+P/Q; return one+z;
33256950Sbostic 	    }
33356950Sbostic 	}
33456950Sbostic 	if (ax >= 28)	/* Out of range */
33556950Sbostic  		if (x>0)
33656950Sbostic 			return (tiny*tiny);
33756950Sbostic 		else
33856950Sbostic 			return (two-tiny);
33956950Sbostic 	z = ax;
34056950Sbostic 	TRUNC(z);
34156950Sbostic 	y = z - ax; y *= (ax+z);
34256950Sbostic 	z *= -z;			/* Here z + y = -x^2 */
34356950Sbostic 		s = one/(-z-y);		/* 1/(x*x) */
34457130Smcilroy 	if (ax >= 4) {			/* 6 <= ax */
34557130Smcilroy 		R = s*(rd1+s*(rd2+s*(rd3+s*(rd4+s*(rd5+
34657130Smcilroy 			s*(rd6+s*(rd7+s*(rd8+s*(rd9+s*(rd10
34757130Smcilroy 			+s*(rd11+s*(rd12+s*rd13))))))))))));
34857130Smcilroy 		y += rd0;
34957130Smcilroy 	} else if (ax >= 2) {
35057130Smcilroy 		R = rb0+s*(rb1+s*(rb2+s*(rb3+s*(rb4+s*(rb5+
35157130Smcilroy 			s*(rb6+s*(rb7+s*(rb8+s*(rb9+s*rb10)))))))));
35257130Smcilroy 		S = one+s*(sb1+s*(sb2+s*sb3));
35357130Smcilroy 		y += R/S;
35457130Smcilroy 		R = -.5*s;
35557130Smcilroy 	} else {
35657130Smcilroy 		R = rc0+s*(rc1+s*(rc2+s*(rc3+s*(rc4+s*(rc5+
35757130Smcilroy 			s*(rc6+s*(rc7+s*(rc8+s*(rc9+s*rc10)))))))));
35857130Smcilroy 		S = one+s*(sc1+s*(sc2+s*sc3));
35957130Smcilroy 		y += R/S;
36057130Smcilroy 		R = -.5*s;
36156950Sbostic 	}
36257130Smcilroy 	/* return exp(-x^2 - lsqrtPI_hi + R + y)/x;	*/
36357130Smcilroy 	s = ((R + y) - lsqrtPI_hi) + z;
36457130Smcilroy 	y = (((z-s) - lsqrtPI_hi) + R) + y;
36557452Sbostic 	r = __exp__D(s, y)/x;
36656950Sbostic 	if (x>0)
36756950Sbostic 		return r;
36856950Sbostic 	else
36956950Sbostic 		return two-r;
37024592Szliu }
371