big_constant.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) 2011 John Maddock
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_TOOLS_BIG_CONSTANT_HPP
  6. #define BOOST_MATH_TOOLS_BIG_CONSTANT_HPP
  7. #include <boost/math/tools/config.hpp>
  8. // On NVRTC we don't need any of this
  9. // We just have a simple definition of the macro since the largest float
  10. // type on the platform is a 64-bit double
  11. #ifndef BOOST_MATH_HAS_NVRTC
  12. #ifndef BOOST_MATH_STANDALONE
  13. #include <boost/lexical_cast.hpp>
  14. #endif
  15. #include <cstdlib>
  16. #include <type_traits>
  17. #include <limits>
  18. namespace boost{ namespace math{
  19. namespace tools{
  20. template <class T>
  21. struct numeric_traits : public std::numeric_limits< T > {};
  22. #ifdef BOOST_MATH_USE_FLOAT128
  23. typedef __float128 largest_float;
  24. #define BOOST_MATH_LARGEST_FLOAT_C(x) x##Q
  25. template <>
  26. struct numeric_traits<__float128>
  27. {
  28. static const int digits = 113;
  29. static const int digits10 = 33;
  30. static const int max_exponent = 16384;
  31. static const bool is_specialized = true;
  32. };
  33. #elif LDBL_DIG > DBL_DIG
  34. typedef long double largest_float;
  35. #define BOOST_MATH_LARGEST_FLOAT_C(x) x##L
  36. #else
  37. typedef double largest_float;
  38. #define BOOST_MATH_LARGEST_FLOAT_C(x) x
  39. #endif
  40. template <class T>
  41. BOOST_MATH_GPU_ENABLED constexpr T make_big_value(largest_float v, const char*, std::true_type const&, std::false_type const&) BOOST_MATH_NOEXCEPT(T)
  42. {
  43. return static_cast<T>(v);
  44. }
  45. template <class T>
  46. BOOST_MATH_GPU_ENABLED constexpr T make_big_value(largest_float v, const char*, std::true_type const&, std::true_type const&) BOOST_MATH_NOEXCEPT(T)
  47. {
  48. return static_cast<T>(v);
  49. }
  50. #ifndef BOOST_MATH_NO_LEXICAL_CAST
  51. template <class T>
  52. inline T make_big_value(largest_float, const char* s, std::false_type const&, std::false_type const&)
  53. {
  54. return boost::lexical_cast<T>(s);
  55. }
  56. #else
  57. template <typename T>
  58. inline T make_big_value(largest_float, const char*, std::false_type const&, std::false_type const&)
  59. {
  60. static_assert(sizeof(T) == 0, "Type is unsupported in standalone mode. Please disable and try again.");
  61. }
  62. #endif
  63. template <class T>
  64. inline constexpr T make_big_value(largest_float, const char* s, std::false_type const&, std::true_type const&) BOOST_MATH_NOEXCEPT(T)
  65. {
  66. return T(s);
  67. }
  68. //
  69. // For constants which might fit in a long double (if it's big enough):
  70. //
  71. // Note that gcc-13 has std::is_convertible<long double, std::float64_t>::value false, likewise
  72. // std::is_constructible<std::float64_t, long double>::value, even though the conversions do
  73. // actually work. Workaround is the || std::is_floating_point<T>::value part which thankfully is true.
  74. //
  75. #define BOOST_MATH_BIG_CONSTANT(T, D, x)\
  76. boost::math::tools::make_big_value<T>(\
  77. BOOST_MATH_LARGEST_FLOAT_C(x), \
  78. BOOST_MATH_STRINGIZE(x), \
  79. std::integral_constant<bool, (std::is_convertible<boost::math::tools::largest_float, T>::value || std::is_floating_point<T>::value) && \
  80. ((D <= boost::math::tools::numeric_traits<boost::math::tools::largest_float>::digits) \
  81. || std::is_floating_point<T>::value \
  82. || (boost::math::tools::numeric_traits<T>::is_specialized && \
  83. (boost::math::tools::numeric_traits<T>::digits10 <= boost::math::tools::numeric_traits<boost::math::tools::largest_float>::digits10))) >(), \
  84. std::is_constructible<T, const char*>())
  85. //
  86. // For constants too huge for any conceivable long double (and which generate compiler errors if we try and declare them as such):
  87. //
  88. #define BOOST_MATH_HUGE_CONSTANT(T, D, x)\
  89. boost::math::tools::make_big_value<T>(0.0L, BOOST_MATH_STRINGIZE(x), \
  90. std::integral_constant<bool, std::is_floating_point<T>::value || (boost::math::tools::numeric_traits<T>::is_specialized && boost::math::tools::numeric_traits<T>::max_exponent <= boost::math::tools::numeric_traits<boost::math::tools::largest_float>::max_exponent && boost::math::tools::numeric_traits<T>::digits <= boost::math::tools::numeric_traits<boost::math::tools::largest_float>::digits)>(), \
  91. std::is_constructible<T, const char*>())
  92. }}} // namespaces
  93. #endif // BOOST_MATH_HAS_NVRTC
  94. #endif