big_constant.hpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_constructible.hpp>
  12. #include <boost/type_traits/is_convertible.hpp>
  13. #include <boost/type_traits/is_floating_point.hpp>
  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. #else
  30. typedef long double largest_float;
  31. #define BOOST_MATH_LARGEST_FLOAT_C(x) x##L
  32. #endif
  33. template <class T>
  34. inline BOOST_CONSTEXPR_OR_CONST T make_big_value(largest_float v, const char*, mpl::true_ const&, mpl::false_ const&) BOOST_MATH_NOEXCEPT(T)
  35. {
  36. return static_cast<T>(v);
  37. }
  38. template <class T>
  39. inline BOOST_CONSTEXPR_OR_CONST T make_big_value(largest_float v, const char*, mpl::true_ const&, mpl::true_ const&) BOOST_MATH_NOEXCEPT(T)
  40. {
  41. return static_cast<T>(v);
  42. }
  43. #ifndef BOOST_MATH_NO_LEXICAL_CAST
  44. template <class T>
  45. inline T make_big_value(largest_float, const char* s, mpl::false_ const&, mpl::false_ const&)
  46. {
  47. return boost::lexical_cast<T>(s);
  48. }
  49. #endif
  50. template <class T>
  51. inline BOOST_MATH_CONSTEXPR T make_big_value(largest_float, const char* s, mpl::false_ const&, mpl::true_ const&) BOOST_MATH_NOEXCEPT(T)
  52. {
  53. return T(s);
  54. }
  55. //
  56. // For constants which might fit in a long double (if it's big enough):
  57. //
  58. #define BOOST_MATH_BIG_CONSTANT(T, D, x)\
  59. boost::math::tools::make_big_value<T>(\
  60. BOOST_MATH_LARGEST_FLOAT_C(x), \
  61. BOOST_STRINGIZE(x), \
  62. boost::mpl::bool_< (boost::is_convertible<boost::math::tools::largest_float, T>::value) && \
  63. ((D <= boost::math::tools::numeric_traits<boost::math::tools::largest_float>::digits) \
  64. || boost::is_floating_point<T>::value \
  65. || (boost::math::tools::numeric_traits<T>::is_specialized && \
  66. (boost::math::tools::numeric_traits<T>::digits10 <= boost::math::tools::numeric_traits<boost::math::tools::largest_float>::digits10))) >(), \
  67. boost::is_constructible<T, const char*>())
  68. //
  69. // For constants too huge for any conceivable long double (and which generate compiler errors if we try and declare them as such):
  70. //
  71. #define BOOST_MATH_HUGE_CONSTANT(T, D, x)\
  72. boost::math::tools::make_big_value<T>(0.0L, BOOST_STRINGIZE(x), \
  73. boost::mpl::bool_<boost::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)>(), \
  74. boost::is_constructible<T, const char*>())
  75. }}} // namespaces
  76. #endif