148402Sbostic /*- 2*56950Sbostic * Copyright (c) 1992 The Regents of the University of California. 348402Sbostic * All rights reserved. 448402Sbostic * 5*56950Sbostic * %sccs.include.redist.c% 634121Sbostic */ 724592Szliu 834121Sbostic #ifndef lint 9*56950Sbostic static char sccsid[] = "@(#)erf.c 5.4 (Berkeley) 12/02/92"; 1034121Sbostic #endif /* not lint */ 1134121Sbostic 1224592Szliu /* 13*56950Sbostic * ==================================================== 14*56950Sbostic * Copyright (C) 1992 by Sun Microsystems, Inc. 15*56950Sbostic * 16*56950Sbostic * Developed at SunPro, a Sun Microsystems, Inc. business. 17*56950Sbostic * Permission to use, copy, modify, and distribute this 18*56950Sbostic * software is freely granted, provided that this notice 19*56950Sbostic * is preserved. 20*56950Sbostic * ==================================================== 21*56950Sbostic * 22*56950Sbostic * ******************* WARNING ******************** 23*56950Sbostic * This is an alpha version of SunPro's FDLIBM (Freely 24*56950Sbostic * Distributable Math Library) for IEEE double precision 25*56950Sbostic * arithmetic. FDLIBM is a basic math library written 26*56950Sbostic * in C that runs on machines that conform to IEEE 27*56950Sbostic * Standard 754/854. This alpha version is distributed 28*56950Sbostic * for testing purpose. Those who use this software 29*56950Sbostic * should report any bugs to 30*56950Sbostic * 31*56950Sbostic * fdlibm-comments@sunpro.eng.sun.com 32*56950Sbostic * 33*56950Sbostic * -- K.C. Ng, Oct 12, 1992 34*56950Sbostic * ************************************************ 35*56950Sbostic */ 3624592Szliu 37*56950Sbostic /* Modified Nov 30, 1992 P. McILROY: 38*56950Sbostic * Replaced expansion for x > 6 39*56950Sbostic * Add #ifdef's for vax/tahoe. 40*56950Sbostic */ 4124592Szliu 42*56950Sbostic /* double erf(double x) 43*56950Sbostic * double erfc(double x) 44*56950Sbostic * x 45*56950Sbostic * 2 |\ 46*56950Sbostic * erf(x) = --------- | exp(-t*t)dt 47*56950Sbostic * sqrt(pi) \| 48*56950Sbostic * 0 49*56950Sbostic * 50*56950Sbostic * erfc(x) = 1-erf(x) 51*56950Sbostic * 52*56950Sbostic * Method: 53*56950Sbostic * 1. Reduce x to |x| by erf(-x) = -erf(x) 54*56950Sbostic * 2. For x in [0, 0.84375] 55*56950Sbostic * erf(x) = x + x*P(x^2) 56*56950Sbostic * erfc(x) = 1 - erf(x) if x<=0.25 57*56950Sbostic * = 0.5 + ((0.5-x)-x*P) if x in [0.25,0.84375] 58*56950Sbostic * where 59*56950Sbostic * 2 2 4 20 60*56950Sbostic * P = P(x ) = (p0 + p1 * x + p2 * x + ... + p10 * x ) 61*56950Sbostic * is an approximation to (erf(x)-x)/x with precision 62*56950Sbostic * 63*56950Sbostic * -56.45 64*56950Sbostic * | P - (erf(x)-x)/x | <= 2 65*56950Sbostic * 66*56950Sbostic * 67*56950Sbostic * Remark. The formula is derived by noting 68*56950Sbostic * erf(x) = (2/sqrt(pi))*(x - x^3/3 + x^5/10 - x^7/42 + ....) 69*56950Sbostic * and that 70*56950Sbostic * 2/sqrt(pi) = 1.128379167095512573896158903121545171688 71*56950Sbostic * is close to one. The interval is chosen because the fixed 72*56950Sbostic * point of erf(x) is near 0.6174 (i.e., erf(x)=x when x is 73*56950Sbostic * near 0.6174), and by some experiment, 0.84375 is chosen to 74*56950Sbostic * guarantee the error is less than one ulp for erf. 75*56950Sbostic * 76*56950Sbostic * 3. For x in [0.84375,1.25], let s = x - 1, and 77*56950Sbostic * c = 0.84506291151 rounded to single (24 bits) 78*56950Sbostic * erf(x) = c + P1(s)/Q1(s) 79*56950Sbostic * erfc(x) = (1-c) - P1(s)/Q1(s) 80*56950Sbostic * |P1/Q1 - (erf(x)-c)| <= 2**-59.06 81*56950Sbostic * Remark: here we use the taylor series expansion at x=1. 82*56950Sbostic * erf(1+s) = erf(1) + s*Poly(s) 83*56950Sbostic * = 0.845.. + P1(s)/Q1(s) 84*56950Sbostic * That is, we use rational approximation to approximate 85*56950Sbostic * erf(1+s) - (c = (single)0.84506291151) 86*56950Sbostic * Note that |P1/Q1|< 0.078 for x in [0.84375,1.25] 87*56950Sbostic * where 88*56950Sbostic * P1(s) = degree 7 poly in s 89*56950Sbostic * 90*56950Sbostic * 4. For x in [1.25,6], 91*56950Sbostic * erf(x) = 1 - erfc(x) 92*56950Sbostic * erfc(x) = exp(-x*x)*(1/x)*R1(1/x)/S1(1/x) 93*56950Sbostic * where 94*56950Sbostic * R1(y) = degree 7 poly in y, (y=1/x) 95*56950Sbostic * S1(y) = degree 8 poly in y 96*56950Sbostic * 97*56950Sbostic * 5. For x in [6,28] 98*56950Sbostic * erf(x) = 1.0 - tiny 99*56950Sbostic * erfc(x) = (1/x)exp(-x*x-(.5*log(pi)+eps) + zP(z)) 100*56950Sbostic * 101*56950Sbostic * Where P is degree 9 polynomial in z = 1/(x*x) 102*56950Sbostic * 103*56950Sbostic * Notes: 104*56950Sbostic * Here 4 and 5 make use of the asymptotic series 105*56950Sbostic * exp(-x*x) 106*56950Sbostic * erfc(x) ~ ---------- * ( 1 + Poly(1/x^2) ); 107*56950Sbostic * x*sqrt(pi) 108*56950Sbostic * 109*56950Sbostic * where for z = 1/(x*x) 110*56950Sbostic * P(z) ~ z/2*(-1 + z*3/2*(1 + z*5/2*(-1 + z*7/2*(1 +...)))) 111*56950Sbostic * 112*56950Sbostic * Thus we use rational approximation to approximate 113*56950Sbostic * erfc*x*exp(x*x) ~ 1/sqrt(pi) 114*56950Sbostic * 115*56950Sbostic * The error bound for the target function, G(z) for 116*56950Sbostic * case 5 is 117*56950Sbostic * |eps + 1/(x*x)P(1/x*x) - G(x)| < 2**(-58.34) 118*56950Sbostic * For case 4, 119*56950Sbostic * |R2/S2 - erfc*x*exp(x*x)| < 2**(-61.52) 120*56950Sbostic * 121*56950Sbostic * 6. For inf > x >= 28 122*56950Sbostic * erf(x) = 1 - tiny (raise inexact) 123*56950Sbostic * erfc(x) = tiny*tiny (raise underflow) 124*56950Sbostic * 125*56950Sbostic * 7. Special cases: 126*56950Sbostic * erf(0) = 0, erf(inf) = 1, erf(-inf) = -1, 127*56950Sbostic * erfc(0) = 1, erfc(inf) = 0, erfc(-inf) = 2, 128*56950Sbostic * erfc/erf(NaN) is NaN 129*56950Sbostic */ 13024592Szliu 131*56950Sbostic #if defined(vax) || defined(tahoe) 132*56950Sbostic #define _IEEE 0 133*56950Sbostic #define TRUNC(x) (double) (float) (x) 134*56950Sbostic #else 135*56950Sbostic #define _IEEE 1 136*56950Sbostic #define TRUNC(x) *(((int *) &x) + 1) &= 0xf8000000 137*56950Sbostic #define infnan(x) 0.0 138*56950Sbostic #endif 13924592Szliu 140*56950Sbostic #ifdef _IEEE_LIBM 141*56950Sbostic /* 142*56950Sbostic * redefining "___function" to "function" in _IEEE_LIBM mode 143*56950Sbostic */ 144*56950Sbostic #include "ieee_libm.h" 145*56950Sbostic #endif 14624592Szliu 147*56950Sbostic static double 148*56950Sbostic tiny = 1e-300, 149*56950Sbostic half = 0.5, 150*56950Sbostic one = 1.0, 151*56950Sbostic two = 2.0, 152*56950Sbostic c = 8.45062911510467529297e-01, /* (float)0.84506291151 */ 153*56950Sbostic /* 154*56950Sbostic * Coefficients for approximation to erf on [0,0.84375] 155*56950Sbostic */ 156*56950Sbostic p0t8 = 1.02703333676410051049867154944018394163280, 157*56950Sbostic p0 = 1.283791670955125638123339436800229927041e-0001, 158*56950Sbostic p1 = -3.761263890318340796574473028946097022260e-0001, 159*56950Sbostic p2 = 1.128379167093567004871858633779992337238e-0001, 160*56950Sbostic p3 = -2.686617064084433642889526516177508374437e-0002, 161*56950Sbostic p4 = 5.223977576966219409445780927846432273191e-0003, 162*56950Sbostic p5 = -8.548323822001639515038738961618255438422e-0004, 163*56950Sbostic p6 = 1.205520092530505090384383082516403772317e-0004, 164*56950Sbostic p7 = -1.492214100762529635365672665955239554276e-0005, 165*56950Sbostic p8 = 1.640186161764254363152286358441771740838e-0006, 166*56950Sbostic p9 = -1.571599331700515057841960987689515895479e-0007, 167*56950Sbostic p10= 1.073087585213621540635426191486561494058e-0008, 168*56950Sbostic /* 169*56950Sbostic * Coefficients for approximation to erf in [0.84375,1.25] 170*56950Sbostic */ 171*56950Sbostic pa0 = -2.362118560752659485957248365514511540287e-0003, 172*56950Sbostic pa1 = 4.148561186837483359654781492060070469522e-0001, 173*56950Sbostic pa2 = -3.722078760357013107593507594535478633044e-0001, 174*56950Sbostic pa3 = 3.183466199011617316853636418691420262160e-0001, 175*56950Sbostic pa4 = -1.108946942823966771253985510891237782544e-0001, 176*56950Sbostic pa5 = 3.547830432561823343969797140537411825179e-0002, 177*56950Sbostic pa6 = -2.166375594868790886906539848893221184820e-0003, 178*56950Sbostic qa1 = 1.064208804008442270765369280952419863524e-0001, 179*56950Sbostic qa2 = 5.403979177021710663441167681878575087235e-0001, 180*56950Sbostic qa3 = 7.182865441419627066207655332170665812023e-0002, 181*56950Sbostic qa4 = 1.261712198087616469108438860983447773726e-0001, 182*56950Sbostic qa5 = 1.363708391202905087876983523620537833157e-0002, 183*56950Sbostic qa6 = 1.198449984679910764099772682882189711364e-0002, 184*56950Sbostic /* 185*56950Sbostic * Coefficients for approximation to erfc in [1.25,6] 186*56950Sbostic */ 187*56950Sbostic ra0 = 5.641895806197543833169694096883621225329e-0001, 188*56950Sbostic ra1 = 7.239004794325021293310782759791744583987e+0000, 189*56950Sbostic ra2 = 4.615482605646378370356340497765510677914e+0001, 190*56950Sbostic ra3 = 1.831130716384318567879039478746072928548e+0002, 191*56950Sbostic ra4 = 4.827304689401256945023566678442020977744e+0002, 192*56950Sbostic ra5 = 8.443683805001379929687313735294340282751e+0002, 193*56950Sbostic ra6 = 9.151771804289399937165800774604677980269e+0002, 194*56950Sbostic ra7 = 4.884236881266866025539987843147838061930e+0002, 195*56950Sbostic sa1 = 1.283080158932067675016971332625972882793e+0001, 196*56950Sbostic sa2 = 8.230730944985601552133528541648529041935e+0001, 197*56950Sbostic sa3 = 3.309746710535947168967275132570416337810e+0002, 198*56950Sbostic sa4 = 8.960238586988354676031385802384985611536e+0002, 199*56950Sbostic sa5 = 1.652440076836585407285764071805622271834e+0003, 200*56950Sbostic sa6 = 2.010492426273281289533320672757992672142e+0003, 201*56950Sbostic sa7 = 1.466304171232599681829476641652969136592e+0003, 202*56950Sbostic sa8 = 4.884237022526160104676542187698268809111e+0002; 203*56950Sbostic /* 204*56950Sbostic * Coefficients for approximation to erfc in [6,28] 205*56950Sbostic */ 206*56950Sbostic #define a0_hi -0.5723649429247001929610405568 /* ~-.5log(pi) */ 207*56950Sbostic #define a0_lo -0.0000000000000000189783711362898601 20824592Szliu 209*56950Sbostic #define P0 -4.99999999999749700219098258458e-0001 /* -1 /2 */ 210*56950Sbostic #define P1 6.24999999807451578348604925850e-0001 /* 5/2 /4 */ 211*56950Sbostic #define P2 -1.54166659013994022942029005208e+0001 /* -37/3 /8 */ 212*56950Sbostic #define P3 5.51560710872094706047619183664e+0001 /* 353/4 /16 */ 213*56950Sbostic #define P4 -2.55036053070125880992691236315e+0002 /* -4081/5 /32 */ 214*56950Sbostic #define P5 1.43505282730286381820405949838e+0002 /* 55205/6 /64 */ 215*56950Sbostic #define P6 -9.36421869861889035746571607888e+0002 /* ....etc.... */ 216*56950Sbostic #define P7 6.51030087738772090233396738768e+0003 217*56950Sbostic #define P8 -3.98835620275180117459967732430e+0004 218*56950Sbostic #define P9 1.44460450428346201078966259956e+0005 21924592Szliu 22024592Szliu 221*56950Sbostic double erf(x) 222*56950Sbostic double x; 223*56950Sbostic { 224*56950Sbostic double R,S,P,Q,ax,s,y,z,odd,even,r,fabs(),exp(); 225*56950Sbostic if(!finite(x)) { /* erf(nan)=nan */ 226*56950Sbostic if (isnan(x)) 227*56950Sbostic return(x); 228*56950Sbostic return (x > 0 ? one : -one); /* erf(+/-inf)= +/-1 */ 22924592Szliu } 230*56950Sbostic if ((ax = x) < 0) 231*56950Sbostic ax = - ax; 232*56950Sbostic if (ax < .84375) { 233*56950Sbostic if (ax < 3.7e-09) { 234*56950Sbostic if (ax < 1.0e-308) 235*56950Sbostic return 0.125*(8.0*x+p0t8*x); /*avoid underflow */ 236*56950Sbostic return x + p0*x; 237*56950Sbostic } 238*56950Sbostic y = x*x; 239*56950Sbostic z = y*y; 240*56950Sbostic even = z*(p2+z*(p4+z*(p6+z*(p8+z*p10)))); 241*56950Sbostic odd = p1+z*(p3+z*(p5+z*(p7+z*p9))); 242*56950Sbostic r = y*odd+even; 243*56950Sbostic return x + x*(p0+r); 24424592Szliu } 245*56950Sbostic if (ax < 1.25) { /* 0.84375 <= |x| < 1.25 */ 246*56950Sbostic s = fabs(x)-one; 247*56950Sbostic P = pa0+s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6))))); 248*56950Sbostic Q = one+s*(qa1+s*(qa2+s*(qa3+s*(qa4+s*(qa5+s*qa6))))); 249*56950Sbostic if (x>=0) 250*56950Sbostic return (c + P/Q); 251*56950Sbostic else 252*56950Sbostic return (-c - P/Q); 253*56950Sbostic } 254*56950Sbostic if (ax >= 6.0) { /* inf>|x|>=6 */ 255*56950Sbostic if (x >= 0.0) 256*56950Sbostic return (one-tiny); 257*56950Sbostic else 258*56950Sbostic return (tiny-one); 259*56950Sbostic } 260*56950Sbostic /* 1.25 <= |x| < 6 */ 261*56950Sbostic s = one/fabs(x); 262*56950Sbostic R=ra0+s*(ra1+s*(ra2+s*(ra3+s*(ra4+s*(ra5+s*(ra6+s*ra7)))))); 263*56950Sbostic S=one+s*(sa1+s*(sa2+s*(sa3+s*(sa4+s*(sa5+s*(sa6+s*(sa7+s*sa8))))))); 264*56950Sbostic z = exp(-x*x)*(R/S)*s; 265*56950Sbostic if (x >= 0) 266*56950Sbostic return (one-z); 267*56950Sbostic else 268*56950Sbostic return (z-one); 26924592Szliu } 27024592Szliu 271*56950Sbostic double erfc(x) 272*56950Sbostic double x; 273*56950Sbostic { 274*56950Sbostic double R,S,P,Q,s,ax,y,odd,even,z,r,fabs(),exp__D(); 275*56950Sbostic if (!finite(x)) { 276*56950Sbostic if (isnan(x)) /* erfc(NaN) = NaN */ 277*56950Sbostic return(x); 278*56950Sbostic else if (x > 0) /* erfc(+-inf)=0,2 */ 279*56950Sbostic return 0.0; 280*56950Sbostic else 281*56950Sbostic return 2.0; 28224592Szliu } 283*56950Sbostic if ((ax = x) < 0) 284*56950Sbostic ax = -ax; 285*56950Sbostic if (ax < .84375) { /* |x|<0.84375 */ 286*56950Sbostic if (ax < 1.38777878078144568e-17) /* |x|<2**-56 */ 287*56950Sbostic return one-x; 288*56950Sbostic y = x*x; 289*56950Sbostic z = y*y; 290*56950Sbostic even = z*(p2+z*(p4+z*(p6+z*(p8+z*p10)))); 291*56950Sbostic odd = p1+z*(p3+z*(p5+z*(p7+z*p9))); 292*56950Sbostic r = y*odd+even; 293*56950Sbostic if (ax < .0625) { /* |x|<2**-4 */ 294*56950Sbostic return (one-(x+x*(p0+r))); 295*56950Sbostic } else { 296*56950Sbostic r = x*(p0+r); 297*56950Sbostic r += (x-half); 298*56950Sbostic return (half - r); 299*56950Sbostic } 300*56950Sbostic } 301*56950Sbostic if (ax < 1.25) { /* 0.84375 <= |x| < 1.25 */ 302*56950Sbostic s = ax-one; 303*56950Sbostic P = pa0+s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6))))); 304*56950Sbostic Q = one+s*(qa1+s*(qa2+s*(qa3+s*(qa4+s*(qa5+s*qa6))))); 305*56950Sbostic if (x>=0) { 306*56950Sbostic z = one-c; return z - P/Q; 307*56950Sbostic } else { 308*56950Sbostic z = c+P/Q; return one+z; 309*56950Sbostic } 310*56950Sbostic } 311*56950Sbostic if (ax >= 28) /* Out of range */ 312*56950Sbostic if (x>0) 313*56950Sbostic return (tiny*tiny); 314*56950Sbostic else 315*56950Sbostic return (two-tiny); 316*56950Sbostic z = ax; 317*56950Sbostic TRUNC(z); 318*56950Sbostic y = z - ax; y *= (ax+z); 319*56950Sbostic z *= -z; /* Here z + y = -x^2 */ 320*56950Sbostic if (ax >= 6) { /* 6 <= ax */ 321*56950Sbostic s = one/(-z-y); /* 1/(x*x) */ 322*56950Sbostic R = s*(P0+s*(P1+s*(P2+s*(P3+s*(P4+ 323*56950Sbostic s*(P5+s*(P6+s*(P7+s*(P8+s*P9))))))))); 324*56950Sbostic y += a0_lo; 325*56950Sbostic /* return exp(-x^2 + a0_hi + R)/x; */ 326*56950Sbostic s = ((R + y) + a0_hi) + z; 327*56950Sbostic y = (((z-s) + a0_hi) + R) + y; 328*56950Sbostic r = exp__D(s, y)/x; 329*56950Sbostic } else { /* 1.25 <= ax <= 6 */ 330*56950Sbostic s = one/(ax); 331*56950Sbostic R=ra0+s*(ra1+s*(ra2+s*(ra3+s*(ra4+ 332*56950Sbostic s*(ra5+s*(ra6+s*ra7)))))); 333*56950Sbostic S=one+s*(sa1+s*(sa2+s*(sa3+s*(sa4+ 334*56950Sbostic s*(sa5+s*(sa6+s*(sa7+s*sa8))))))); 335*56950Sbostic r = (R/S)/x; 336*56950Sbostic s = z + y; y = (z-s) + y; 337*56950Sbostic r *= exp__D(s, y); 338*56950Sbostic } 339*56950Sbostic if (x>0) 340*56950Sbostic return r; 341*56950Sbostic else 342*56950Sbostic return two-r; 34324592Szliu } 344