big_constant.hpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #ifndef BOOST_MATH_STANDALONE
  9. #include <boost/lexical_cast.hpp>
  10. #endif
  11. #include <cstdlib>
  12. #include <type_traits>
  13. #include <limits>
  14. namespace boost{ namespace math{
  15. namespace tools{
  16. template <class T>
  17. struct numeric_traits : public std::numeric_limits< T > {};
  18. #ifdef BOOST_MATH_USE_FLOAT128
  19. typedef __float128 largest_float;
  20. #define BOOST_MATH_LARGEST_FLOAT_C(x) x##Q
  21. template <>
  22. struct numeric_traits<__float128>
  23. {
  24. static const int digits = 113;
  25. static const int digits10 = 33;
  26. static const int max_exponent = 16384;
  27. static const bool is_specialized = true;
  28. };
  29. #elif LDBL_DIG > DBL_DIG
  30. typedef long double largest_float;
  31. #define BOOST_MATH_LARGEST_FLOAT_C(x) x##L
  32. #else
  33. typedef double largest_float;
  34. #define BOOST_MATH_LARGEST_FLOAT_C(x) x
  35. #endif
  36. template <class T>
  37. inline constexpr T make_big_value(largest_float v, const char*, std::true_type const&, std::false_type const&) BOOST_MATH_NOEXCEPT(T)
  38. {
  39. return static_cast<T>(v);
  40. }
  41. template <class T>
  42. inline constexpr T make_big_value(largest_float v, const char*, std::true_type const&, std::true_type const&) BOOST_MATH_NOEXCEPT(T)
  43. {
  44. return static_cast<T>(v);
  45. }
  46. #ifndef BOOST_MATH_NO_LEXICAL_CAST
  47. template <class T>
  48. inline T make_big_value(largest_float, const char* s, std::false_type const&, std::false_type const&)
  49. {
  50. return boost::lexical_cast<T>(s);
  51. }
  52. #else
  53. template <typename T>
  54. inline T make_big_value(largest_float, const char*, std::false_type const&, std::false_type const&)
  55. {
  56. static_assert(sizeof(T) == 0, "Type is unsupported in standalone mode. Please disable and try again.");
  57. }
  58. #endif
  59. template <class T>
  60. inline constexpr T make_big_value(largest_float, const char* s, std::false_type const&, std::true_type const&) BOOST_MATH_NOEXCEPT(T)
  61. {
  62. return T(s);
  63. }
  64. //
  65. // For constants which might fit in a long double (if it's big enough):
  66. //
  67. #define BOOST_MATH_BIG_CONSTANT(T, D, x)\
  68. boost::math::tools::make_big_value<T>(\
  69. BOOST_MATH_LARGEST_FLOAT_C(x), \
  70. BOOST_MATH_STRINGIZE(x), \
  71. std::integral_constant<bool, (std::is_convertible<boost::math::tools::largest_float, T>::value) && \
  72. ((D <= boost::math::tools::numeric_traits<boost::math::tools::largest_float>::digits) \
  73. || std::is_floating_point<T>::value \
  74. || (boost::math::tools::numeric_traits<T>::is_specialized && \
  75. (boost::math::tools::numeric_traits<T>::digits10 <= boost::math::tools::numeric_traits<boost::math::tools::largest_float>::digits10))) >(), \
  76. std::is_constructible<T, const char*>())
  77. //
  78. // For constants too huge for any conceivable long double (and which generate compiler errors if we try and declare them as such):
  79. //
  80. #define BOOST_MATH_HUGE_CONSTANT(T, D, x)\
  81. boost::math::tools::make_big_value<T>(0.0L, BOOST_MATH_STRINGIZE(x), \
  82. 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)>(), \
  83. std::is_constructible<T, const char*>())
  84. }}} // namespaces
  85. #endif