Skip to content

Commit 81f9865

Browse files
jensmaurerzygoloid
authored andcommitted
P0631R8 Math constants
1 parent c6900f9 commit 81f9865

File tree

5 files changed

+98
-6
lines changed

5 files changed

+98
-6
lines changed

source/compatibility.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,6 +2157,7 @@
21572157
\tcode{<compare>},
21582158
\tcode{<concepts>},
21592159
\tcode{<coroutine>},
2160+
\tcode{<numbers>},
21602161
\tcode{<ranges>},
21612162
\tcode{<span>},
21622163
\tcode{<syncstream>}, and

source/concepts.tex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,15 @@
167167
template<class T, class U>
168168
concept Common = @\seebelow@;
169169

170-
// \ref{concepts.integral}, integral concepts
170+
// \ref{concepts.arithmetic}, arithmetic concepts
171171
template<class T>
172172
concept Integral = @\seebelow@;
173173
template<class T>
174174
concept SignedIntegral = @\seebelow@;
175175
template<class T>
176176
concept UnsignedIntegral = @\seebelow@;
177+
template<class T>
178+
concept FloatingPoint = @\seebelow@;
177179

178180
// \ref{concept.assignable}, concept \libconcept{Assignable}
179181
template<class LHS, class RHS>
@@ -464,7 +466,7 @@
464466

465467
\end{itemdescr}
466468

467-
\rSec2[concepts.integral]{Integral concepts}
469+
\rSec2[concepts.arithmetic]{Arithmetic concepts}
468470

469471
\indexlibrary{\idxcode{Integral}}%
470472
\indexlibrary{\idxcode{SignedIntegral}}%
@@ -476,6 +478,8 @@
476478
concept SignedIntegral = Integral<T> && is_signed_v<T>;
477479
template<class T>
478480
concept UnsignedIntegral = Integral<T> && !SignedIntegral<T>;
481+
template<class T>
482+
concept FloatingPoint = is_floating_point_v<T>;
479483
\end{itemdecl}
480484

481485
\begin{itemdescr}

source/lib-intro.tex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,7 @@
12491249
\tcode{<mutex>} \\
12501250
\columnbreak
12511251
\tcode{<new>} \\
1252+
\tcode{<numbers>} \\
12521253
\tcode{<numeric>} \\
12531254
\tcode{<optional>} \\
12541255
\tcode{<ostream>} \\
@@ -1265,8 +1266,8 @@
12651266
\tcode{<stack>} \\
12661267
\tcode{<stdexcept>} \\
12671268
\tcode{<streambuf>} \\
1268-
\tcode{<string>} \\
12691269
\columnbreak
1270+
\tcode{<string>} \\
12701271
\tcode{<string_view>} \\
12711272
\tcode{<strstream>} \\
12721273
\tcode{<syncstream>} \\

source/numerics.tex

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
complex number types, random number generation,
1313
numeric (%
1414
\textit{n}-at-a-time)
15-
arrays, generalized numeric algorithms,
16-
and mathematical functions for floating-point types,
15+
arrays, generalized numeric algorithms, and
16+
mathematical constants and functions for floating-point types,
1717
as summarized in \tref{numerics.summary}.
1818

1919
\begin{libsumtab}{Numerics library summary}{numerics.summary}
@@ -24,7 +24,8 @@
2424
\ref{rand} & Random number generation & \tcode{<random>} \\ \rowsep
2525
\ref{numarray} & Numeric arrays & \tcode{<valarray>} \\ \rowsep
2626
\ref{c.math} & Mathematical functions for floating-point types &
27-
\tcode{<cmath>}, \tcode{<cstdlib>} \\
27+
\tcode{<cmath>}, \tcode{<cstdlib>} \\ \rowsep
28+
\ref{numbers} & Numbers & \tcode{<numbers>} \\
2829
\end{libsumtab}
2930

3031
\rSec1[numeric.requirements]{Numeric type requirements}
@@ -10417,3 +10418,86 @@
1041710418
\end{itemdescr}
1041810419

