數學常數
出自 cppreference.com
目錄 |
[編輯] 常數 (C++20 起)
| 定義於標頭檔
<numbers> | |||
| 定義於命名空間
std::numbers | |||
| e_v |
數學常數 e (變數模板) | ||
| log2e_v |
log2e (變數模板) | ||
| log10e_v |
log10e (變數模板) | ||
| pi_v |
數學常數 π (變數模板) | ||
| inv_pi_v |
(變數模板) | ||
| inv_sqrtpi_v |
(變數模板) | ||
| ln2_v |
ln 2 (變數模板) | ||
| ln10_v |
ln 10 (變數模板) | ||
| sqrt2_v |
√2 (變數模板) | ||
| sqrt3_v |
√3 (變數模板) | ||
| inv_sqrt3_v |
(變數模板) | ||
| egamma_v |
歐拉-馬斯刻若尼常數 γ (變數模板) | ||
| phi_v |
黃金比例 Φ (
(變數模板) | ||
| inline constexpr double e |
e_v<double> (常數) | ||
| inline constexpr double log2e |
log2e_v<double> (常數) | ||
| inline constexpr double log10e |
log10e_v<double> (常數) | ||
| inline constexpr double pi |
pi_v<double> (常數) | ||
| inline constexpr double inv_pi |
inv_pi_v<double> (常數) | ||
| inline constexpr double inv_sqrtpi |
inv_sqrtpi_v<double> (常數) | ||
| inline constexpr double ln2 |
ln2_v<double> (常數) | ||
| inline constexpr double ln10 |
ln10_v<double> (常數) | ||
| inline constexpr double sqrt2 |
sqrt2_v<double> (常數) | ||
| inline constexpr double sqrt3 |
sqrt3_v<double> (常數) | ||
| inline constexpr double inv_sqrt3 |
inv_sqrt3_v<double> (常數) | ||
| inline constexpr double egamma |
egamma_v<double> (常數) | ||
| inline constexpr double phi |
phi_v<double> (常數) | ||
[編輯] 備註
實例化數學常數變數模板的主模板的程式是不合法的 (ill-formed)。
標準程式庫為所有浮點型別(即 float、double、long double 以及 固定寬度浮點型別(C++23 起))特化了數學常數變數模板。
若特化依賴於 程式自定義型別,則程式可以部分或顯式特化數學常數變數模板。
| 功能測試巨集 | 數值 | 標準 | 功能 |
|---|---|---|---|
__cpp_lib_math_constants |
201907L |
(C++20) | 數學常數 |
[編輯] 範例
執行此程式碼
#include <cmath> #include <iomanip> #include <iostream> #include <limits> #include <numbers> #include <string_view> 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<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'; }
可能輸出
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
[編輯] 參見
| (C++11) |
表示精確的有理分數 (類別模板) |