1[section:error_function Error Function erf and complement erfc] 2 3[h4 Synopsis] 4 5`` 6#include <boost/math/special_functions/erf.hpp> 7`` 8 9 namespace boost{ namespace math{ 10 11 template <class T> 12 ``__sf_result`` erf(T z); 13 14 template <class T, class ``__Policy``> 15 ``__sf_result`` erf(T z, const ``__Policy``&); 16 17 template <class T> 18 ``__sf_result`` erfc(T z); 19 20 template <class T, class ``__Policy``> 21 ``__sf_result`` erfc(T z, const ``__Policy``&); 22 23 }} // namespaces 24 25The return type of these functions is computed using the __arg_promotion_rules: 26the return type is `double` if T is an integer type, and T otherwise. 27 28[optional_policy] 29 30[h4 Description] 31 32 template <class T> 33 ``__sf_result`` erf(T z); 34 35 template <class T, class ``__Policy``> 36 ``__sf_result`` erf(T z, const ``__Policy``&); 37 38Returns the [@http://en.wikipedia.org/wiki/Error_function error function] 39[@http://functions.wolfram.com/GammaBetaErf/Erf/ erf] of z: 40 41[equation erf1] 42 43[graph erf] 44 45 template <class T> 46 ``__sf_result`` erfc(T z); 47 48 template <class T, class ``__Policy``> 49 ``__sf_result`` erfc(T z, const ``__Policy``&); 50 51Returns the complement of the [@http://functions.wolfram.com/GammaBetaErf/Erfc/ error function] of z: 52 53[equation erf2] 54 55[graph erfc] 56 57[h4 Accuracy] 58 59The following table shows the peak errors (in units of epsilon) 60found on various platforms with various floating-point types, 61along with comparisons to the __gsl, __glibc, __hpc and __cephes libraries. 62Unless otherwise specified any floating-point type that is narrower 63than the one shown will have __zero_error. 64 65[table_erf] 66 67[table_erfc] 68 69The following error plot are based on an exhaustive search of the functions domain, MSVC-15.5 at `double` precision, 70and GCC-7.1/Ubuntu for `long double` and `__float128`. 71 72[graph erf__double] 73 74[graph erf__80_bit_long_double] 75 76[graph erf____float128] 77 78[h4 Testing] 79 80The tests for these functions come in two parts: 81basic sanity checks use spot values calculated using 82[@http://functions.wolfram.com/webMathematica/FunctionEvaluation.jsp?name=Erf Mathworld's online evaluator], 83while accuracy checks use high-precision test values calculated at 1000-bit precision with 84[@http://shoup.net/ntl/doc/RR.txt NTL::RR] and this implementation. 85Note that the generic and type-specific 86versions of these functions use differing implementations internally, so this 87gives us reasonably independent test data. Using our test data to test other 88"known good" implementations also provides an additional sanity check. 89 90[h4 Implementation] 91 92All versions of these functions first use the usual reflection formulas 93to make their arguments positive: 94 95[expression ['erf(-z) = 1 - erf(z);] ] 96 97[expression ['erfc(-z) = 2 - erfc(z);] // preferred when -z < -0.5] 98 99[expression ['erfc(-z) = 1 + erf(z);] // preferred when -0.5 <= -z < 0] 100 101The generic versions of these functions are implemented in terms of 102the incomplete gamma function. 103 104When the significand (mantissa) size is recognised 105(currently for 53, 64 and 113-bit reals, plus single-precision 24-bit handled via promotion to double) 106then a series of rational approximations [jm_rationals] are used. 107 108For `z <= 0.5` then a rational approximation to erf is used, based on the 109observation that erf is an odd function and therefore erf is calculated using: 110 111[expression ['erf(z) = z * (C + R(z*z));]] 112 113where the rational approximation /R(z*z)/ is optimised for absolute error: 114as long as its absolute error is small enough compared to the constant C, then any 115round-off error incurred during the computation of R(z*z) will effectively 116disappear from the result. As a result the error for erf and erfc in this 117region is very low: the last bit is incorrect in only a very small number of 118cases. 119 120For `z > 0.5` we observe that over a small interval \[['a, b)] then: 121 122[expression ['erfc(z) * exp(z*z) * z ~ c]] 123 124for some constant c. 125 126Therefore for `z > 0.5` we calculate `erfc` using: 127 128[expression ['erfc(z) = exp(-z*z) * (C + R(z - B)) / z;]] 129 130Again R(z - B) is optimised for absolute error, and the constant `C` is 131the average of `erfc(z) * exp(z*z) * z` taken at the endpoints of the range. 132Once again, as long as the absolute error in R(z - B) is small 133compared to `c` then `c + R(z - B)` will be correctly rounded, and the error 134in the result will depend only on the accuracy of the exp function. In practice, 135in all but a very small number of cases, the error is confined to the last bit 136of the result. The constant `B` is chosen so that the left hand end of the range 137of the rational approximation is 0. 138 139For large `z` over a range \[a, +[infin]\] the above approximation is modified to: 140 141[expression ['erfc(z) = exp(-z*z) * (C + R(1 / z)) / z;]] 142 143[endsect] [/section:error_function Error Function erf and complement erfc] 144 145[/ 146 Copyright 2006 John Maddock and Paul A. Bristow. 147 Distributed under the Boost Software License, Version 1.0. 148 (See accompanying file LICENSE_1_0.txt or copy at 149 http://www.boost.org/LICENSE_1_0.txt). 150] 151