round.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. #ifndef BOOST_MATH_HAS_NVRTC
  13. #include <boost/math/ccmath/detail/config.hpp>
  14. #include <boost/math/policies/error_handling.hpp>
  15. #include <boost/math/special_functions/math_fwd.hpp>
  16. #include <boost/math/special_functions/fpclassify.hpp>
  17. #include <type_traits>
  18. #include <limits>
  19. #include <cmath>
  20. #if !defined(BOOST_MATH_NO_CCMATH) && !defined(BOOST_MATH_NO_CONSTEXPR_DETECTION)
  21. #include <boost/math/ccmath/ldexp.hpp>
  22. # define BOOST_MATH_HAS_CONSTEXPR_LDEXP
  23. #endif
  24. namespace boost{ namespace math{
  25. namespace detail{
  26. template <class T, class Policy>
  27. BOOST_MATH_GPU_ENABLED inline tools::promote_args_t<T> round(const T& v, const Policy& pol, const std::false_type&)
  28. {
  29. BOOST_MATH_STD_USING
  30. using result_type = tools::promote_args_t<T>;
  31. if(!(boost::math::isfinite)(v))
  32. {
  33. return policies::raise_rounding_error("boost::math::round<%1%>(%1%)", nullptr, static_cast<result_type>(v), static_cast<result_type>(v), pol);
  34. }
  35. //
  36. // The logic here is rather convoluted, but avoids a number of traps,
  37. // see discussion here https://github.com/boostorg/math/pull/8
  38. //
  39. if (T(-0.5) < v && v < T(0.5))
  40. {
  41. // special case to avoid rounding error on the direct
  42. // predecessor of +0.5 resp. the direct successor of -0.5 in
  43. // IEEE floating point types
  44. return static_cast<result_type>(0);
  45. }
  46. else if (v > 0)
  47. {
  48. // subtract v from ceil(v) first in order to avoid rounding
  49. // errors on largest representable integer numbers
  50. result_type c(ceil(v));
  51. return T(0.5) < c - v ? c - 1 : c;
  52. }
  53. else
  54. {
  55. // see former branch
  56. result_type f(floor(v));
  57. return T(0.5) < v - f ? f + 1 : f;
  58. }
  59. }
  60. template <class T, class Policy>
  61. BOOST_MATH_GPU_ENABLED inline tools::promote_args_t<T> round(const T& v, const Policy&, const std::true_type&)
  62. {
  63. return v;
  64. }
  65. } // namespace detail
  66. template <class T, class Policy>
  67. BOOST_MATH_GPU_ENABLED inline tools::promote_args_t<T> round(const T& v, const Policy& pol)
  68. {
  69. return detail::round(v, pol, std::integral_constant<bool, detail::is_integer_for_rounding<T>::value>());
  70. }
  71. template <class T>
  72. BOOST_MATH_GPU_ENABLED inline tools::promote_args_t<T> round(const T& v)
  73. {
  74. return round(v, policies::policy<>());
  75. }
  76. //
  77. // The following functions will not compile unless T has an
  78. // implicit conversion to the integer types. For user-defined
  79. // number types this will likely not be the case. In that case
  80. // these functions should either be specialized for the UDT in
  81. // question, or else overloads should be placed in the same
  82. // namespace as the UDT: these will then be found via argument
  83. // dependent lookup. See our concept archetypes for examples.
  84. //
  85. // Non-standard numeric limits syntax "(std::numeric_limits<int>::max)()"
  86. // is to avoid macro substiution from MSVC
  87. // https://stackoverflow.com/questions/27442885/syntax-error-with-stdnumeric-limitsmax
  88. //
  89. template <class T, class Policy>
  90. inline int iround(const T& v, const Policy& pol)
  91. {
  92. BOOST_MATH_STD_USING
  93. using result_type = tools::promote_args_t<T>;
  94. result_type r = boost::math::round(v, pol);
  95. #if defined(BOOST_MATH_HAS_CONSTEXPR_LDEXP) && !defined(BOOST_MATH_HAS_GPU_SUPPORT)
  96. if constexpr (std::is_arithmetic_v<result_type>
  97. #ifdef BOOST_MATH_FLOAT128_TYPE
  98. && !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type>
  99. #endif
  100. )
  101. {
  102. constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits);
  103. if (r >= max_val || r < -max_val)
  104. {
  105. return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", nullptr, v, static_cast<int>(0), pol));
  106. }
  107. }
  108. else
  109. {
  110. static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits);
  111. if (r >= max_val || r < -max_val)
  112. {
  113. return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", nullptr, v, static_cast<int>(0), pol));
  114. }
  115. }
  116. #else
  117. BOOST_MATH_STATIC_LOCAL_VARIABLE const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits);
  118. if (r >= max_val || r < -max_val)
  119. {
  120. return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", nullptr, v, static_cast<int>(0), pol));
  121. }
  122. #endif
  123. return static_cast<int>(r);
  124. }
  125. template <class T>
  126. BOOST_MATH_GPU_ENABLED inline int iround(const T& v)
  127. {
  128. return iround(v, policies::policy<>());
  129. }
  130. template <class T, class Policy>
  131. BOOST_MATH_GPU_ENABLED inline long lround(const T& v, const Policy& pol)
  132. {
  133. BOOST_MATH_STD_USING
  134. using result_type = tools::promote_args_t<T>;
  135. result_type r = boost::math::round(v, pol);
  136. #if defined(BOOST_MATH_HAS_CONSTEXPR_LDEXP) && !defined(BOOST_MATH_HAS_GPU_SUPPORT)
  137. if constexpr (std::is_arithmetic_v<result_type>
  138. #ifdef BOOST_MATH_FLOAT128_TYPE
  139. && !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type>
  140. #endif
  141. )
  142. {
  143. constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits);
  144. if (r >= max_val || r < -max_val)
  145. {
  146. return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", nullptr, v, static_cast<long>(0), pol));
  147. }
  148. }
  149. else
  150. {
  151. static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits);
  152. if (r >= max_val || r < -max_val)
  153. {
  154. return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", nullptr, v, static_cast<long>(0), pol));
  155. }
  156. }
  157. #else
  158. BOOST_MATH_STATIC_LOCAL_VARIABLE const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits);
  159. if (r >= max_val || r < -max_val)
  160. {
  161. return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", nullptr, v, static_cast<long>(0), pol));
  162. }
  163. #endif
  164. return static_cast<long>(r);
  165. }
  166. template <class T>
  167. BOOST_MATH_GPU_ENABLED inline long lround(const T& v)
  168. {
  169. return lround(v, policies::policy<>());
  170. }
  171. template <class T, class Policy>
  172. BOOST_MATH_GPU_ENABLED inline long long llround(const T& v, const Policy& pol)
  173. {
  174. BOOST_MATH_STD_USING
  175. using result_type = boost::math::tools::promote_args_t<T>;
  176. result_type r = boost::math::round(v, pol);
  177. #if defined(BOOST_MATH_HAS_CONSTEXPR_LDEXP) && !defined(BOOST_MATH_HAS_GPU_SUPPORT)
  178. if constexpr (std::is_arithmetic_v<result_type>
  179. #ifdef BOOST_MATH_FLOAT128_TYPE
  180. && !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type>
  181. #endif
  182. )
  183. {
  184. constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits);
  185. if (r >= max_val || r < -max_val)
  186. {
  187. return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", nullptr, v, static_cast<long long>(0), pol));
  188. }
  189. }
  190. else
  191. {
  192. static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits);
  193. if (r >= max_val || r < -max_val)
  194. {
  195. return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", nullptr, v, static_cast<long long>(0), pol));
  196. }
  197. }
  198. #else
  199. BOOST_MATH_STATIC_LOCAL_VARIABLE const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits);
  200. if (r >= max_val || r < -max_val)
  201. {
  202. return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", nullptr, v, static_cast<long long>(0), pol));
  203. }
  204. #endif
  205. return static_cast<long long>(r);
  206. }
  207. template <class T>
  208. BOOST_MATH_GPU_ENABLED inline long long llround(const T& v)
  209. {
  210. return llround(v, policies::policy<>());
  211. }
  212. }} // namespaces
  213. #else // Specialized NVRTC overloads
  214. namespace boost {
  215. namespace math {
  216. template <typename T>
  217. BOOST_MATH_GPU_ENABLED T round(T x)
  218. {
  219. return ::round(x);
  220. }
  221. template <>
  222. BOOST_MATH_GPU_ENABLED float round(float x)
  223. {
  224. return ::roundf(x);
  225. }
  226. template <typename T, typename Policy>
  227. BOOST_MATH_GPU_ENABLED T round(T x, const Policy&)
  228. {
  229. return ::round(x);
  230. }
  231. template <typename Policy>
  232. BOOST_MATH_GPU_ENABLED float round(float x, const Policy&)
  233. {
  234. return ::roundf(x);
  235. }
  236. template <typename T>
  237. BOOST_MATH_GPU_ENABLED int iround(T x)
  238. {
  239. return static_cast<int>(::lround(x));
  240. }
  241. template <>
  242. BOOST_MATH_GPU_ENABLED int iround(float x)
  243. {
  244. return static_cast<int>(::lroundf(x));
  245. }
  246. template <typename T, typename Policy>
  247. BOOST_MATH_GPU_ENABLED int iround(T x, const Policy&)
  248. {
  249. return static_cast<int>(::lround(x));
  250. }
  251. template <typename Policy>
  252. BOOST_MATH_GPU_ENABLED int iround(float x, const Policy&)
  253. {
  254. return static_cast<int>(::lroundf(x));
  255. }
  256. template <typename T>
  257. BOOST_MATH_GPU_ENABLED long lround(T x)
  258. {
  259. return ::lround(x);
  260. }
  261. template <>
  262. BOOST_MATH_GPU_ENABLED long lround(float x)
  263. {
  264. return ::lroundf(x);
  265. }
  266. template <typename T, typename Policy>
  267. BOOST_MATH_GPU_ENABLED long lround(T x, const Policy&)
  268. {
  269. return ::lround(x);
  270. }
  271. template <typename Policy>
  272. BOOST_MATH_GPU_ENABLED long lround(float x, const Policy&)
  273. {
  274. return ::lroundf(x);
  275. }
  276. template <typename T>
  277. BOOST_MATH_GPU_ENABLED long long llround(T x)
  278. {
  279. return ::llround(x);
  280. }
  281. template <>
  282. BOOST_MATH_GPU_ENABLED long long llround(float x)
  283. {
  284. return ::llroundf(x);
  285. }
  286. template <typename T, typename Policy>
  287. BOOST_MATH_GPU_ENABLED long long llround(T x, const Policy&)
  288. {
  289. return ::llround(x);
  290. }
  291. template <typename Policy>
  292. BOOST_MATH_GPU_ENABLED long long llround(float x, const Policy&)
  293. {
  294. return ::llroundf(x);
  295. }
  296. } // Namespace math
  297. } // Namespace boost
  298. #endif // BOOST_MATH_HAS_NVRTC
  299. #endif // BOOST_MATH_ROUND_HPP