...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/zeta.hpp>
namespace boost{ namespace math{ template <class T> calculated-result-type zeta(T z); template <class T, class Policy> calculated-result-type zeta(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 zeta(T z); template <class T, class Policy> calculated-result-type zeta(T z, const Policy&);
Returns the zeta 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 and Cephes libraries. Unless otherwise specified any floating point type that is narrower than the one shown will have effectively zero error.
Table 6.76. Error rates for zeta
GNU C++ version 7.1.0 |
GNU C++ version 7.1.0 |
Sun compiler version 0x5150 |
Microsoft Visual C++ version 14.1 |
|
---|---|---|---|---|
Zeta: Random values greater than 1 |
Max = 0.846ε (Mean = 0.0833ε) |
Max = 0ε (Mean = 0ε) |
Max = 0.846ε (Mean = 0.0833ε) |
Max = 0.836ε (Mean = 0.093ε) |
Zeta: Random values less than 1 |
Max = 7.03ε (Mean = 2.93ε) |
Max = 0ε (Mean = 0ε) |
Max = 70.1ε (Mean = 17.1ε) |
Max = 6.84ε (Mean = 3.12ε) |
Zeta: Values close to and greater than 1 |
Max = 0.995ε (Mean = 0.5ε) |
Max = 0ε (Mean = 0ε) |
Max = 0.995ε (Mean = 0.5ε) |
Max = 0.994ε (Mean = 0.421ε) |
Zeta: Values close to and less than 1 |
Max = 0.998ε (Mean = 0.508ε) |
Max = 0ε (Mean = 0ε) |
Max = 0.998ε (Mean = 0.508ε) |
Max = 0.991ε (Mean = 0.375ε) |
Zeta: Integer arguments |
Max = 9ε (Mean = 3.06ε) |
Max = 0ε (Mean = 0ε) |
Max = 28ε (Mean = 5.62ε) |
Max = 9ε (Mean = 3ε) |
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
.
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:
The generic versions of these functions are implemented using the series:
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 0 < z < 1 the approximating form is:
For a rational approximation R(1-z) and a constant C.
For 1 < z < 4 the approximating form is:
For a rational approximation R(n-z) and a constant C and integer n.
For z > 4 the approximating form is:
ζ(z) = 1 + eR(z - n)
For a rational approximation R(z-n) and integer n, note that the accuracy required for R(z-n) is not full machine precision, but an absolute error of: ε/R(0). This saves us quite a few digits when dealing with large z, especially when ε is small.
Finally, there are some special cases for integer arguments, there are closed forms for negative or even integers:
and for positive odd integers we simply cache pre-computed values as these are of great benefit to some infinite series calculations.