Boost.Math provides a collection of mathematical constants.
-
Readable. For the very many jobs just using built-in like double
, you can just write expressions like
double area = pi * r * r;
(If that's all you want, jump direct to use
in non-template code!)
-
Effortless - avoiding a search of reference sources.
-
Usable with both builtin floating point types, and user-defined, possibly
extended precision, types such as NTL, MPFR/GMP, mp_float: in the latter
case the constants are computed to the necessary precision and then cached.
-
Accurate - ensuring that the values are as accurate as possible for the
chosen floating-point type
-
No loss of accuracy from repeated rounding of intermediate computations.
-
Result is computed with higher precision and only rounded once.
-
Less risk of inaccurate result from functions pow, trig and log at
corner cases.
-
Less risk of cancellation
error.
-
Portable - as possible between different systems using different floating-point
precisions: see use in template
code.
-
Tested - by comparison with other published sources, or separately computed
at long double precision.
-
Faster - can avoid (re-)calculation at runtime.
-
If the value returned is a builtin type then it's returned by value
as a
constexpr
(C++11
feature, if available).
-
If the value is computed and cached (or constructed from a string
representation and cached), then it's returned by constant reference.
This can be significant if:
-
Functions pow, trig or log are used.
-
Inside an inner loop.
-
Using a high-precision UDT like Boost.Multiprecision.
-
Compiler optimizations possible with built-in types, especially
double
, are not available.