Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/constants"

From cppreference.com
< cpp‎ | numeric
(~ s/1 ... 1/42)
m (See also: + std::ratio)
Line 101: Line 101:
 
γ with 34 digits precision = 0.577215664901532860606512090082402
 
γ with 34 digits precision = 0.577215664901532860606512090082402
 
}}
 
}}
 +
 +
===See also===
 +
{{dsc begin}}
 +
{{dsc inc | cpp/numeric/ratio/dsc ratio}}
 +
{{dsc end}}
  
 
{{langlinks|es|ja|zh}}
 
{{langlinks|es|ja|zh}}

Revision as of 10:43, 11 April 2021

 
 
 
Mathematical constants
 

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
π
(variable template)
inv_pi_v
1
π

(variable template)
inv_sqrtpi_v
1
π

(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
1
3

(variable template)
egamma_v
the Euler–Mascheroni constant
(variable template)
phi_v
the golden ratio Φ constant (
1 + 5
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)

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

#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <numbers>
#include <string_view>
 
struct two_t {};
template <class T>
constexpr auto operator^(T base, two_t) { return base * base; }
 
int main()
{
    using namespace std::numbers;
    constexpr two_t ²;
 
    std::cout << "The answer is " <<
        (((std::sin(e)^²) + (std::cos(e)^²)) + 
        std::pow(e, ln2) + std::sqrt(pi) * inv_sqrtpi +
        ((std::cosh(pi)^²) - (std::sinh(pi)^²)) +
        sqrt3 * inv_sqrt3 * log2e * ln2 * log10e * ln10 *
        pi * inv_pi + (phi * phi - phi)) *
        ((sqrt2 * sqrt3)^²) << '\n';
 
    auto egamma_aprox = [] (unsigned const iterations) {
        long double s = 0, 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) [edit]