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