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