Difference between revisions of "cpp/numeric/constants"
From cppreference.com
m (Added Spanish link) |
(+ example) |
||
Line 40: | Line 40: | ||
A program may partially or explicitly specialize a mathematical constant variable template provided that the specialization depends on a program-defined type. | A program may partially or explicitly specialize a mathematical constant variable template provided that the specialization depends on a program-defined type. | ||
+ | |||
+ | ===Example=== | ||
+ | {{example||code= | ||
+ | #include <numbers> | ||
+ | #include <cmath> | ||
+ | #include <iostream> | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | using namespace std::numbers; | ||
+ | |||
+ | std::cout | ||
+ | << std::pow(e, ln2) / 2 << ' ' | ||
+ | << std::pow(std::cosh(pi), 2) - std::pow(std::sinh(pi), 2) << ' ' | ||
+ | << std::sqrt(pi) * inv_sqrtpi << ' ' | ||
+ | << std::pow(sqrt2 * sqrt3, 2) / 6 << ' ' | ||
+ | << sqrt3 * inv_sqrt3 << ' ' | ||
+ | << log2e * ln2 << ' ' | ||
+ | << log10e * ln10 << ' ' | ||
+ | << pi * inv_pi << ' ' | ||
+ | << phi * phi - phi << '\n'; | ||
+ | |||
+ | auto egamma_aprox = [] { | ||
+ | long double s = 0, m = 2.0; | ||
+ | for (unsigned c = 2; c != 1'000'000; ++c, ++m) { | ||
+ | const long double t = std::riemann_zeta(m) / m; | ||
+ | (c & 1) == 0 ? s += t : s -= t; | ||
+ | } | ||
+ | return s; | ||
+ | }; | ||
+ | |||
+ | std::cout << std::fixed << (egamma_aprox() - egamma_v<long double>) << '\n'; | ||
+ | } | ||
+ | |p=true | ||
+ | |output= | ||
+ | 1 1 1 1 1 1 1 1 1 | ||
+ | -0.000001 | ||
+ | }} | ||
{{langlinks|es|ja|zh}} | {{langlinks|es|ja|zh}} |
Revision as of 17:21, 4 October 2020
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 |
π (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 Φ constant (
(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.
Example
Run this code
#include <numbers> #include <cmath> #include <iostream> int main() { using namespace std::numbers; std::cout << std::pow(e, ln2) / 2 << ' ' << std::pow(std::cosh(pi), 2) - std::pow(std::sinh(pi), 2) << ' ' << std::sqrt(pi) * inv_sqrtpi << ' ' << std::pow(sqrt2 * sqrt3, 2) / 6 << ' ' << sqrt3 * inv_sqrt3 << ' ' << log2e * ln2 << ' ' << log10e * ln10 << ' ' << pi * inv_pi << ' ' << phi * phi - phi << '\n'; auto egamma_aprox = [] { long double s = 0, m = 2.0; for (unsigned c = 2; c != 1'000'000; ++c, ++m) { const long double t = std::riemann_zeta(m) / m; (c & 1) == 0 ? s += t : s -= t; } return s; }; std::cout << std::fixed << (egamma_aprox() - egamma_v<long double>) << '\n'; }
Possible output:
1 1 1 1 1 1 1 1 1 -0.000001