File: erf.qbk

package info (click to toggle)
boost1.49 1.49.0-3.2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 427,096 kB
  • sloc: cpp: 1,806,930; xml: 101,307; ansic: 43,491; python: 28,668; sh: 11,922; cs: 2,118; perl: 714; makefile: 671; yacc: 456; asm: 353; php: 116; lisp: 60; sql: 13; csh: 6
file content (214 lines) | stat: -rw-r--r-- 6,352 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
[section:error_function Error Functions]

[h4 Synopsis]

``
#include <boost/math/special_functions/erf.hpp>
``

   namespace boost{ namespace math{
   
   template <class T>
   ``__sf_result`` erf(T z);
   
   template <class T, class ``__Policy``>
   ``__sf_result`` erf(T z, const ``__Policy``&);
   
   template <class T>
   ``__sf_result`` erfc(T z);
   
   template <class T, class ``__Policy``>
   ``__sf_result`` erfc(T z, const ``__Policy``&);
   
   }} // namespaces
   
The return type of these functions is computed using the __arg_pomotion_rules:
the return type is `double` if T is an integer type, and T otherwise.

[optional_policy]

[h4 Description]

   template <class T>
   ``__sf_result`` erf(T z);
   
   template <class T, class ``__Policy``>
   ``__sf_result`` erf(T z, const ``__Policy``&);
   
