Mathematical constants
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Numerics library
Interpolations
Generic numeric operations
C-style checked integer arithmetic
Mathematical special functions (C++17)
Mathematical constants (C++20)
Basic linear algebra algorithms (C++26)
Data-parallel types (SIMD) (C++26)
Floating-point environment (C++11)
Bit manipulation (C++20)
Saturation arithmetic (C++26)
(C++17)
(C++17)
(C++20)
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++26)
Mathematical constants
[edit] Constants (since C++20)
Defined in header
<numbers>
Defined in namespace
std::numbers
log2e_v
(variable template)
log10e_v
(variable template)
inv_pi_v
1
π
(variable template)
inv_sqrtpi_v
1
√π
(variable template)
ln2_v
(variable template)
ln10_v
(variable template)
sqrt2_v
(variable template)
sqrt3_v
(variable template)
inv_sqrt3_v
1
√3
(variable template)
egamma_v
(variable template)
phi_v
1 + √5
2
)(variable template)
inline constexpr double e
(constant)
inline constexpr double log2e
(constant)
inline constexpr double log10e
(constant)
inline constexpr double pi
(constant)
inline constexpr double inv_pi
(constant)
inline constexpr double inv_sqrtpi
(constant)
inline constexpr double ln2
(constant)
inline constexpr double ln10
(constant)
inline constexpr double sqrt2
(constant)
inline constexpr double sqrt3
(constant)
inline constexpr double inv_sqrt3
(constant)
inline constexpr double egamma
(constant)
inline constexpr double phi
(constant)
[edit] Notes
A program that instantiates a primary template of a mathematical constant variable template is ill-formed.
The standard library specializes mathematical constant variable templates for all floating-point types (i.e. float, double, long double , and fixed width floating-point types (since C++23)).
A program may partially or explicitly specialize a mathematical constant variable template provided that the specialization depends on a program-defined type.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_math_constants |
201907L |
(C++20) | Mathematical constants |
[edit] Example
Run this code
#include <cmath> #include <iomanip> #include <iostream> #include <limits> #include <numbers> #include <string_view> auto egamma_aprox(const unsigned iterations) { long double s{}; for (unsigned m{2}; m != iterations; ++m) if (const long double t{std::riemann_zetal (m) / m}; m % 2) s -= t; else s += t; return s; }; int main() { using namespace std::numbers; using namespace std::string_view_literals; const auto x = std::sqrt (inv_pi) / inv_sqrtpi + std::ceil (std::exp2 (log2e)) + sqrt3 * inv_sqrt3 + std::exp (0); const auto v = (phi * phi - phi) + 1 / std::log2 (sqrt2) + log10e * ln10 + std::pow (e, ln2) - std::cos (pi); std::cout << "The answer is " << x * v << '\n'; constexpr auto γ{"0.577215664901532860606512090082402"sv}; std::cout << "γ as 106 sums of ±ζ(m)/m = " << egamma_aprox(1'000'000) << '\n' << "γ as egamma_v<float> = " << std::setprecision (std::numeric_limits <float>::digits10 + 1) << egamma_v<float> << '\n' << "γ as egamma_v<double> = " << std::setprecision (std::numeric_limits <double>::digits10 + 1) << egamma_v<double> << '\n' << "γ as egamma_v<long double> = " << std::setprecision (std::numeric_limits <long double>::digits10 + 1) << egamma_v<long double> << '\n' << "γ with " << γ.length() - 1 << " digits precision = " << γ << '\n'; }
Possible output:
The answer is 42 γ as 106 sums of ±ζ(m)/m = 0.577215 γ as egamma_v<float> = 0.5772157 γ as egamma_v<double> = 0.5772156649015329 γ as egamma_v<long double> = 0.5772156649015328606 γ with 34 digits precision = 0.577215664901532860606512090082402