precision.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2018 John Maddock. Distributed under the Boost
  3. // 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_MP_PRECISION_HPP
  6. #define BOOST_MP_PRECISION_HPP
  7. #include <boost/multiprecision/traits/is_variable_precision.hpp>
  8. #include <boost/multiprecision/detail/number_base.hpp>
  9. #include <boost/multiprecision/detail/digits.hpp>
  10. namespace boost{ namespace multiprecision{ namespace detail{
  11. template <class B, boost::multiprecision::expression_template_option ET>
  12. inline BOOST_CONSTEXPR unsigned current_precision_of_last_chance_imp(const boost::multiprecision::number<B, ET>&, const mpl::false_&)
  13. {
  14. return std::numeric_limits<boost::multiprecision::number<B, ET> >::digits10;
  15. }
  16. template <class B, boost::multiprecision::expression_template_option ET>
  17. inline unsigned current_precision_of_last_chance_imp(const boost::multiprecision::number<B, ET>& val, const mpl::true_&)
  18. {
  19. //
  20. // We have an arbitrary precision integer, take it's "precision" as the
  21. // location of the most-significant-bit less the location of the
  22. // least-significant-bit, ie the number of bits required to represent the
  23. // the value assuming we will have an exponent to shift things by:
  24. //
  25. return val.is_zero() ? 1 : digits2_2_10(msb(abs(val)) - lsb(abs(val)) + 1);
  26. }
  27. template <class B, boost::multiprecision::expression_template_option ET>
  28. inline unsigned current_precision_of_imp(const boost::multiprecision::number<B, ET>& n, const mpl::true_&)
  29. {
  30. return n.precision();
  31. }
  32. template <class B, boost::multiprecision::expression_template_option ET>
  33. inline BOOST_CONSTEXPR unsigned current_precision_of_imp(const boost::multiprecision::number<B, ET>& val, const mpl::false_&)
  34. {
  35. return current_precision_of_last_chance_imp(val,
  36. mpl::bool_<
  37. std::numeric_limits<boost::multiprecision::number<B, ET> >::is_specialized
  38. && std::numeric_limits<boost::multiprecision::number<B, ET> >::is_integer
  39. && std::numeric_limits<boost::multiprecision::number<B, ET> >::is_exact
  40. && !std::numeric_limits<boost::multiprecision::number<B, ET> >::is_modulo>());
  41. }
  42. template <class Terminal>
  43. inline BOOST_CONSTEXPR unsigned current_precision_of(const Terminal&)
  44. {
  45. return std::numeric_limits<Terminal>::digits10;
  46. }
  47. template <class Terminal, std::size_t N>
  48. inline BOOST_CONSTEXPR unsigned current_precision_of(const Terminal(&)[N])
  49. { // For string literals:
  50. return 0;
  51. }
  52. template <class B, boost::multiprecision::expression_template_option ET>
  53. inline BOOST_CONSTEXPR unsigned current_precision_of(const boost::multiprecision::number<B, ET>& n)
  54. {
  55. return current_precision_of_imp(n, boost::multiprecision::detail::is_variable_precision<boost::multiprecision::number<B, ET> >());
  56. }
  57. template<class tag, class Arg1>
  58. inline BOOST_CONSTEXPR unsigned current_precision_of(const expression<tag, Arg1, void, void, void>& expr)
  59. {
  60. return current_precision_of(expr.left_ref());
  61. }
  62. template<class Arg1>
  63. inline BOOST_CONSTEXPR unsigned current_precision_of(const expression<terminal, Arg1, void, void, void>& expr)
  64. {
  65. return current_precision_of(expr.value());
  66. }
  67. template <class tag, class Arg1, class Arg2>
  68. inline BOOST_CONSTEXPR unsigned current_precision_of(const expression<tag, Arg1, Arg2, void, void>& expr)
  69. {
  70. return (std::max)(current_precision_of(expr.left_ref()), current_precision_of(expr.right_ref()));
  71. }
  72. template <class tag, class Arg1, class Arg2, class Arg3>
  73. inline BOOST_CONSTEXPR unsigned current_precision_of(const expression<tag, Arg1, Arg2, Arg3, void>& expr)
  74. {
  75. return (std::max)((std::max)(current_precision_of(expr.left_ref()), current_precision_of(expr.right_ref())), current_precision_of(expr.middle_ref()));
  76. }
  77. #ifdef BOOST_MSVC
  78. #pragma warning(push)
  79. #pragma warning(disable:4130)
  80. #endif
  81. template <class R, bool = boost::multiprecision::detail::is_variable_precision<R>::value>
  82. struct scoped_default_precision
  83. {
  84. template <class T>
  85. BOOST_CONSTEXPR scoped_default_precision(const T&) {}
  86. template <class T, class U>
  87. BOOST_CONSTEXPR scoped_default_precision(const T&, const U&) {}
  88. template <class T, class U, class V>
  89. BOOST_CONSTEXPR scoped_default_precision(const T&, const U&, const V&) {}
  90. //
  91. // This function is never called: in C++17 it won't be compiled either:
  92. //
  93. unsigned precision()const
  94. {
  95. BOOST_ASSERT("This function should never be called!!" == 0);
  96. return 0;
  97. }
  98. };
  99. #ifdef BOOST_MSVC
  100. #pragma warning(pop)
  101. #endif
  102. template <class R>
  103. struct scoped_default_precision<R, true>
  104. {
  105. template <class T>
  106. BOOST_CXX14_CONSTEXPR scoped_default_precision(const T& a)
  107. {
  108. init(current_precision_of(a));
  109. }
  110. template <class T, class U>
  111. BOOST_CXX14_CONSTEXPR scoped_default_precision(const T& a, const U& b)
  112. {
  113. init((std::max)(current_precision_of(a), current_precision_of(b)));
  114. }
  115. template <class T, class U, class V>
  116. BOOST_CXX14_CONSTEXPR scoped_default_precision(const T& a, const U& b, const V& c)
  117. {
  118. init((std::max)((std::max)(current_precision_of(a), current_precision_of(b)), current_precision_of(c)));
  119. }
  120. ~scoped_default_precision()
  121. {
  122. R::default_precision(m_old_prec);
  123. }
  124. BOOST_CXX14_CONSTEXPR unsigned precision()const
  125. {
  126. return m_new_prec;
  127. }
  128. private:
  129. BOOST_CXX14_CONSTEXPR void init(unsigned p)
  130. {
  131. m_old_prec = R::default_precision();
  132. if (p)
  133. {
  134. R::default_precision(p);
  135. m_new_prec = p;
  136. }
  137. else
  138. m_new_prec = m_old_prec;
  139. }
  140. unsigned m_old_prec, m_new_prec;
  141. };
  142. template <class T>
  143. inline void maybe_promote_precision(T*, const mpl::false_&){}
  144. template <class T>
  145. inline void maybe_promote_precision(T* obj, const mpl::true_&)
  146. {
  147. if (obj->precision() != T::default_precision())
  148. {
  149. obj->precision(T::default_precision());
  150. }
  151. }
  152. template <class T>
  153. inline void maybe_promote_precision(T* obj)
  154. {
  155. maybe_promote_precision(obj, boost::multiprecision::detail::is_variable_precision<T>());
  156. }
  157. #ifndef BOOST_NO_CXX17_IF_CONSTEXPR
  158. # define BOOST_MP_CONSTEXPR_IF_VARIABLE_PRECISION(T) if constexpr (boost::multiprecision::detail::is_variable_precision<T>::value)
  159. #else
  160. # define BOOST_MP_CONSTEXPR_IF_VARIABLE_PRECISION(T) if(boost::multiprecision::detail::is_variable_precision<T>::value)
  161. #endif
  162. }
  163. }
  164. }
  165. #endif // BOOST_MP_IS_BACKEND_HPP