You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
6.1KB

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