cmath 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Math extensions -*- C++ -*-
  2. // Copyright (C) 2013-2023 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 ext/cmath
  21. * This file is a GNU extension to the Standard C++ Library.
  22. */
  23. #ifndef _EXT_CMATH
  24. #define _EXT_CMATH 1
  25. #pragma GCC system_header
  26. #include <bits/requires_hosted.h> // GNU extensions are currently omitted
  27. #if __cplusplus < 201103L
  28. # include <bits/c++0x_warning.h>
  29. #else
  30. #include <cmath>
  31. #include <type_traits>
  32. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  33. {
  34. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  35. // A class for math constants.
  36. template<typename _RealType>
  37. struct __math_constants
  38. {
  39. static_assert(std::is_floating_point<_RealType>::value,
  40. "template argument not a floating point type");
  41. // Constant @f$ \pi @f$.
  42. static constexpr _RealType __pi = 3.1415926535897932384626433832795029L;
  43. // Constant @f$ \pi / 2 @f$.
  44. static constexpr _RealType __pi_half = 1.5707963267948966192313216916397514L;
  45. // Constant @f$ \pi / 3 @f$.
  46. static constexpr _RealType __pi_third = 1.0471975511965977461542144610931676L;
  47. // Constant @f$ \pi / 4 @f$.
  48. static constexpr _RealType __pi_quarter = 0.7853981633974483096156608458198757L;
  49. // Constant @f$ \sqrt(\pi / 2) @f$.
  50. static constexpr _RealType __root_pi_div_2 = 1.2533141373155002512078826424055226L;
  51. // Constant @f$ 1 / \pi @f$.
  52. static constexpr _RealType __one_div_pi = 0.3183098861837906715377675267450287L;
  53. // Constant @f$ 2 / \pi @f$.
  54. static constexpr _RealType __two_div_pi = 0.6366197723675813430755350534900574L;
  55. // Constant @f$ 2 / \sqrt(\pi) @f$.
  56. static constexpr _RealType __two_div_root_pi = 1.1283791670955125738961589031215452L;
  57. // Constant Euler's number @f$ e @f$.
  58. static constexpr _RealType __e = 2.7182818284590452353602874713526625L;
  59. // Constant @f$ 1 / e @f$.
  60. static constexpr _RealType __one_div_e = 0.36787944117144232159552377016146087L;
  61. // Constant @f$ \log_2(e) @f$.
  62. static constexpr _RealType __log2_e = 1.4426950408889634073599246810018921L;
  63. // Constant @f$ \log_10(e) @f$.
  64. static constexpr _RealType __log10_e = 0.4342944819032518276511289189166051L;
  65. // Constant @f$ \ln(2) @f$.
  66. static constexpr _RealType __ln_2 = 0.6931471805599453094172321214581766L;
  67. // Constant @f$ \ln(3) @f$.
  68. static constexpr _RealType __ln_3 = 1.0986122886681096913952452369225257L;
  69. // Constant @f$ \ln(10) @f$.
  70. static constexpr _RealType __ln_10 = 2.3025850929940456840179914546843642L;
  71. // Constant Euler-Mascheroni @f$ \gamma_E @f$.
  72. static constexpr _RealType __gamma_e = 0.5772156649015328606065120900824024L;
  73. // Constant Golden Ratio @f$ \phi @f$.
  74. static constexpr _RealType __phi = 1.6180339887498948482045868343656381L;
  75. // Constant @f$ \sqrt(2) @f$.
  76. static constexpr _RealType __root_2 = 1.4142135623730950488016887242096981L;
  77. // Constant @f$ \sqrt(3) @f$.
  78. static constexpr _RealType __root_3 = 1.7320508075688772935274463415058724L;
  79. // Constant @f$ \sqrt(5) @f$.
  80. static constexpr _RealType __root_5 = 2.2360679774997896964091736687312762L;
  81. // Constant @f$ \sqrt(7) @f$.
  82. static constexpr _RealType __root_7 = 2.6457513110645905905016157536392604L;
  83. // Constant @f$ 1 / \sqrt(2) @f$.
  84. static constexpr _RealType __one_div_root_2 = 0.7071067811865475244008443621048490L;
  85. };
  86. // And the template definitions for the constants.
  87. template<typename _RealType>
  88. constexpr _RealType __math_constants<_RealType>::__pi;
  89. template<typename _RealType>
  90. constexpr _RealType __math_constants<_RealType>::__pi_half;
  91. template<typename _RealType>
  92. constexpr _RealType __math_constants<_RealType>::__pi_third;
  93. template<typename _RealType>
  94. constexpr _RealType __math_constants<_RealType>::__pi_quarter;
  95. template<typename _RealType>
  96. constexpr _RealType __math_constants<_RealType>::__root_pi_div_2;
  97. template<typename _RealType>
  98. constexpr _RealType __math_constants<_RealType>::__one_div_pi;
  99. template<typename _RealType>
  100. constexpr _RealType __math_constants<_RealType>::__two_div_pi;
  101. template<typename _RealType>
  102. constexpr _RealType __math_constants<_RealType>::__two_div_root_pi;
  103. template<typename _RealType>
  104. constexpr _RealType __math_constants<_RealType>::__e;
  105. template<typename _RealType>
  106. constexpr _RealType __math_constants<_RealType>::__one_div_e;
  107. template<typename _RealType>
  108. constexpr _RealType __math_constants<_RealType>::__log2_e;
  109. template<typename _RealType>
  110. constexpr _RealType __math_constants<_RealType>::__log10_e;
  111. template<typename _RealType>
  112. constexpr _RealType __math_constants<_RealType>::__ln_2;
  113. template<typename _RealType>
  114. constexpr _RealType __math_constants<_RealType>::__ln_3;
  115. template<typename _RealType>
  116. constexpr _RealType __math_constants<_RealType>::__ln_10;
  117. template<typename _RealType>
  118. constexpr _RealType __math_constants<_RealType>::__gamma_e;
  119. template<typename _RealType>
  120. constexpr _RealType __math_constants<_RealType>::__phi;
  121. template<typename _RealType>
  122. constexpr _RealType __math_constants<_RealType>::__root_2;
  123. template<typename _RealType>
  124. constexpr _RealType __math_constants<_RealType>::__root_3;
  125. template<typename _RealType>
  126. constexpr _RealType __math_constants<_RealType>::__root_5;
  127. template<typename _RealType>
  128. constexpr _RealType __math_constants<_RealType>::__root_7;
  129. template<typename _RealType>
  130. constexpr _RealType __math_constants<_RealType>::__one_div_root_2;
  131. _GLIBCXX_END_NAMESPACE_VERSION
  132. } // namespace __gnu_cxx
  133. #endif // C++11
  134. #endif // _EXT_CMATH