erf.qbk 4.9 KB

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