1041910420
\indextext{mathematical special functions|)}
10421+
10422+
\rSec1[numbers]{Numbers}
10423+
10424+
\rSec2[numbers.syn]{Header \tcode{<numbers>} synopsis}
10425+
\indexhdr{numbers}%
10426+
10427+
\begin{codeblock}
10428+
namespace std {
10429+
namespace numbers {
10430+
template<typename T> inline constexpr T e_v = @\unspec@;
10431+
template<typename T> inline constexpr T log2e_v = @\unspec@;
10432+
template<typename T> inline constexpr T log10e_v = @\unspec@;
10433+
template<typename T> inline constexpr T pi_v = @\unspec@;
10434+
template<typename T> inline constexpr T inv_pi_v = @\unspec@;
10435+
template<typename T> inline constexpr T inv_sqrtpi_v = @\unspec@;
10436+
template<typename T> inline constexpr T ln2_v = @\unspec@;
10437+
template<typename T> inline constexpr T ln10_v = @\unspec@;
10438+
template<typename T> inline constexpr T sqrt2_v = @\unspec@;
10439+
template<typename T> inline constexpr T sqrt3_v = @\unspec@;
10440+
template<typename T> inline constexpr T inv_sqrt3_v = @\unspec@;
10441+
template<typename T> inline constexpr T egamma_v = @\unspec@;
10442+
template<typename T> inline constexpr T phi_v = @\unspec@;
10443+
10444+
template<FloatingPoint T> inline constexpr T e_v<T> = @\seebelow@;
10445+
template<FloatingPoint T> inline constexpr T log2e_v<T> = @\seebelow@;
10446+
template<FloatingPoint T> inline constexpr T log10e_v<T> = @\seebelow@;
10447+
template<FloatingPoint T> inline constexpr T pi_v<T> = @\seebelow@;
10448+
template<FloatingPoint T> inline constexpr T inv_pi_v<T> = @\seebelow@;
10449+
template<FloatingPoint T> inline constexpr T inv_sqrtpi_v<T> = @\seebelow@;
10450+
template<FloatingPoint T> inline constexpr T ln2_v<T> = @\seebelow@;
10451+
template<FloatingPoint T> inline constexpr T ln10_v<T> = @\seebelow@;
10452+
template<FloatingPoint T> inline constexpr T sqrt2_v<T> = @\seebelow@;
10453+
template<FloatingPoint T> inline constexpr T sqrt3_v<T> = @\seebelow@;
10454+
template<FloatingPoint T> inline constexpr T inv_sqrt3_v<T> = @\seebelow@;
10455+
template<FloatingPoint T> inline constexpr T egamma_v<T> = @\seebelow@;
10456+
template<FloatingPoint T> inline constexpr T phi_v<T> = @\seebelow@;
10457+
10458+
inline constexpr double e = e_v<double>;
10459+
inline constexpr double log2e = log2e_v<double>;
10460+
inline constexpr double log10e = log10e_v<double>;
10461+
inline constexpr double pi = pi_v<double>;
10462+
inline constexpr double inv_pi = inv_pi_v<double>;
10463+
inline constexpr double inv_sqrtpi = inv_sqrtpi_v<double>;
10464+
inline constexpr double ln2 = ln2_v<double>;
10465+
inline constexpr double ln10 = ln10_v<double>;
10466+
inline constexpr double sqrt2 = sqrt2_v<double>;
10467+
inline constexpr double sqrt3 = sqrt3_v<double>;
10468+
inline constexpr double inv_sqrt3 = inv_sqrt3_v<double>;
10469+
inline constexpr double egmma = egamma_v<double>;
10470+
inline constexpr double phi = phi_v<double>;
10471+
}
10472+
}
10473+
\end{codeblock}
10474+
10475+
\rSec2[math.constants]{Mathematical constants}
10476+
10477+
\pnum
10478+
The library-defined partial specializations of math constant variable templates
10479+
are initialized with the nearest representable values of
10480+
$\mathrm{e}$,
10481+
$\log_{2} \mathrm{e}$,
10482+
$\log_{10} \mathrm{e}$,
10483+
$\pi$,
10484+
$\frac{1}{\pi}$,
10485+
$\frac{1}{\sqrt{\pi}}$,
10486+
$\ln 2$,
10487+
$\ln 10$,
10488+
$\sqrt{2}$,
10489+
$\sqrt{3}$,
10490+
$\frac{1}{\sqrt{3}}$,
10491+
the Euler-Mascheroni $\gamma$ constant, and
10492+
the golden ratio $\phi$ constant $\frac{1+\sqrt{5}}{2}$,
10493+
respectively.
10494+
10495+
\pnum
10496+
Pursuant to \ref{namespace.std},
10497+
a program may partially or explicitly specialize
10498+
a math constant variable template
10499+
provided that the specialization depends on a program-defined type.
10500+
10501+
\pnum
10502+
A program that instantiates a primary template
10503+
of a math constant variable template is ill-formed.

source/support.tex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,8 @@
644644
\tcode{<memory>} \\ \rowsep
645645
\defnlibxname{cpp_lib_map_try_emplace} & \tcode{201411L} &
646646
\tcode{<map>} \\ \rowsep
647+
\defnlibxname{cpp_lib_math_constants} & \tcode{201907L} &
648+
\tcode{<numbers>} \\ \rowsep
647649
\defnlibxname{cpp_lib_math_special_functions} & \tcode{201603L} &
648650
\tcode{<cmath>} \\ \rowsep
649651
\defnlibxname{cpp_lib_memory_resource} & \tcode{201603L} &

0 commit comments

Comments
 (0)