Synopsis
#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
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.
Descriptiontemplate <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:
AccuracyThe 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?27.?Errors In the Function erf(z)
Table?28.?Errors In the Function erfc(z)
TestingThe 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. ImplementationAll 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 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 erfc(z) * exp(z*z) * z ~ c for some constant c.
Therefore for erfc(z) = exp(-z*z) * (C + R(z - B)) / z;
Again R(z - B) is optimised for absolute error, and the constant
For large erfc(z) = exp(-z*z) * (C + R(1 / z)) / z; |