add Berkeley copyright; NOTE: this code is AT&T derived!
[unix-history] / usr / src / lib / libm / common_source / erf.c
CommitLineData
b6be2d90
KB
1/*
2 * Copyright (c) 1985 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8static char sccsid[] = "@(#)erf.c 5.2 (Berkeley) %G%";
9#endif /* not lint */
824cad0f
ZAL
10
11/*
12 C program for floating point error function
13
14 erf(x) returns the error function of its argument
15 erfc(x) returns 1.0-erf(x)
16
17 erf(x) is defined by
18 ${2 over sqrt(pi)} int from 0 to x e sup {-t sup 2} dt$
19
20 the entry for erfc is provided because of the
21 extreme loss of relative accuracy if erf(x) is
22 called for large x and the result subtracted
23 from 1. (e.g. for x= 10, 12 places are lost).
24
25 There are no error returns.
26
27 Calls exp.
28
29 Coefficients for large x are #5667 from Hart & Cheney (18.72D).
30*/
31
32#define M 7
33#define N 9
34static double torp = 1.1283791670955125738961589031;
35static double p1[] = {
36 0.804373630960840172832162e5,
37 0.740407142710151470082064e4,
38 0.301782788536507577809226e4,
39 0.380140318123903008244444e2,
40 0.143383842191748205576712e2,
41 -.288805137207594084924010e0,
42 0.007547728033418631287834e0,
43};
44static double q1[] = {
45 0.804373630960840172826266e5,
46 0.342165257924628539769006e5,
47 0.637960017324428279487120e4,
48 0.658070155459240506326937e3,
49 0.380190713951939403753468e2,
50 0.100000000000000000000000e1,
51 0.0,
52};
53static double p2[] = {
54 0.18263348842295112592168999e4,
55 0.28980293292167655611275846e4,
56 0.2320439590251635247384768711e4,
57 0.1143262070703886173606073338e4,
58 0.3685196154710010637133875746e3,
59 0.7708161730368428609781633646e2,
60 0.9675807882987265400604202961e1,
61 0.5641877825507397413087057563e0,
62 0.0,
63};
64static double q2[] = {
65 0.18263348842295112595576438e4,
66 0.495882756472114071495438422e4,
67 0.60895424232724435504633068e4,
68 0.4429612803883682726711528526e4,
69 0.2094384367789539593790281779e4,
70 0.6617361207107653469211984771e3,
71 0.1371255960500622202878443578e3,
72 0.1714980943627607849376131193e2,
73 1.0,
74};
75
76double
77erf(arg) double arg;{
78 double erfc();
79 int sign;
80 double argsq;
81 double d, n;
82 int i;
83
84 sign = 1;
85 if(arg < 0.){
86 arg = -arg;
87 sign = -1;
88 }
89 if(arg < 0.5){
90 argsq = arg*arg;
91 for(n=0,d=0,i=M-1; i>=0; i--){
92 n = n*argsq + p1[i];
93 d = d*argsq + q1[i];
94 }
95 return(sign*torp*arg*n/d);
96 }
97 if(arg >= 10.)
98 return(sign*1.);
99 return(sign*(1. - erfc(arg)));
100}
101
102double
103erfc(arg) double arg;{
104 double erf();
105 double exp();
106 double n, d;
107 int i;
108
109 if(arg < 0.)
110 return(2. - erfc(-arg));
111/*
112 if(arg < 0.5)
113 return(1. - erf(arg));
114*/
115 if(arg >= 10.)
116 return(0.);
117
118 for(n=0,d=0,i=N-1; i>=0; i--){
119 n = n*arg + p2[i];
120 d = d*arg + q2[i];
121 }
122 return(exp(-arg*arg)*n/d);
123}