数学常数
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    常数 (C++20 起)
|   定义于头文件  
<numbers>  | |||
|   定义于命名空间  
std::numbers | |||
|    e_v  | 
   数学常数 e  (变量模板)  | ||
|    log2e_v  | 
   log 2e (变量模板)  | ||
|    log10e_v  | 
   log 10e (变量模板)  | ||
|    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>   (常量)  | ||
注解
实例化数学常数变量模板的初等模板的程序为谬构。
标准库对所有浮点类型(即 float 、 double 与 long double )特化数学常数变量模板。
程序可以部分或显式特化数学常数变量模板,只要该特化依赖程序定义的类型。
示例
运行此代码
#include <cmath> #include <iomanip> #include <iostream> #include <limits> #include <numbers> #include <string_view> 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_v<long double> - egamma_aprox()) << '\n'; constexpr std::string_view γ {"0.577215664901532860606512090082402"}; std::cout << "γ 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'; }
可能的输出:
1 1 1 1 1 1 1 1 1 0.000001 γ 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