Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Error Function erf and complement erfc

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 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.

Description
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:

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-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 8.28. Error rates for erf

GNU C++ version 7.1.0
linux
long double

GNU C++ version 7.1.0
linux
double

Sun compiler version 0x5150
Sun Solaris
long double

Microsoft Visual C++ version 14.1
Win32
double

Erf Function: Small Values

Max = 0.925ε (Mean = 0.193ε)

(<cmath>: Max = 0.944ε (Mean = 0.191ε))
(<math.h>: Max = 0.944ε (Mean = 0.191ε))

Max = 0.841ε (Mean = 0.0687ε)

(GSL 2.1: Max = 2.06ε (Mean = 0.319ε))

Max = 0.925ε (Mean = 0.193ε)

(<math.h>: Max = 0.944ε (Mean = 0.194ε))

Max = 0.996ε (Mean = 0.182ε)

(<math.h>: Max = 1.57ε (Mean = 0.317ε))

Erf Function: Medium Values

Max = 1.5ε (Mean = 0.193ε)

(<cmath>: Max = 0.921ε (Mean = 0.0723ε))
(<math.h>: Max = 0.921ε (Mean = 0.0723ε))

Max = 1ε (Mean = 0.119ε)

(GSL 2.1: Max = 2.31ε (Mean = 0.368ε))

Max = 1.5ε (Mean = 0.197ε)

(<math.h>: Max = 0.921ε (Mean = 0.071ε))

Max = 1ε (Mean = 0.171ε)

(<math.h>: Max = 1.19ε (Mean = 0.244ε))

Erf Function: Large Values

Max = 0ε (Mean = 0ε)

(<cmath>: Max = 0ε (Mean = 0ε))
(<math.h>: Max = 0ε (Mean = 0ε))

Max = 0ε (Mean = 0ε)

(GSL 2.1: Max = 0ε (Mean = 0ε))

Max = 0ε (Mean = 0ε)

(<math.h>: Max = 0ε (Mean = 0ε))

Max = 0ε (Mean = 0ε)

(<math.h>: Max = 0ε (Mean = 0ε))


Table 8.29. Error rates for erfc

GNU C++ version 7.1.0
linux
long double

GNU C++ version 7.1.0
linux
double

Sun compiler version 0x5150
Sun Solaris
long double

Microsoft Visual C++ version 14.1
Win32
double

Erf Function: Small Values

Max = 0ε (Mean = 0ε)

(<cmath>: Max = 0ε (Mean = 0ε))
(<math.h>: Max = 0ε (Mean = 0ε))

Max = 0.658ε (Mean = 0.0537ε)

(GSL 2.1: Max = 1.01ε (Mean = 0.485ε))

Max = 0ε (Mean = 0ε)

(<math.h>: Max = 0ε (Mean = 0ε))

Max = 0ε (Mean = 0ε)

(<math.h>: Max = 0ε (Mean = 0ε))

Erf Function: Medium Values

Max = 1.76ε (Mean = 0.365ε)

(<cmath>: Max = 1.35ε (Mean = 0.307ε))
(<math.h>: Max = 1.35ε (Mean = 0.307ε))

Max = 0.983ε (Mean = 0.213ε)

(GSL 2.1: Max = 2.64ε (Mean = 0.662ε))

Max = 1.76ε (Mean = 0.38ε)

(<math.h>: Max = 2.81ε (Mean = 0.739ε))

Max = 1.65ε (Mean = 0.373ε)

(<math.h>: Max = 2.36ε (Mean = 0.539ε))

Erf Function: Large Values

Max = 1.57ε (Mean = 0.542ε)

(<cmath>: Max = 1.26ε (Mean = 0.441ε))
(<math.h>: Max = 1.26ε (Mean = 0.441ε))

Max = 0.868ε (Mean = 0.147ε)

(GSL 2.1: Max = 3.9ε (Mean = 0.472ε))

Max = 1.57ε (Mean = 0.564ε)

(<math.h>: Max = 4.91ε (Mean = 1.54ε))

Max = 1.14ε (Mean = 0.248ε)

(<math.h>: Max = 1.84ε (Mean = 0.331ε))


The following error plot are based on an exhaustive search of the functions domain, MSVC-15.5 at double precision, and GCC-7.1/Ubuntu for long double and __float128.

Testing

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.

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 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;


PrevUpHomeNext