libstdc++
numbers
Go to the documentation of this file.
1 // <numbers> -*- C++ -*-
2 
3 // Copyright (C) 2019-2025 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 #ifdef _GLIBCXX_SYSHDR
33 #pragma GCC system_header
34 #endif
35 
36 #define __glibcxx_want_math_constants
37 #include <bits/version.h>
38 
39 #ifdef __cpp_lib_math_constants // C++ >= 20
40 
41 #include <type_traits>
42 
43 #pragma GCC diagnostic push
44 #pragma GCC diagnostic ignored "-Wpedantic" // Q extension
45 
46 namespace std _GLIBCXX_VISIBILITY(default)
47 {
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49 
50 /** @defgroup math_constants Mathematical constants
51  * @ingroup numerics
52  * @{
53  */
54 
55 /// Namespace for mathematical constants
56 namespace numbers
57 {
58 
59  /// @cond undocumented
60  template<typename _Tp>
61  using _Enable_if_floating = enable_if_t<is_floating_point_v<_Tp>, _Tp>;
62  /// @endcond
63 
64  /// e
65  template<typename _Tp>
66  inline constexpr _Tp e_v
67  = _Enable_if_floating<_Tp>(2.718281828459045235360287471352662498L);
68 
69  /// log_2 e
70  template<typename _Tp>
71  inline constexpr _Tp log2e_v
72  = _Enable_if_floating<_Tp>(1.442695040888963407359924681001892137L);
73 
74  /// log_10 e
75  template<typename _Tp>
76  inline constexpr _Tp log10e_v
77  = _Enable_if_floating<_Tp>(0.434294481903251827651128918916605082L);
78 
79  /// pi
80  template<typename _Tp>
81  inline constexpr _Tp pi_v
82  = _Enable_if_floating<_Tp>(3.141592653589793238462643383279502884L);
83 
84  /// 1/pi
85  template<typename _Tp>
86  inline constexpr _Tp inv_pi_v
87  = _Enable_if_floating<_Tp>(0.318309886183790671537767526745028724L);
88 
89  /// 1/sqrt(pi)
90  template<typename _Tp>
91  inline constexpr _Tp inv_sqrtpi_v
92  = _Enable_if_floating<_Tp>(0.564189583547756286948079451560772586L);
93 
94  /// log_e 2
95  template<typename _Tp>
96  inline constexpr _Tp ln2_v
97  = _Enable_if_floating<_Tp>(0.693147180559945309417232121458176568L);
98 
99  /// log_e 10
100  template<typename _Tp>
101  inline constexpr _Tp ln10_v
102  = _Enable_if_floating<_Tp>(2.302585092994045684017991454684364208L);
103 
104  /// sqrt(2)
105  template<typename _Tp>
106  inline constexpr _Tp sqrt2_v
107  = _Enable_if_floating<_Tp>(1.414213562373095048801688724209698079L);
108 
109  /// sqrt(3)
110  template<typename _Tp>
111  inline constexpr _Tp sqrt3_v
112  = _Enable_if_floating<_Tp>(1.732050807568877293527446341505872367L);
113 
114  /// 1/sqrt(3)
115  template<typename _Tp>
116  inline constexpr _Tp inv_sqrt3_v
117  = _Enable_if_floating<_Tp>(0.577350269189625764509148780501957456L);
118 
119  /// The Euler-Mascheroni constant
120  template<typename _Tp>
121  inline constexpr _Tp egamma_v
122  = _Enable_if_floating<_Tp>(0.577215664901532860606512090082402431L);
123 
124  /// The golden ratio, (1+sqrt(5))/2
125  template<typename _Tp>
126  inline constexpr _Tp phi_v
127  = _Enable_if_floating<_Tp>(1.618033988749894848204586834365638118L);
128 
129  inline constexpr double e = e_v<double>;
130  inline constexpr double log2e = log2e_v<double>;
131  inline constexpr double log10e = log10e_v<double>;
132  inline constexpr double pi = pi_v<double>;
133  inline constexpr double inv_pi = inv_pi_v<double>;
134  inline constexpr double inv_sqrtpi = inv_sqrtpi_v<double>;
135  inline constexpr double ln2 = ln2_v<double>;
136  inline constexpr double ln10 = ln10_v<double>;
137  inline constexpr double sqrt2 = sqrt2_v<double>;
138  inline constexpr double sqrt3 = sqrt3_v<double>;
139  inline constexpr double inv_sqrt3 = inv_sqrt3_v<double>;
140  inline constexpr double egamma = egamma_v<double>;
141  inline constexpr double phi = phi_v<double>;
142 
143 #define __glibcxx_numbers(TYPE, SUFFIX) \
144  /* e */ \
145  template<> \
146  inline constexpr TYPE e_v<TYPE> \
147  = 2.718281828459045235360287471352662498##SUFFIX; \
148  \
149  /* log_2 e */ \
150  template<> \
151  inline constexpr TYPE log2e_v<TYPE> \
152  = 1.442695040888963407359924681001892137##SUFFIX; \
153  \
154  /* log_10 e */ \
155  template<> \
156  inline constexpr TYPE log10e_v<TYPE> \
157  = 0.434294481903251827651128918916605082##SUFFIX; \
158  \
159  /* pi */ \
160  template<> \
161  inline constexpr TYPE pi_v<TYPE> \
162  = 3.141592653589793238462643383279502884##SUFFIX; \
163  \
164  /* 1/pi */ \
165  template<> \
166  inline constexpr TYPE inv_pi_v<TYPE> \
167  = 0.318309886183790671537767526745028724##SUFFIX; \
168  \
169  /* 1/sqrt(pi) */ \
170  template<> \
171  inline constexpr TYPE inv_sqrtpi_v<TYPE> \
172  = 0.564189583547756286948079451560772586##SUFFIX; \
173  \
174  /* log_e 2 */ \
175  template<> \
176  inline constexpr TYPE ln2_v<TYPE> \
177  = 0.693147180559945309417232121458176568##SUFFIX; \
178  \
179  /* log_e 10 */ \
180  template<> \
181  inline constexpr TYPE ln10_v<TYPE> \
182  = 2.302585092994045684017991454684364208##SUFFIX; \
183  \
184  /* sqrt(2) */ \
185  template<> \
186  inline constexpr TYPE sqrt2_v<TYPE> \
187  = 1.414213562373095048801688724209698079##SUFFIX; \
188  \
189  /* sqrt(3) */ \
190  template<> \
191  inline constexpr TYPE sqrt3_v<TYPE> \
192  = 1.732050807568877293527446341505872367##SUFFIX; \
193  \
194  /* 1/sqrt(3) */ \
195  template<> \
196  inline constexpr TYPE inv_sqrt3_v<TYPE> \
197  = 0.577350269189625764509148780501957456##SUFFIX; \
198  \
199  /* The Euler-Mascheroni constant */ \
200  template<> \
201  inline constexpr TYPE egamma_v<TYPE> \
202  = 0.577215664901532860606512090082402431##SUFFIX; \
203  \
204  /* The golden ratio, (1+sqrt(5))/2 */ \
205  template<> \
206  inline constexpr TYPE phi_v<TYPE> \
207  = 1.618033988749894848204586834365638118##SUFFIX
208 
209 #ifdef __STDCPP_FLOAT16_T__
210 __glibcxx_numbers (_Float16, F16);
211 #endif
212 
213 #ifdef __STDCPP_FLOAT32_T__
214 __glibcxx_numbers (_Float32, F32);
215 #endif
216 
217 #ifdef __STDCPP_FLOAT64_T__
218 __glibcxx_numbers (_Float64, F64);
219 #endif
220 
221 #ifdef __STDCPP_FLOAT128_T__
222 __glibcxx_numbers (_Float128, F128);
223 #endif
224 
225 #ifdef __STDCPP_BFLOAT128_T__
226 __glibcxx_numbers (__gnu_cxx::__bfloat16_t, BF16);
227 #endif
228 
229 #if !defined(__STRICT_ANSI__) && defined(_GLIBCXX_USE_FLOAT128)
230 __glibcxx_numbers (__float128, Q);
231 #endif // USE_FLOAT128
232 
233 #undef __glibcxx_numbers
234 
235 } // namespace numbers
236 /// @}
237 _GLIBCXX_END_NAMESPACE_VERSION
238 } // namespace std
239 
240 #pragma GCC diagnostic pop
241 
242 #endif // __cpp_lib_math_constants
243 #endif // _GLIBCXX_NUMBERS