round.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright John Maddock 2007.
  2. // Copyright Matt Borland 2023.
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_MATH_ROUND_HPP
  7. #define BOOST_MATH_ROUND_HPP
  8. #ifdef _MSC_VER
  9. #pragma once
  10. #endif
  11. #include <boost/math/tools/config.hpp>
  12. #include <boost/math/ccmath/detail/config.hpp>
  13. #include <boost/math/policies/error_handling.hpp>
  14. #include <boost/math/special_functions/math_fwd.hpp>
  15. #include <boost/math/special_functions/fpclassify.hpp>
  16. #include <type_traits>
  17. #include <limits>
  18. #include <cmath>
  19. #if !defined(BOOST_MATH_NO_CCMATH) && !defined(BOOST_MATH_NO_CONSTEXPR_DETECTION)
  20. #include <boost/math/ccmath/ldexp.hpp>
  21. # define BOOST_MATH_HAS_CONSTEXPR_LDEXP
  22. #endif
  23. namespace boost{ namespace math{
  24. namespace detail{
  25. template <class T, class Policy>
  26. inline tools::promote_args_t<T> round(const T& v, const Policy& pol, const std::false_type&)
  27. {
  28. BOOST_MATH_STD_USING
  29. using result_type = tools::promote_args_t<T>;
  30. if(!(boost::math::isfinite)(v))
  31. {
  32. return policies::raise_rounding_error("boost::math::round<%1%>(%1%)", nullptr, static_cast<result_type>(v), static_cast<result_type>(v), pol);
  33. }
  34. //
  35. // The logic here is rather convoluted, but avoids a number of traps,
  36. // see discussion here https://github.com/boostorg/math/pull/8
  37. //
  38. if (T(-0.5) < v && v < T(0.5))
  39. {
  40. // special case to avoid rounding error on the direct
  41. // predecessor of +0.5 resp. the direct successor of -0.5 in
  42. // IEEE floating point types
  43. return static_cast<result_type>(0);
  44. }
  45. else if (v > 0)
  46. {
  47. // subtract v from ceil(v) first in order to avoid rounding
  48. // errors on largest representable integer numbers
  49. result_type c(ceil(v));
  50. return T(0.5) < c - v ? c - 1 : c;
  51. }
  52. else
  53. {
  54. // see former branch
  55. result_type f(floor(v));
  56. return T(0.5) < v - f ? f + 1 : f;
  57. }
  58. }
  59. template <class T, class Policy>
  60. inline tools::promote_args_t<T> round(const T& v, const Policy&, const std::true_type&)
  61. {
  62. return v;
  63. }
  64. } // namespace detail
  65. template <class T, class Policy>
  66. inline tools::promote_args_t<T> round(const T& v, const Policy& pol)
  67. {
  68. return detail::round(v, pol, std::integral_constant<bool, detail::is_integer_for_rounding<T>::value>());
  69. }
  70. template <class T>
  71. inline tools::promote_args_t<T> round(const T& v)
  72. {
  73. return round(v, policies::policy<>());
  74. }
  75. //
  76. // The following functions will not compile unless T has an
  77. // implicit conversion to the integer types. For user-defined
  78. // number types this will likely not be the case. In that case
  79. // these functions should either be specialized for the UDT in
  80. // question, or else overloads should be placed in the same
  81. // namespace as the UDT: these will then be found via argument
  82. // dependent lookup. See our concept archetypes for examples.
  83. //
  84. // Non-standard numeric limits syntax "(std::numeric_limits<int>::max)()"
  85. // is to avoid macro substiution from MSVC
  86. // https://stackoverflow.com/questions/27442885/syntax-error-with-stdnumeric-limitsmax
  87. //
  88. template <class T, class Policy>
  89. inline int iround(const T& v, const Policy& pol)
  90. {
  91. BOOST_MATH_STD_USING
  92. using result_type = tools::promote_args_t<T>;
  93. result_type r = boost::math::round(v, pol);
  94. #ifdef BOOST_MATH_HAS_CONSTEXPR_LDEXP
  95. if constexpr (std::is_arithmetic_v<result_type>
  96. #ifdef BOOST_MATH_FLOAT128_TYPE
  97. && !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type>
  98. #endif
  99. )
  100. {
  101. constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits);
  102. if (r >= max_val || r < -max_val)
  103. {
  104. return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", nullptr, v, static_cast<int>(0), pol));
  105. }
  106. }
  107. else
  108. {
  109. static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits);
  110. if (r >= max_val || r < -max_val)
  111. {
  112. return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", nullptr, v, static_cast<int>(0), pol));
  113. }
  114. }
  115. #else
  116. static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits);
  117. if (r >= max_val || r < -max_val)
  118. {
  119. return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", nullptr, v, static_cast<int>(0), pol));
  120. }
  121. #endif
  122. return static_cast<int>(r);
  123. }
  124. template <class T>
  125. inline int iround(const T& v)
  126. {
  127. return iround(v, policies::policy<>());
  128. }
  129. template <class T, class Policy>
  130. inline long lround(const T& v, const Policy& pol)
  131. {
  132. BOOST_MATH_STD_USING
  133. using result_type = tools::promote_args_t<T>;
  134. result_type r = boost::math::round(v, pol);
  135. #ifdef BOOST_MATH_HAS_CONSTEXPR_LDEXP
  136. if constexpr (std::is_arithmetic_v<result_type>
  137. #ifdef BOOST_MATH_FLOAT128_TYPE
  138. && !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type>
  139. #endif
  140. )
  141. {
  142. constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits);
  143. if (r >= max_val || r < -max_val)
  144. {
  145. return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", nullptr, v, static_cast<long>(0), pol));
  146. }
  147. }
  148. else
  149. {
  150. static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits);
  151. if (r >= max_val || r < -max_val)
  152. {
  153. return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", nullptr, v, static_cast<long>(0), pol));
  154. }
  155. }
  156. #else
  157. static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits);
  158. if (r >= max_val || r < -max_val)
  159. {
  160. return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", nullptr, v, static_cast<long>(0), pol));
  161. }
  162. #endif
  163. return static_cast<long>(r);
  164. }
  165. template <class T>
  166. inline long lround(const T& v)
  167. {
  168. return lround(v, policies::policy<>());
  169. }
  170. template <class T, class Policy>
  171. inline long long llround(const T& v, const Policy& pol)
  172. {
  173. BOOST_MATH_STD_USING
  174. using result_type = boost::math::tools::promote_args_t<T>;
  175. result_type r = boost::math::round(v, pol);
  176. #ifdef BOOST_MATH_HAS_CONSTEXPR_LDEXP
  177. if constexpr (std::is_arithmetic_v<result_type>
  178. #ifdef BOOST_MATH_FLOAT128_TYPE
  179. && !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type>
  180. #endif
  181. )
  182. {
  183. constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits);
  184. if (r >= max_val || r < -max_val)
  185. {
  186. return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", nullptr, v, static_cast<long long>(0), pol));
  187. }
  188. }
  189. else
  190. {
  191. static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits);
  192. if (r >= max_val || r < -max_val)
  193. {
  194. return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", nullptr, v, static_cast<long long>(0), pol));
  195. }
  196. }
  197. #else
  198. static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits);
  199. if (r >= max_val || r < -max_val)
  200. {
  201. return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", nullptr, v, static_cast<long long>(0), pol));
  202. }
  203. #endif
  204. return static_cast<long long>(r);
  205. }
  206. template <class T>
  207. inline long long llround(const T& v)
  208. {
  209. return llround(v, policies::policy<>());
  210. }
  211. }} // namespaces
  212. #endif // BOOST_MATH_ROUND_HPP