Mathematical constants - cppreference.com (original) (raw)
[edit] Constants (since C++20)
| Defined in namespace std::numbers | |
| the mathematical constant \(\small e\)e(variable template) | |
| \(\log_{2}e\)log2e(variable template) | |
| \(\log_{10}e\)log10e(variable template) | |
| the mathematical constant \(\pi\)π(variable template) | |
| \(\frac1\pi\)(variable template) | |
| \(\frac1{\sqrt\pi}\)(variable template) | |
| \(\ln{2}\)ln 2(variable template) | |
| \(\ln{10}\)ln 10(variable template) | |
| \(\sqrt2\)√2(variable template) | |
| \(\sqrt3\)√3(variable template) | |
| \(\frac1{\sqrt3}\)(variable template) | |
| the Euler–Mascheroni constant γ(variable template) | |
| the golden ratio Φ (\(\frac{1+\sqrt5}2\))(variable template) | |
| inline constexpr double e | e_v<double> (constant) |
| inline constexpr double log2e | log2e_v<double> (constant) |
| inline constexpr double log10e | log10e_v<double> (constant) |
| inline constexpr double pi | pi_v<double> (constant) |
| inline constexpr double inv_pi | inv_pi_v<double> (constant) |
| inline constexpr double inv_sqrtpi | inv_sqrtpi_v<double> (constant) |
| inline constexpr double ln2 | ln2_v<double> (constant) |
| inline constexpr double ln10 | ln10_v<double> (constant) |
| inline constexpr double sqrt2 | sqrt2_v<double> (constant) |
| inline constexpr double sqrt3 | sqrt3_v<double> (constant) |
| inline constexpr double inv_sqrt3 | inv_sqrt3_v<double> (constant) |
| inline constexpr double egamma | egamma_v<double> (constant) |
| inline constexpr double phi | phi_v<double> (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
#include
#include
#include
#include
#include
#include
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 10⁶ sums of ±ζ(m)/m = "
<< egamma_aprox(1'000'000) << '\n'
<< "γ as egamma_v = "
<< std::setprecision(std::numeric_limits::digits10 + 1)
<< egamma_v << '\n'
<< "γ as egamma_v = "
<< std::setprecision(std::numeric_limits::digits10 + 1)
<< egamma_v << '\n'
<< "γ as egamma_v = "
<< std::setprecision(std::numeric_limits::digits10 + 1)
<< egamma_v << '\n'
<< "γ with " << γ.length() - 1 << " digits precision = " << γ << '\n';
}
Possible output:
The answer is 42 γ as 10⁶ sums of ±ζ(m)/m = 0.577215 γ as egamma_v = 0.5772157 γ as egamma_v = 0.5772156649015329 γ as egamma_v = 0.5772156649015328606 γ with 34 digits precision = 0.577215664901532860606512090082402
[edit] See also
| | represents exact rational fraction (class template) [edit] | | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |