big_constant.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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_NO_LEXICAL_CAST
  9. #include <boost/lexical_cast.hpp>
  10. #endif
  11. #include <boost/type_traits/is_convertible.hpp>
  12. namespace boost{ namespace math{
  13. namespace tools{
  14. template <class T>
  15. struct numeric_traits : public std::numeric_limits< T > {};
  16. #ifdef BOOST_MATH_USE_FLOAT128
  17. typedef __float128 largest_float;
  18. #define BOOST_MATH_LARGEST_FLOAT_C(x) x##Q
  19. template <>
  20. struct numeric_traits<__float128>
  21. {
  22. static const int digits = 113;
  23. static const int digits10 = 33;
  24. static const int max_exponent = 16384;
  25. static const bool is_specialized = true;
  26. };
  27. #else
  28. typedef long double largest_float;
  29. #define BOOST_MATH_LARGEST_FLOAT_C(x) x##L
  30. #endif
  31. template <class T>
  32. inline BOOST_CONSTEXPR_OR_CONST T make_big_value(largest_float v, const char*, mpl::true_ const&, mpl::false_ const&) BOOST_MATH_NOEXCEPT(T)
  33. {
  34. return static_cast<T>(v);
  35. }
  36. template <class T>
  37. inline BOOST_CONSTEXPR_OR_CONST T make_big_value(largest_float v, const char*, mpl::true_ const&, mpl::true_ const&) BOOST_MATH_NOEXCEPT(T)
  38. {
  39. return static_cast<T>(v);
  40. }
  41. #ifndef BOOST_MATH_NO_LEXICAL_CAST
  42. template <class T>
  43. inline T make_big_value(largest_float, const char* s, mpl::false_ const&, mpl::false_ const&)
  44. {
  45. return boost::lexical_cast<T>(s);
  46. }
  47. #endif
  48. template <class T>
  49. inline BOOST_MATH_CONSTEXPR T make_big_value(largest_float, const char* s, mpl::false_ const&, mpl::true_ const&) BOOST_MATH_NOEXCEPT(T)
  50. {
  51. return T(s);
  52. }
  53. //
  54. // For constants which might fit in a long double (if it's big enough):
  55. //
  56. #define BOOST_MATH_BIG_CONSTANT(T, D, x)\
  57. boost::math::tools::make_big_value<T>(\
  58. BOOST_MATH_LARGEST_FLOAT_C(x), \
  59. BOOST_STRINGIZE(x), \
  60. mpl::bool_< (is_convertible<boost::math::tools::largest_float, T>::value) && \
  61. ((D <= boost::math::tools::numeric_traits<boost::math::tools::largest_float>::digits) \
  62. || is_floating_point<T>::value \
  63. || (boost::math::tools::numeric_traits<T>::is_specialized && \
  64. (boost::math::tools::numeric_traits<T>::digits10 <= boost::math::tools::numeric_traits<boost::math::tools::largest_float>::digits10))) >(), \
  65. boost::is_convertible<const char*, T>())
  66. //
  67. // For constants too huge for any conceivable long double (and which generate compiler errors if we try and declare them as such):
  68. //
  69. #define BOOST_MATH_HUGE_CONSTANT(T, D, x)\
  70. boost::math::tools::make_big_value<T>(0.0L, BOOST_STRINGIZE(x), \
  71. mpl::bool_<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)>(), \
  72. boost::is_constructible<const char*, T>())
  73. }}} // namespaces
  74. #endif