Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Mathematical constants

      From cppreference.com
      <cpp‎ |numeric
       
       
       
      Mathematical constants
       

      Contents

      [edit]Constants(since C++20)

      Defined in header<numbers>
      Defined in namespacestd::numbers
      e_v
      the mathematical constant\(\small e\)e
      (variable template)
      log2e_v
      \(\log_{2}e\)log2e
      (variable template)
      log10e_v
      \(\log_{10}e\)log10e
      (variable template)
      pi_v
      the mathematical constant\(\pi\)π
      (variable template)
      inv_pi_v
      \(\frac1\pi\)
      1
      π

      (variable template)
      inv_sqrtpi_v
      \(\frac1{\sqrt\pi}\)
      1
      π

      (variable template)
      ln2_v
      \(\ln{2}\)ln 2
      (variable template)
      ln10_v
      \(\ln{10}\)ln 10
      (variable template)
      sqrt2_v
      \(\sqrt2\)2
      (variable template)
      sqrt3_v
      \(\sqrt3\)3
      (variable template)
      inv_sqrt3_v
      \(\frac1{\sqrt3}\)
      1
      3

      (variable template)
      egamma_v
      the Euler–Mascheroni constant γ
      (variable template)
      phi_v
      the golden ratio Φ (\(\frac{1+\sqrt5}2\)
      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,longdouble, andfixed width floating-point types(since C++23)).

      A program may partially or explicitly specialize a mathematical constant variable template provided that the specialization depends on aprogram-defined type.

      Feature-test macroValueStdFeature
      __cpp_lib_math_constants201907L(C++20)Mathematical constants

      [edit]Example

      Run this code
      #include <cmath>#include <iomanip>#include <iostream>#include <limits>#include <numbers>#include <string_view> auto egamma_aprox(constunsigned iterations){longdouble s{};for(unsigned m{2}; m!= iterations;++m)if(constlongdouble t{std::riemann_zetal(m)/ m}; m%2)            s-= t;else            s+= t;return s;}; int main(){usingnamespace std::numbers;usingnamespace std::string_view_literals; constauto x=std::sqrt(inv_pi)/ inv_sqrtpi+std::ceil(std::exp2(log2e))+ sqrt3* inv_sqrt3+std::exp(0);constauto 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'; constexprauto γ{"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<longdouble>::digits10+1)<< egamma_v<longdouble><<'\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]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/constants&oldid=182822"

      [8]ページ先頭

      ©2009-2025 Movatter.jp