Returns the [@http://en.wikipedia.org/wiki/Error_function error function]
[@http://functions.wolfram.com/GammaBetaErf/Erf/ erf] of z:

[equation erf1]

[graph erf]

   template <class T>
   ``__sf_result`` erfc(T z);
   
   template <class T, class ``__Policy``>
   ``__sf_result`` erfc(T z, const ``__Policy``&);
   
Returns the complement of the [@http://functions.wolfram.com/GammaBetaErf/Erfc/ error function] of z:

[equation erf2]

[graph erfc]

[h4 Accuracy]

The following table shows the peak errors (in units of epsilon) 
found on various platforms with various floating point types, 
along with comparisons to the __gsl, __glibc, __hpc and __cephes libraries.
Unless otherwise specified any floating point type that is narrower
than the one shown will have __zero_error.

[table Errors In the Function erf(z)
[[Significand Size] [Platform and Compiler] [z < 0.5]  [0.5 < z < 8] [z > 8]]
[[53] [Win32, Visual C++ 8] [Peak=0 Mean=0

GSL Peak=2.0 Mean=0.3 

__cephes Peak=1.1 Mean=0.7]  [Peak=0.9 Mean=0.09 

GSL Peak=2.3 Mean=0.3 

__cephes Peak=1.3 Mean=0.2] [Peak=0 Mean=0

GSL Peak=0 Mean=0 

__cephes Peak=0 Mean=0]]
[[64] [RedHat Linux IA32, gcc-3.3] [Peak=0.7 Mean=0.07

__glibc Peak=0.9 Mean=0.2]  [Peak=0.9 Mean=0.2

__glibc Peak=0.9 Mean=0.07] [Peak=0 Mean=0

__glibc Peak=0 Mean=0]]
[[64] [Redhat Linux IA64, gcc-3.4.4] [Peak=0.7 Mean=0.07

__glibc Peak=0 Mean=0]  [Peak=0.9 Mean=0.1

__glibc Peak=0.5 Mean=0.03] [Peak=0 Mean=0

__glibc Peak=0 Mean=0]]
[[113] [HPUX IA64, aCC A.06.06] [Peak=0.8 Mean=0.1

__hpc Lib Peak=0.9 Mean=0.2]  [Peak=0.9 Mean=0.1

__hpc Lib Peak=0.5 Mean=0.02] [Peak=0 Mean=0

__hpc Lib Peak=0 Mean=0]]
]

[table Errors In the Function erfc(z)
[[Significand Size] [Platform and Compiler] [z < 0.5]  [0.5 < z < 8] [z > 8]]
[[53] [Win32, Visual C++ 8] [Peak=0.7 Mean=0.06 

GSL Peak=1.0 Mean=0.4 

__cephes Peak=0.7 Mean=0.06]  [Peak=0.99 Mean=0.3

GSL Peak=2.6 Mean=0.6 

__cephes Peak=3.6 Mean=0.7] [Peak=1.0 Mean=0.2

GSL Peak=3.9 Mean=0.4 

__cephes Peak=2.7 Mean=0.4]]
[[64] [RedHat Linux IA32, gcc-3.3] [Peak=0 Mean=0

__glibc Peak=0 Mean=0]  [Peak=1.4 Mean=0.3

__glibc Peak=1.3 Mean=0.3] [Peak=1.6 Mean=0.4

__glibc Peak=1.3 Mean=0.4]]
[[64] [Redhat Linux IA64, gcc-3.4.4] [Peak=0 Mean=0

__glibc Peak=0 Mean=0]  [Peak=1.4 Mean=0.3

__glibc Peak=0 Mean=0] [Peak=1.5 Mean=0.4

__glibc Peak=0 Mean=0] ]
[[113] [HPUX IA64, aCC A.06.06] [Peak=0 Mean=0

__hpc Peak=0 Mean=0]  [Peak=1.5 Mean=0.3

__hpc Peak=0.9 Mean=0.08] [Peak=1.6 Mean=0.4

__hpc Peak=0.9 Mean=0.1]]
]

[h4 Testing]

The tests for these functions come in two parts:
basic sanity checks use spot values calculated using
[@http://functions.wolfram.com/webMathematica/FunctionEvaluation.jsp?name=Erf Mathworld's online evaluator],
while accuracy checks use high-precision test values calculated at 1000-bit precision with
[@http://shoup.net/ntl/doc/RR.txt NTL::RR] and this implementation. 
Note that the generic and type-specific
versions of these functions use differing implementations internally, so this
gives us reasonably independent test data.  Using our test data to test other
"known good" implementations also provides an additional sanity check. 

[h4 Implementation]

All versions of these functions first use the usual reflection formulas
to make their arguments positive:

   erf(-z) = 1 - erf(z);
   
   erfc(-z) = 2 - erfc(z);  // preferred when -z < -0.5
   
   erfc(-z) = 1 + erf(z);   // preferred when -0.5 <= -z < 0

The generic versions of these functions are implemented in terms of
the incomplete gamma function.

When the significand (mantissa) size is recognised
(currently for 53, 64 and 113-bit reals, plus single-precision 24-bit handled via promotion to double)
then a series of rational approximations [jm_rationals] are used.

For `z <= 0.5` then a rational approximation to erf is used, based on the 
observation that erf is an odd function and therefore erf is calculated using:

   erf(z) = z * (C + R(z*z));
   
where the rational approximation R(z*z) is optimised for absolute error:
as long as its absolute error is small enough compared to the constant C, then any 
round-off error incurred during the computation of R(z*z) will effectively 
disappear from the result.  As a result the error for erf and erfc in this
region is very low: the last bit is incorrect in only a very small number of 
cases.

For `z > 0.5` we observe that over a small interval [a, b) then:

   erfc(z) * exp(z*z) * z ~ c
   
for some constant c.

Therefore for `z > 0.5` we calculate erfc using:

   erfc(z) = exp(-z*z) * (C + R(z - B)) / z;
   
Again R(z - B) is optimised for absolute error, and the constant `C` is
the average of `erfc(z) * exp(z*z) * z` taken at the endpoints of the range.
Once again, as long as the absolute error in R(z - B) is small
compared to `c` then `c + R(z - B)` will be correctly rounded, and the error
in the result will depend only on the accuracy of the exp function.  In practice,
in all but a very small number of cases, the error is confined to the last bit
of the result.  The constant `B` is chosen so that the left hand end of the range
of the rational approximation is 0.

For large `z` over a range \[a, +[infin]\] the above approximation is modified to:

   erfc(z) = exp(-z*z) * (C + R(1 / z)) / z;

[endsect]
[/ :error_function The Error Functions]

[/ 
  Copyright 2006 John Maddock and Paul A. Bristow.
  Distributed under the Boost Software License, Version 1.0.
  (See accompanying file LICENSE_1_0.txt or copy at
  http://www.boost.org/LICENSE_1_0.txt).
]