Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/constants"

From cppreference.com
< cpp‎ | numeric
m (Added Spanish link)
(Links to the definition of “program-defined type”.)
 
(20 intermediate revisions by 7 users not shown)
Line 2: Line 2:
 
{{cpp/numeric/constants/navbar}}
 
{{cpp/numeric/constants/navbar}}
  
=== Constants {{mark since c++20}} ===
+
===Constants {{mark since c++20}}===
 
{{dsc begin}}
 
{{dsc begin}}
 
{{dsc header|numbers}}
 
{{dsc header|numbers}}
 
{{dsc namespace|std::numbers}}
 
{{dsc namespace|std::numbers}}
{{dsc tvar | e_v | nolink=true | [https://en.wikipedia.org/wiki/E_(mathematical_constant) the mathematical constant {{math|e}}]}}
+
{{dsc tvar|e_v|nolink=true|{{enwiki|E (mathematical constant)|the mathematical constant {{mathjax-or|\(\small e\)|e}}}}}}
{{dsc tvar | log2e_v | nolink=true | {{mathjax-or|1=\(\log_{2}e\)|2=log{{su|b=2}}e}}}}
+
{{dsc tvar|log2e_v|nolink=true|{{mathjax-or|1=\(\log_{2}e\)|2=log{{su|b=2}}e}}}}
{{dsc tvar | log10e_v | nolink=true | {{mathjax-or|1=\(\log_{10}e\)|2=log{{su|b=10}}e}}}}
+
{{dsc tvar|log10e_v|nolink=true|{{mathjax-or|1=\(\log_{10}e\)|2=log{{su|b=10}}e}}}}
{{dsc tvar | pi_v | nolink=true | {{mathjax-or|1=\(\pi\)|2=π}} }}
+
{{dsc tvar|pi_v|nolink=true|{{enwiki|Pi (mathematical constant)|the mathematical constant {{mathjax-or|1=\(\pi\)|2=π}}}}}}
{{dsc tvar | inv_pi_v | nolink=true | {{mathjax-or|1=\(\frac1\pi\)|2={{mfrac|1|π}} }} }}
+
{{dsc tvar|inv_pi_v|nolink=true|{{mathjax-or|1=\(\frac1\pi\)|2={{mfrac|1|π}}}}}}
{{dsc tvar | inv_sqrtpi_v | nolink=true | {{mathjax-or|1=\(\frac1{\sqrt\pi}\)|2={{mfrac|1|{{mrad|π}} }} }} }}
+
{{dsc tvar|inv_sqrtpi_v|nolink=true|{{mathjax-or|1=\(\frac1{\sqrt\pi}\)|2={{mfrac|1|{{mrad|π}}}}}}}}
{{dsc tvar | ln2_v | nolink=true | {{mathjax-or|1=\(\ln{2}\)|2=ln 2}}}}
+
{{dsc tvar|ln2_v|nolink=true|{{mathjax-or|1=\(\ln{2}\)|2=ln 2}}}}
{{dsc tvar | ln10_v | nolink=true | {{mathjax-or|1=\(\ln{10}\)|2=ln 10}}}}
+
{{dsc tvar|ln10_v|nolink=true|{{mathjax-or|1=\(\ln{10}\)|2=ln 10}}}}
{{dsc tvar | sqrt2_v | nolink=true |{{mathjax-or|1=\(\sqrt2\)|2={{mrad|2}}}}}}
+
{{dsc tvar|sqrt2_v|nolink=true|{{mathjax-or|1=\(\sqrt2\)|2={{mrad|2}}}}}}
{{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 | [https://en.wikipedia.org/wiki/Euler%E2%80%93Mascheroni_constant the Euler–Mascheroni constant] }}
+
{{dsc tvar|egamma_v|nolink=true|{{enwiki|Euler's constant|the Euler–Mascheroni constant γ}}}}
{{dsc tvar | phi_v | nolink=true | [https://en.wikipedia.org/wiki/Golden_ratio the golden ratio Φ constant] ({{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>}}}}
{{dsc const | {{dsc small|inline constexpr double}} log2e | nolink=true | {{c|log2e_v<double>}} }}
+
{{dsc const|{{dsc small|inline constexpr double}} log2e|nolink=true|{{c|log2e_v<double>}}}}
{{dsc const | {{dsc small|inline constexpr double}} log10e | nolink=true | {{c|log10e_v<double>}} }}
+
{{dsc const|{{dsc small|inline constexpr double}} log10e|nolink=true|{{c|log10e_v<double>}}}}
{{dsc const | {{dsc small|inline constexpr double}} pi | nolink=true | {{c|pi_v<double>}} }}
+
{{dsc const|{{dsc small|inline constexpr double}} pi|nolink=true|{{c|pi_v<double>}}}}
{{dsc const | {{dsc small|inline constexpr double}} inv_pi | nolink=true | {{c|inv_pi_v<double>}} }}
+
{{dsc const|{{dsc small|inline constexpr double}} inv_pi|nolink=true|{{c|inv_pi_v<double>}}}}
{{dsc const | {{dsc small|inline constexpr double}} inv_sqrtpi | nolink=true | {{c|inv_sqrtpi_v<double>}} }}
+
{{dsc const|{{dsc small|inline constexpr double}} inv_sqrtpi|nolink=true|{{c|inv_sqrtpi_v<double>}}}}
{{dsc const | {{dsc small|inline constexpr double}} ln2 | nolink=true | {{c|ln2_v<double>}} }}
+
{{dsc const|{{dsc small|inline constexpr double}} ln2|nolink=true|{{c|ln2_v<double>}}}}
{{dsc const | {{dsc small|inline constexpr double}} ln10 | nolink=true | {{c|ln10_v<double>}} }}
+
{{dsc const|{{dsc small|inline constexpr double}} ln10|nolink=true|{{c|ln10_v<double>}}}}
{{dsc const | {{dsc small|inline constexpr double}} sqrt2 | nolink=true | {{c|sqrt2_v<double>}} }}
+
{{dsc const|{{dsc small|inline constexpr double}} sqrt2|nolink=true|{{c|sqrt2_v<double>}}}}
{{dsc const | {{dsc small|inline constexpr double}} sqrt3 | nolink=true | {{c|sqrt3_v<double>}} }}
+
{{dsc const|{{dsc small|inline constexpr double}} sqrt3|nolink=true|{{c|sqrt3_v<double>}}}}
{{dsc const | {{dsc small|inline constexpr double}} inv_sqrt3 | nolink=true | {{c|inv_sqrt3_v<double>}} }}
+
{{dsc const|{{dsc small|inline constexpr double}} inv_sqrt3|nolink=true|{{c|inv_sqrt3_v<double>}}}}
{{dsc const | {{dsc small|inline constexpr double}} egamma | nolink=true | {{c|egamma_v<double>}} }}
+
{{dsc const|{{dsc small|inline constexpr double}} egamma|nolink=true|{{c|egamma_v<double>}}}}
{{dsc const | {{dsc small|inline constexpr double}} phi | nolink=true | {{c|phi_v<double>}} }}
+
{{dsc const|{{dsc small|inline constexpr double}} phi|nolink=true|{{c|phi_v<double>}}}}
 
{{dsc end}}
 
{{dsc end}}
  
Line 37: Line 37:
 
A program that instantiates a primary template of a mathematical constant variable template is ill-formed.
 
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. {{c|float}}, {{c|double}} and {{c|long double}}).
+
The standard library specializes mathematical constant variable templates for all floating-point types (i.e. {{c/core|float}}, {{c/core|double}} and {{c/core|long double}}).
  
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 {{lsd|cpp/language/type#Program-defined type}}.
 +
{{feature test macro|__cpp_lib_math_constants|std=c++20|value=201907L|[[#top|Mathematical constants]]}}
  
{{langlinks|es|ja|zh}}
+
===Example===
 +
{{example|code=
 +
#include <cmath>
 +
#include <iomanip>
 +
#include <iostream>
 +
#include <limits>
 +
#include <numbers>
 +
#include <string_view>
 +
 
 +
auto egamma_aprox(const unsigned iterations)
 +
{
 +
    long double s = 0;
 +
    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;
 +
   
 +
    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';
 +
   
 +
    using namespace std::string_view_literals;
 +
    constexpr auto γ = "0.577215664901532860606512090082402"sv;
 +
   
 +
    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';
 +
}
 +
|p=true
 +
|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===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/numeric/ratio/dsc ratio}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|es|ja|ru|zh}}

Latest revision as of 19:03, 13 March 2024

 
 
 
Mathematical constants
 

Contents

[edit] 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
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 Φ (
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)

[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 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

[edit] Example

#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <numbers>
#include <string_view>
 
auto egamma_aprox(const unsigned iterations)
{
    long double s = 0;
    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;
 
    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';
 
    using namespace std::string_view_literals;
    constexpr auto γ = "0.577215664901532860606512090082402"sv;
 
    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

[edit] See also

(C++11)
represents exact rational fraction
(class template) [edit]