special_function_util.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Special functions -*- C++ -*-
  2. // Copyright (C) 2006-2022 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 tr1/special_function_util.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{tr1/cmath}
  23. */
  24. //
  25. // ISO C++ 14882 TR1: 5.2 Special functions
  26. //
  27. // Written by Edward Smith-Rowland based on numerous mathematics books.
  28. #ifndef _GLIBCXX_TR1_SPECIAL_FUNCTION_UTIL_H
  29. #define _GLIBCXX_TR1_SPECIAL_FUNCTION_UTIL_H 1
  30. namespace std _GLIBCXX_VISIBILITY(default)
  31. {
  32. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  33. #if _GLIBCXX_USE_STD_SPEC_FUNCS
  34. #elif defined(_GLIBCXX_TR1_CMATH)
  35. namespace tr1
  36. {
  37. #else
  38. # error do not include this header directly, use <cmath> or <tr1/cmath>
  39. #endif
  40. namespace __detail
  41. {
  42. /// A class to encapsulate type dependent floating point
  43. /// constants. Not everything will be able to be expressed as
  44. /// type logic.
  45. template<typename _Tp>
  46. struct __floating_point_constant
  47. {
  48. static const _Tp __value;
  49. };
  50. /// A structure for numeric constants.
  51. template<typename _Tp>
  52. struct __numeric_constants
  53. {
  54. /// Constant @f$ \pi @f$.
  55. static _Tp __pi() throw()
  56. { return static_cast<_Tp>(3.1415926535897932384626433832795029L); }
  57. /// Constant @f$ \pi / 2 @f$.
  58. static _Tp __pi_2() throw()
  59. { return static_cast<_Tp>(1.5707963267948966192313216916397514L); }
  60. /// Constant @f$ \pi / 3 @f$.
  61. static _Tp __pi_3() throw()
  62. { return static_cast<_Tp>(1.0471975511965977461542144610931676L); }
  63. /// Constant @f$ \pi / 4 @f$.
  64. static _Tp __pi_4() throw()
  65. { return static_cast<_Tp>(0.7853981633974483096156608458198757L); }
  66. /// Constant @f$ 1 / \pi @f$.
  67. static _Tp __1_pi() throw()
  68. { return static_cast<_Tp>(0.3183098861837906715377675267450287L); }
  69. /// Constant @f$ 2 / \sqrt(\pi) @f$.
  70. static _Tp __2_sqrtpi() throw()
  71. { return static_cast<_Tp>(1.1283791670955125738961589031215452L); }
  72. /// Constant @f$ \sqrt(2) @f$.
  73. static _Tp __sqrt2() throw()
  74. { return static_cast<_Tp>(1.4142135623730950488016887242096981L); }
  75. /// Constant @f$ \sqrt(3) @f$.
  76. static _Tp __sqrt3() throw()
  77. { return static_cast<_Tp>(1.7320508075688772935274463415058723L); }
  78. /// Constant @f$ \sqrt(\pi/2) @f$.
  79. static _Tp __sqrtpio2() throw()
  80. { return static_cast<_Tp>(1.2533141373155002512078826424055226L); }
  81. /// Constant @f$ 1 / sqrt(2) @f$.
  82. static _Tp __sqrt1_2() throw()
  83. { return static_cast<_Tp>(0.7071067811865475244008443621048490L); }
  84. /// Constant @f$ \log(\pi) @f$.
  85. static _Tp __lnpi() throw()
  86. { return static_cast<_Tp>(1.1447298858494001741434273513530587L); }
  87. /// Constant Euler's constant @f$ \gamma_E @f$.
  88. static _Tp __gamma_e() throw()
  89. { return static_cast<_Tp>(0.5772156649015328606065120900824024L); }
  90. /// Constant Euler-Mascheroni @f$ e @f$
  91. static _Tp __euler() throw()
  92. { return static_cast<_Tp>(2.7182818284590452353602874713526625L); }
  93. };
  94. #if _GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
  95. /// This is a wrapper for the isnan function. Otherwise, for NaN,
  96. /// all comparisons result in false. If/when we build a std::isnan
  97. /// out of intrinsics, this will disappear completely in favor of
  98. /// std::isnan.
  99. template<typename _Tp>
  100. inline bool __isnan(_Tp __x)
  101. { return std::isnan(__x); }
  102. #else
  103. template<typename _Tp>
  104. inline bool __isnan(const _Tp __x)
  105. { return __builtin_isnan(__x); }
  106. template<>
  107. inline bool __isnan<float>(float __x)
  108. { return __builtin_isnanf(__x); }
  109. template<>
  110. inline bool __isnan<long double>(long double __x)
  111. { return __builtin_isnanl(__x); }
  112. #endif
  113. } // namespace __detail
  114. #if ! _GLIBCXX_USE_STD_SPEC_FUNCS && defined(_GLIBCXX_TR1_CMATH)
  115. } // namespace tr1
  116. #endif
  117. _GLIBCXX_END_NAMESPACE_VERSION
  118. }
  119. #endif // _GLIBCXX_TR1_SPECIAL_FUNCTION_UTIL_H