Difference between revisions of "cpp/numeric/constants"
From cppreference.com
m (describe egamma using current Wikipedia article name and remove unnecessary word 'constant' from description of golden ratio) |
(Gonna have to disagree on calling it Euler's constant. Reputable sources like mathworld, proofwiki and the C++ standard call it Euler–Mascheroni, and merely "Euler something" is always a little ambiguous) |
||
Line 17: | Line 17: | ||
{{dsc tvar|sqrt3_v|nolink=true|{{mathjax-or|1=\(\sqrt3\)|2={{mrad|3}}}}}} | {{dsc tvar|sqrt3_v|nolink=true|{{mathjax-or|1=\(\sqrt3\)|2={{mrad|3}}}}}} | ||
{{dsc tvar|inv_sqrt3_v|nolink=true|{{mathjax-or|1=\(\frac1{\sqrt3}\)|2={{mfrac|1|{{mrad|3}}}}}}}} | {{dsc tvar|inv_sqrt3_v|nolink=true|{{mathjax-or|1=\(\frac1{\sqrt3}\)|2={{mfrac|1|{{mrad|3}}}}}}}} | ||
− | {{dsc tvar|egamma_v|nolink=true|[[enwiki:Euler's_constant| | + | {{dsc tvar|egamma_v|nolink=true|[[enwiki:Euler's_constant|the Euler–Mascheroni constant γ]]}} |
{{dsc tvar|phi_v|nolink=true|[[enwiki:Golden_ratio|the golden ratio Φ]] ({{mathjax-or|1=\(\frac{1+\sqrt5}2\)|2={{mfrac|1 + {{mrad|5}}|2}}}})}} | {{dsc tvar|phi_v|nolink=true|[[enwiki:Golden_ratio|the golden ratio Φ]] ({{mathjax-or|1=\(\frac{1+\sqrt5}2\)|2={{mfrac|1 + {{mrad|5}}|2}}}})}} | ||
{{dsc const|{{dsc small|inline constexpr double}} e|nolink=true|{{c|e_v<double>}}}} | {{dsc const|{{dsc small|inline constexpr double}} e|nolink=true|{{c|e_v<double>}}}} |
Revision as of 06:33, 9 May 2023
Contents |
Constants (since C++20)
Defined in header
<numbers> | |||
Defined in namespace
std::numbers | |||
e_v |
the mathematical constant e (variable template) | ||
log2e_v |
log2e (variable template) | ||
log10e_v |
log10e (variable template) | ||
pi_v |
the mathematical constant π (variable template) | ||
inv_pi_v |
(variable template) | ||
inv_sqrtpi_v |
(variable template) | ||
ln2_v |
ln 2 (variable template) | ||
ln10_v |
ln 10 (variable template) | ||
sqrt2_v |
√2 (variable template) | ||
sqrt3_v |
√3 (variable template) | ||
inv_sqrt3_v |
(variable template) | ||
egamma_v |
the Euler–Mascheroni constant γ (variable template) | ||
phi_v |
the golden ratio Φ (
(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) |
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 and long double).
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 |
Example
Run this code
#include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <numbers> #include <string_view> using std::placeholders::_2; template<class T> constexpr auto operator^(T base, decltype(_2)) { return base * base; } int main() { using namespace std::numbers; std::cout << "The answer is " << (((std::sin(e)^_2) + (std::cos(e)^_2)) + std::pow(e, ln2) + std::sqrt(pi) * inv_sqrtpi + ((std::cosh(pi)^_2) - (std::sinh(pi)^_2)) + sqrt3 * inv_sqrt3 * log2e * ln2 * log10e * ln10 * pi * inv_pi + (phi * phi - phi)) * ((sqrt2 * sqrt3)^_2) << '\n'; auto egamma_aprox = [](unsigned const iterations) { long double s{}, m{2.0}; for (unsigned c{2}; c != iterations; ++c, ++m) { const long double t{std::riemann_zeta(m) / m}; (c & 1) == 0 ? s += t : s -= t; } return s; }; constexpr std::string_view γ{"0.577215664901532860606512090082402"}; std::cout << "γ as 10⁶ 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 10⁶ 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
See also
(C++11) |
represents exact rational fraction (class template) |