libstdc++
numbers
Go to the documentation of this file.
1// <numbers> -*- C++ -*-
2
3// Copyright (C) 2019-2022 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file include/numbers
26 * This is a Standard C++ Library header.
27 */
28
29#ifndef _GLIBCXX_NUMBERS
30#define _GLIBCXX_NUMBERS 1
31
32#pragma GCC system_header
33
34#if __cplusplus > 201703L
35
36#include <type_traits>
37
38namespace std _GLIBCXX_VISIBILITY(default)
39{
40_GLIBCXX_BEGIN_NAMESPACE_VERSION
41
42/** @defgroup math_constants Mathematical constants
43 * @ingroup numerics
44 * @{
45 */
46
47/// Namespace for mathematical constants
48namespace numbers
49{
50#define __cpp_lib_math_constants 201907L
51
52 /// @cond undoc
53 template<typename _Tp>
54 using _Enable_if_floating = enable_if_t<is_floating_point_v<_Tp>, _Tp>;
55 /// @endcond
56
57 /// e
58 template<typename _Tp>
59 inline constexpr _Tp e_v
60 = _Enable_if_floating<_Tp>(2.718281828459045235360287471352662498L);
61
62 /// log_2 e
63 template<typename _Tp>
64 inline constexpr _Tp log2e_v
65 = _Enable_if_floating<_Tp>(1.442695040888963407359924681001892137L);
66
67 /// log_10 e
68 template<typename _Tp>
69 inline constexpr _Tp log10e_v
70 = _Enable_if_floating<_Tp>(0.434294481903251827651128918916605082L);
71
72 /// pi
73 template<typename _Tp>
74 inline constexpr _Tp pi_v
75 = _Enable_if_floating<_Tp>(3.141592653589793238462643383279502884L);
76
77 /// 1/pi
78 template<typename _Tp>
79 inline constexpr _Tp inv_pi_v
80 = _Enable_if_floating<_Tp>(0.318309886183790671537767526745028724L);
81
82 /// 1/sqrt(pi)
83 template<typename _Tp>
84 inline constexpr _Tp inv_sqrtpi_v
85 = _Enable_if_floating<_Tp>(0.564189583547756286948079451560772586L);
86
87 /// log_e 2
88 template<typename _Tp>
89 inline constexpr _Tp ln2_v
90 = _Enable_if_floating<_Tp>(0.693147180559945309417232121458176568L);
91
92 /// log_e 10
93 template<typename _Tp>
94 inline constexpr _Tp ln10_v
95 = _Enable_if_floating<_Tp>(2.302585092994045684017991454684364208L);
96
97 /// sqrt(2)
98 template<typename _Tp>
99 inline constexpr _Tp sqrt2_v
100 = _Enable_if_floating<_Tp>(1.414213562373095048801688724209698079L);
101
102 /// sqrt(3)
103 template<typename _Tp>
104 inline constexpr _Tp sqrt3_v
105 = _Enable_if_floating<_Tp>(1.732050807568877293527446341505872367L);
106
107 /// 1/sqrt(3)
108 template<typename _Tp>
109 inline constexpr _Tp inv_sqrt3_v
110 = _Enable_if_floating<_Tp>(0.577350269189625764509148780501957456L);
111
112 /// The Euler-Mascheroni constant
113 template<typename _Tp>
114 inline constexpr _Tp egamma_v
115 = _Enable_if_floating<_Tp>(0.577215664901532860606512090082402431L);
116
117 /// The golden ratio, (1+sqrt(5))/2
118 template<typename _Tp>
119 inline constexpr _Tp phi_v
120 = _Enable_if_floating<_Tp>(1.618033988749894848204586834365638118L);
121
122 inline constexpr double e = e_v<double>;
123 inline constexpr double log2e = log2e_v<double>;
124 inline constexpr double log10e = log10e_v<double>;
125 inline constexpr double pi = pi_v<double>;
126 inline constexpr double inv_pi = inv_pi_v<double>;
127 inline constexpr double inv_sqrtpi = inv_sqrtpi_v<double>;
128 inline constexpr double ln2 = ln2_v<double>;
129 inline constexpr double ln10 = ln10_v<double>;
130 inline constexpr double sqrt2 = sqrt2_v<double>;
131 inline constexpr double sqrt3 = sqrt3_v<double>;
132 inline constexpr double inv_sqrt3 = inv_sqrt3_v<double>;
133 inline constexpr double egamma = egamma_v<double>;
134 inline constexpr double phi = phi_v<double>;
135
136#if !defined(__STRICT_ANSI__) && defined(_GLIBCXX_USE_FLOAT128)
137 template<>
138 inline constexpr __float128 e_v<__float128>
139 = 2.718281828459045235360287471352662498Q;
140
141 /// log_2 e
142 template<>
143 inline constexpr __float128 log2e_v<__float128>
144 = 1.442695040888963407359924681001892137Q;
145
146 /// log_10 e
147 template<>
148 inline constexpr __float128 log10e_v<__float128>
149 = 0.434294481903251827651128918916605082Q;
150
151 /// pi
152 template<>
153 inline constexpr __float128 pi_v<__float128>
154 = 3.141592653589793238462643383279502884Q;
155
156 /// 1/pi
157 template<>
158 inline constexpr __float128 inv_pi_v<__float128>
159 = 0.318309886183790671537767526745028724Q;
160
161 /// 1/sqrt(pi)
162 template<>
163 inline constexpr __float128 inv_sqrtpi_v<__float128>
164 = 0.564189583547756286948079451560772586Q;
165
166 /// log_e 2
167 template<>
168 inline constexpr __float128 ln2_v<__float128>
169 = 0.693147180559945309417232121458176568Q;
170
171 /// log_e 10
172 template<>
173 inline constexpr __float128 ln10_v<__float128>
174 = 2.302585092994045684017991454684364208Q;
175
176 /// sqrt(2)
177 template<>
178 inline constexpr __float128 sqrt2_v<__float128>
179 = 1.414213562373095048801688724209698079Q;
180
181 /// sqrt(3)
182 template<>
183 inline constexpr __float128 sqrt3_v<__float128>
184 = 1.732050807568877293527446341505872367Q;
185
186 /// 1/sqrt(3)
187 template<>
188 inline constexpr __float128 inv_sqrt3_v<__float128>
189 = 0.577350269189625764509148780501957456Q;
190
191 /// The Euler-Mascheroni constant
192 template<>
193 inline constexpr __float128 egamma_v<__float128>
194 = 0.577215664901532860606512090082402431Q;
195
196 /// The golden ratio, (1+sqrt(5))/2
197 template<>
198 inline constexpr __float128 phi_v<__float128>
199 = 1.618033988749894848204586834365638118Q;
200#endif // USE_FLOAT128
201
202} // namespace numbers
203/// @}
204_GLIBCXX_END_NAMESPACE_VERSION
205} // namespace std
206
207#endif // C++20
208#endif // _GLIBCXX_NUMBERS