The C++ standard library
Before C++20, we usually wrote mathematical constants like this:
const double PI = 3.14159265358979323846;
The problem is very obvious:Inconsistent precision, easy to get wrong, and not type-safe.</p>
C++20 officially introduced #include
std::numbers is a standard library module introduced in C++20, mainly used to provide a set of commonly used mathematical constants; it is a repository of standard mathematical and physical constants.
std::numbersis located in the header file, and contains many mathematical constants, covering common constants such as pi, the base of the natural logarithm, and the golden ratio.
Using these constants in C++ can improve code readability, precision, and efficiency, avoiding the need to redefine and manually input constant values.
std::numbers Advantages compared with the traditional approach:
| Dimension | Traditional approach | |
|---|---|---|
| Precision | Written by hand | Standard high precision |
| Type | Fixed | Auto-matched |
| Readability | Average | Semantically clear |
| Compile-time constant | Not necessarily | Always |
| Standardization | Poor | Standard-library level |
Constants included in the std::numbers module
The following members are included under the std::numbers namespace:
template inline constexpr T e_v // 自然常数e
template inline constexpr T log2e_v // log2e
template inline constexpr T log10e_v // log10e
template inline constexpr T pi_v // 圆周率π
template inline constexpr T inv_pi_v // 1/π
template inline constexpr T inv_sqrtpi_v //1/根号π
template inline constexpr T ln2_v // ln2
template inline constexpr T ln10_v // ln10
template inline constexpr T sqrt2_v //根号2
template inline constexpr T sqrt3_v //根号3
template inline constexpr T inv_sqrt3_v // 1/根号3
template inline constexpr T egamma_v // 欧拉常数γ
template inline constexpr T phi_v // 黄金分割比Φ
inline constexpr double e = e_v
inline constexpr double log2e = log2e_v
inline constexpr double log10e = log10e_v
inline constexpr double pi = pi_v
inline constexpr double inv_pi = pi_v
inline constexpr double inv_sqrtpi = pi_v
inline constexpr double ln2 = ln2_v
inline constexpr double ln10 = ln10_v
inline constexpr double sqrt2 = sqrt2_v
inline constexpr double sqrt3 = sqrt3_v
inline constexpr double inv_sqrt3 = sqrt3_v
inline constexpr double egamma = egamma_v
inline constexpr double phi = phi_v
The following is a list of constants included in std::numbers:
| Constant / template name | Description | Example value (approximate) |
|---|---|---|
e_v |
Mathematical constant e (base of the natural logarithm) | 2.718281828459045 |
log2e_v |
Base-2 logarithm of e (log2(e)) | 1.4426950408889634 |
log10e_v |
Base-10 logarithm of e (log10(e)) | 0.4342944819032518 |
pi_v |
Mathematical constant pi (pi) | 3.141592653589793 |
inv_pi_v |
1/pi (reciprocal of pi) | 0.318309886183121 |
inv_sqrtpi_v |
1/sqrt(pi) (reciprocal of the square root of pi) | 0.5641895835477563 |
ln2_v |
Base-2 logarithm of the natural log base (ln(2)) | 0.6931471805599453 |
ln10_v |
Base-10 logarithm of the natural log base (ln(10)) | 2.302585092994046 |
sqrt2_v |
sqrt(2) (square root of 2) | 1.4142135623730951 |
sqrt3_v |
sqrt(3) (square root of 3) | 1.7320508075688772 |
inv_sqrt3_v |
1/sqrt(3) (reciprocal of the square root of 3) | 0.5773502691896257 |
egamma_v |
Euler-Mascheroni constant gamma (Euler-Mascheroni constant) | 0.5772156649015329 |
phi_v |
Golden ratio Phi ((1 + sqrt(5)) / 2) | 1.618033988749895 |
e |
Constant e (equivalent to e_v) |
2.718281828459045 |
log2e |
Constant log2(e) (equivalent to log2e_v) |
1.4426950408889634 |
log10e |
Constant log10(e) (equivalent to log10e_v) |
0.4342944819032518 |
pi |
Constant pi (equivalent to pi_v) |
3.141592653589793 |
inv_pi |
Constant 1/pi (equivalent to inv_pi_v) |
0.318309886183121 |
inv_sqrtpi |
Constant 1/sqrt(pi) (equivalent to inv_sqrtpi_v) |
0.5641895835477563 |
ln2 |
Constant ln(2) (equivalent to ln2_v) |
0.6931471805599453 |
ln10 |
Constant ln(10) (equivalent to ln10_v) |
2.302585092994046 |
sqrt2 |
Constant sqrt(2) (equivalent to sqrt2_v) |
1.4142135623730951 |
sqrt3 |
Constant sqrt(3) (equivalent to sqrt3_v) |
1.7320508075688772 |
inv_sqrt3 |
Constant 1/sqrt(3) (equivalent to inv_sqrt3_v) |
0.5773502691896257 |
egamma |
Constant gamma (Euler-Mascheroni constant) (equivalent to egamma_v) |
0.5772156649015329 |
phi |
Constant golden ratio Phi (equivalent to phi_v) |
1.618033988749895 |
These constants and variable templates cover commonly used mathematical constants such as pi, the base of the natural logarithm, and the golden ratio, and provide precision options through different types of variable templates, such as float, double, and long double.
Example
#include
#include
#include
#include
using
namespace
std
;
int
main
(
)
{
// Use the default double-type constant
cout
<<
fixed
<<
setprecision
(
15
)
;
// Set all output to 15 decimal places
// Print the value of pi
cout
<<
"The value of pi is: "
<<
numbers
::
pi
<<
endl
;
// Print the value of the base of the natural logarithm e
cout
<<
"The value of the natural constant e is: "
<<
numbers
::
e
<<
endl
;
// Print the value of sqrt2 (square root of 2)
cout
<<
"The value of sqrt2 (square root of 2) is: "
<<
numbers
::
sqrt2
<<
endl
;
// Print the value of sqrt3 (square root of 3)
cout
<<
"The value of sqrt3 (square root of 3) is: "
<<
numbers
::
sqrt3
<<
endl
;
// Print the value of the golden ratio phi
cout
<<
"The value of the golden ratio phi is: "
<<
numbers
::
phi
<<
endl
;
// Print the value of the Euler-Mascheroni constant egamma
cout
<<
"The value of the Euler-Mascheroni constant egamma is: "
<<
numbers
::
egamma
<<
endl
;
// Print the value of 1/pi (inv_pi)
cout
<<
"The value of 1/pi (inv_pi) is: "
<<
numbers
::
inv_pi
<<
endl
;
// Print the value of 1/sqrt(pi) (inv_sqrtpi)
cout
<<
"The value of 1/sqrt(pi) (inv_sqrtpi) is: "
<<
numbers
::
inv_sqrtpi
<<
endl
;
// Print the value of the base-2 logarithm of e, log2e
cout
<<
"The value of log2e (base-2 log of e) is: "
<<
numbers
::
log2e
<<
endl
;
// Print the value of the base-10 logarithm of e, log10e
cout
<<
"The value of log10e (base-10 log of e) is: "
<<
numbers
::
log10e
<<
endl
;
// Print the value of sin(pi/2)
cout
<<
"The value of sin(pi/2) is: "
<<
sin
(
numbers
::
pi
/
2
)
<<
endl
;
// Print the value of cos(pi)
cout
<<
"The value of cos(pi) is: "
<<
cos
(
numbers
::
pi
)
<<
endl
;
// Print the value of ln(e)
cout
<<
"The value of ln(e) is: "
<<
log
(
numbers
::
e
)
<<
endl
;
// Demonstrate using different data types
float
pi_f
=
numbers
::
pi_v
<
float
;
// Use the float-type pi
cout
<<
"\nUsing float-type pi: "
<<
setprecision
(
7
)
<<
pi_f
<<
endl
;
// Output 7 decimal places
long
double
pi_ld
=
numbers
::
pi_v
<
long
double;
// Use the long double-type pi
cout
<<
"Using long double-type pi: "
<<
setprecision
(
21
)
<<
pi_ld
<<
endl
;
// Output 21 decimal places
// Calculate values at different precisions
cout
<<
"\nsin(pi/2) using double: "
<<
sin
(
numbers
::
pi
/
2
)
<<
endl
;
cout
<<
"sin(pi/2) using float: "
<<
sin
(
static_cast
<
float
(
numbers::
pi
)
/
2
)
<<
endl
;
cout
<<
"sin(pi/2) using long double: "
<<
sin
(
static_cast
<
long
double
(
numbers::
pi
)
/
2
)
<<
endl
;
return
0
;
}
Code analysis:
Formatted output:
- Use
fixedandsetprecision()to ensure a fixed number of decimal places in the output. Here 15 decimal places are output by default, but different precisions can be set for each different data type.
Calculate common math functions:
- calculates
sin(π/2),cos(π)andln(e)and other common math functions, and usescmathfunctions from (such assin,cosandlog) to demonstrate how to use mathematical constants.
Precision of different data types:
- Use
numbers::pi_vandnumbers::pi_vto show constants of different types. Forfloatandlong double, different precisions can be used for output.
Calculations using different types:
- when calculating
sin, it shows how to perform calculations with types of different precision, demonstrating thefloat,doubleandlong doubledifferences under the same mathematical expression.
Output:
圆周率 pi 的值是: 3.141592653589793
自然常数 e 的值是: 2.718281828459045
根号2 (sqrt2) 的值是: 1.414213562373095
根号3 (sqrt3) 的值是: 1.732050807568877
黄金比例 phi 的值是: 1.618033988749895
欧拉-马歇罗尼常数 egamma 的值是: 0.5772156649015329
1/π (inv_pi) 的值是: 0.318309886183121
1/√π (inv_sqrtpi) 的值是: 0.5641895835477563
以 2 为底的 e 的对数 log2e 的值是: 1.4426950408889634
以 10 为底的 e 的对数 log10e 的值是: 0.4342944819032518
sin(π/2) 的值是: 1
cos(π) 的值是: -1
ln(e) 的值是: 1
使用 float 类型的 pi: 3.1415927
使用 long double 类型的 pi: 3.141592653589793238462643
sin(π/2) 使用 double: 1
sin(π/2) 使用 float: 1
sin(π/2) 使用 long double: 1
Usage examples
The following examples cover
g++ -std=c++20 main.cpp -o main
Example 1: Basic usage
Using pi to calculate the area / circumference of a circle:
Example
#include
#include
#include
using
namespace
std
;
using
namespace
std
::
numbers
;
// Simplify constant calls (optional)
int
main
(
)
{
// Radius of the circle
double
r
=
5.0
;
// 1. Calculate the circumference: 2 * pi * r
double
circumference
=
2
pi
r
;
// Use the double-version pi directly
cout
<<
"Radius is"
<<
r
<<
" circumference:"
<<
circumference
<<
endl
;
// Output: 31.4159
// 2. Calculate the area: pi * r squared
double
area
=
pi
pow
(
r,
2
)
;
cout
<<
"Radius is"
<<
r
<<
" area:"
<<
area
<<
endl
;
// Output: 78.5398
// 3. High-precision version (long double)
long
double
r_ld
=
5.0L
;
long
double
area_ld
=
pi_v
<
long
double
powl
(
r_ld,
2
);
// powl is the long double version of pow
cout
<<
"High-precision area:"
<<
area_ld
<<
endl
;
// Outputs the more precise 78.53981633974483...
return
0
;
}
Example 2: Constant e
The natural constant e and exponential calculation:
Example
#include
#include
#include
using
namespace
std
;
using
namespace
std
::
numbers
;
int
main
(
)
{
// Natural exponential calculation: e^x (the exp function is the exponential function in cmath)
double
x
=
2.0
;
double
exp_result
=
exp
(
x
)
;
// Manually calculate e^2
double
exp_result_direct
=
pow
(
e, x
)
;
// Calculate e^2 using numbers::e
cout
<<
"e^2 = "
<<
exp_result
<<
endl
;
// Output: 7.38906
cout
<<
"e^2 (direct calculation) = "
<<
exp_result_direct
<<
endl
;
// Results are consistent
return
0
;
}
Example 3: Golden ratio
Application of the golden ratio phi:
Example
#include
#include
using
namespace
std
;
using
namespace
std
::
numbers
;
int
main
(
)
{
// Golden ratio: a golden rectangle has length/width = phi
double
width
=
10.0
;
double
golden_height
=
width
phi
;
// Height of the golden rectangle
cout
<<
"Width is"
<<
width
<<
" golden rectangle height:"
<<
golden_height
<<
endl
;
// Output: 16.1803
// Verify the mathematical definition of phi: phi = (1 + sqrt(5))/2
double
phi_manual
=
(
1
sqrt
(
5.0
)
)
/
2
;
cout
<<
"Manually calculated golden ratio:"
<<
phi_manual
<<
endl
;
// Consistent with numbers::phi
return
0
;
}
Example 4: Floating-point types
Constant adaptation for different floating-point types:
Example
#include
#include
#include
using
namespace
std
;
int
main
(
)
{
// Set high-precision output (show 15 decimal places)
cout
<<
fixed
<<
setprecision
(
15
)
;
// float-version pi (single precision)
float
pi_float
=
std
::
numbers
::
pi_v
<
float
;
cout
<<
"float-version pi:"
<<
pi_float
<<
endl
;
// Output: 3.141592741012573
// double-version pi (double precision, default)
double
pi_double
=
std
::
numbers
::
pi
;
cout
<<
"double-version pi:"
<<
pi_double
<<
endl
;
// Output: 3.141592653589793
// long double-version pi (high precision)
long
double
pi_long_double
=
std
::
numbers
::
pi_v
<
long
double;
cout
<<
"long double-version pi:"
<<
pi_long_double
<<
endl
;
// Outputs the more precise 3.1415926535897932385...
return
0
;
}