...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
#include <boost/math/special_functions/erf.hpp>
namespace boost{ namespace math{ template <class T> calculated-result-type erf(T z); template <class T, class Policy> calculated-result-type erf(T z, const Policy&); template <class T> calculated-result-type erfc(T z); template <class T, class Policy> calculated-result-type erfc(T z, const Policy&); }} // namespaces
The return type of these functions is computed using the result
type calculation rules: the return type is double
if T is an integer type, and T otherwise.
The final Policy argument is optional and can be used to control the behaviour of the function: how it handles errors, what level of precision to use etc. Refer to the policy documentation for more details.
template <class T> calculated-result-type erf(T z); template <class T, class Policy> calculated-result-type erf(T z, const Policy&);
Returns the error function erf of z:
template <class T> calculated-result-type erfc(T z); template <class T, class Policy> calculated-result-type erfc(T z, const Policy&);
Returns the complement of the error function of z:
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-1.9, GNU C Lib, HP-UX C Library and Cephes libraries. Unless otherwise specified any floating point type that is narrower than the one shown will have effectively zero error.
Table 6.28. Error rates for erf
Microsoft Visual C++ version 12.0 |
GNU C++ version 5.1.0 |
GNU C++ version 5.1.0 |
Sun compiler version 0x5130 |
|
---|---|---|---|---|
Erf Function: Small Values |
Max = 0.996ε (Mean = 0.182ε) |
Max = 0.925ε (Mean = 0.193ε) |
Max = 0.841ε (Mean = 0.0687ε) |
Max = 0.925ε (Mean = 0.193ε) |
Erf Function: Medium Values |
Max = 1ε (Mean = 0.169ε) |
Max = 1.5ε (Mean = 0.193ε) |
Max = 1ε (Mean = 0.119ε) |
Max = 1.5ε (Mean = 0.202ε) |
Erf Function: Large Values |
Max = 0ε (Mean = 0ε) |
Max = 0ε (Mean = 0ε) |
Max = 0ε (Mean = 0ε) |
Max = 0ε (Mean = 0ε) |
Table 6.29. Error rates for erfc
Microsoft Visual C++ version 12.0 |
GNU C++ version 5.1.0 |
GNU C++ version 5.1.0 |
Sun compiler version 0x5130 |
|
---|---|---|---|---|
Erf Function: Small Values |
Max = 0ε (Mean = 0ε) |
Max = 0ε (Mean = 0ε) |
Max = 0.658ε (Mean = 0.0537ε) |
Max = 0ε (Mean = 0ε) |
Erf Function: Medium Values |
Max = 1.65ε (Mean = 0.373ε) |
Max = 1.76ε (Mean = 0.365ε) |
Max = 0.983ε (Mean = 0.213ε) |
Max = 1.76ε (Mean = 0.383ε) |
Erf Function: Large Values |
Max = 1.14ε (Mean = 0.248ε) |
Max = 1.57ε (Mean = 0.542ε) |
Max = 0.868ε (Mean = 0.147ε) |
Max = 1.57ε (Mean = 0.564ε) |
The tests for these functions come in two parts: basic sanity checks use spot values calculated using Mathworld's online evaluator, while accuracy checks use high-precision test values calculated at 1000-bit precision with 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.
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 devised by JM 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, +∞] the above approximation is modified to:
erfc(z) = exp(-z*z) * (C + R(1 / z)) / z;