trunc.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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_TRUNC_HPP
  7. #define BOOST_MATH_TRUNC_HPP
  8. #ifdef _MSC_VER
  9. #pragma once
  10. #endif
  11. #include <type_traits>
  12. #include <boost/math/special_functions/math_fwd.hpp>
  13. #include <boost/math/tools/config.hpp>
  14. #include <boost/math/ccmath/detail/config.hpp>
  15. #include <boost/math/policies/error_handling.hpp>
  16. #include <boost/math/special_functions/fpclassify.hpp>
  17. #include <boost/math/tools/is_constant_evaluated.hpp>
  18. #if !defined(BOOST_MATH_NO_CCMATH) && !defined(BOOST_MATH_NO_CONSTEXPR_DETECTION)
  19. #include <boost/math/ccmath/ldexp.hpp>
  20. # define BOOST_MATH_HAS_CONSTEXPR_LDEXP
  21. #endif
  22. namespace boost{ namespace math{ namespace detail{
  23. template <class T, class Policy>
  24. inline tools::promote_args_t<T> trunc(const T& v, const Policy& pol, const std::false_type&)
  25. {
  26. BOOST_MATH_STD_USING
  27. using result_type = tools::promote_args_t<T>;
  28. if(!(boost::math::isfinite)(v))
  29. {
  30. return policies::raise_rounding_error("boost::math::trunc<%1%>(%1%)", nullptr, static_cast<result_type>(v), static_cast<result_type>(v), pol);
  31. }
  32. return (v >= 0) ? static_cast<result_type>(floor(v)) : static_cast<result_type>(ceil(v));
  33. }
  34. template <class T, class Policy>
  35. inline tools::promote_args_t<T> trunc(const T& v, const Policy&, const std::true_type&)
  36. {
  37. return v;
  38. }
  39. }
  40. template <class T, class Policy>
  41. inline tools::promote_args_t<T> trunc(const T& v, const Policy& pol)
  42. {
  43. return detail::trunc(v, pol, std::integral_constant<bool, detail::is_integer_for_rounding<T>::value>());
  44. }
  45. template <class T>
  46. inline tools::promote_args_t<T> trunc(const T& v)
  47. {
  48. return trunc(v, policies::policy<>());
  49. }
  50. //
  51. // The following functions will not compile unless T has an
  52. // implicit conversion to the integer types. For user-defined
  53. // number types this will likely not be the case. In that case
  54. // these functions should either be specialized for the UDT in
  55. // question, or else overloads should be placed in the same
  56. // namespace as the UDT: these will then be found via argument
  57. // dependent lookup. See our concept archetypes for examples.
  58. //
  59. // Non-standard numeric limits syntax "(std::numeric_limits<int>::max)()"
  60. // is to avoid macro substiution from MSVC
  61. // https://stackoverflow.com/questions/27442885/syntax-error-with-stdnumeric-limitsmax
  62. //
  63. template <class T, class Policy>
  64. inline int itrunc(const T& v, const Policy& pol)
  65. {
  66. BOOST_MATH_STD_USING
  67. using result_type = tools::promote_args_t<T>;
  68. result_type r = boost::math::trunc(v, pol);
  69. #ifdef BOOST_MATH_HAS_CONSTEXPR_LDEXP
  70. if constexpr (std::is_arithmetic_v<result_type>
  71. #ifdef BOOST_MATH_FLOAT128_TYPE
  72. && !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type>
  73. #endif
  74. )
  75. {
  76. constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits);
  77. if (r >= max_val || r < -max_val)
  78. {
  79. return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::itrunc<%1%>(%1%)", nullptr, v, static_cast<int>(0), pol));
  80. }
  81. }
  82. else
  83. {
  84. static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits);
  85. if (r >= max_val || r < -max_val)
  86. {
  87. return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::itrunc<%1%>(%1%)", nullptr, v, static_cast<int>(0), pol));
  88. }
  89. }
  90. #else
  91. static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits);
  92. if (r >= max_val || r < -max_val)
  93. {
  94. return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::itrunc<%1%>(%1%)", nullptr, v, static_cast<int>(0), pol));
  95. }
  96. #endif
  97. return static_cast<int>(r);
  98. }
  99. template <class T>
  100. inline int itrunc(const T& v)
  101. {
  102. return itrunc(v, policies::policy<>());
  103. }
  104. template <class T, class Policy>
  105. inline long ltrunc(const T& v, const Policy& pol)
  106. {
  107. BOOST_MATH_STD_USING
  108. using result_type = tools::promote_args_t<T>;
  109. result_type r = boost::math::trunc(v, pol);
  110. #ifdef BOOST_MATH_HAS_CONSTEXPR_LDEXP
  111. if constexpr (std::is_arithmetic_v<result_type>
  112. #ifdef BOOST_MATH_FLOAT128_TYPE
  113. && !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type>
  114. #endif
  115. )
  116. {
  117. constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits);
  118. if (r >= max_val || r < -max_val)
  119. {
  120. return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::ltrunc<%1%>(%1%)", nullptr, v, static_cast<long>(0), pol));
  121. }
  122. }
  123. else
  124. {
  125. static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits);
  126. if (r >= max_val || r < -max_val)
  127. {
  128. return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::ltrunc<%1%>(%1%)", nullptr, v, static_cast<long>(0), pol));
  129. }
  130. }
  131. #else
  132. static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits);
  133. if (r >= max_val || r < -max_val)
  134. {
  135. return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::ltrunc<%1%>(%1%)", nullptr, v, static_cast<long>(0), pol));
  136. }
  137. #endif
  138. return static_cast<long>(r);
  139. }
  140. template <class T>
  141. inline long ltrunc(const T& v)
  142. {
  143. return ltrunc(v, policies::policy<>());
  144. }
  145. template <class T, class Policy>
  146. inline long long lltrunc(const T& v, const Policy& pol)
  147. {
  148. BOOST_MATH_STD_USING
  149. using result_type = tools::promote_args_t<T>;
  150. result_type r = boost::math::trunc(v, pol);
  151. #ifdef BOOST_MATH_HAS_CONSTEXPR_LDEXP
  152. if constexpr (std::is_arithmetic_v<result_type>
  153. #ifdef BOOST_MATH_FLOAT128_TYPE
  154. && !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type>
  155. #endif
  156. )
  157. {
  158. constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits);
  159. if (r >= max_val || r < -max_val)
  160. {
  161. return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::lltrunc<%1%>(%1%)", nullptr, v, static_cast<long long>(0), pol));
  162. }
  163. }
  164. else
  165. {
  166. static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits);
  167. if (r >= max_val || r < -max_val)
  168. {
  169. return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::lltrunc<%1%>(%1%)", nullptr, v, static_cast<long long>(0), pol));
  170. }
  171. }
  172. #else
  173. static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits);
  174. if (r >= max_val || r < -max_val)
  175. {
  176. return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::lltrunc<%1%>(%1%)", nullptr, v, static_cast<long long>(0), pol));
  177. }
  178. #endif
  179. return static_cast<long long>(r);
  180. }
  181. template <class T>
  182. inline long long lltrunc(const T& v)
  183. {
  184. return lltrunc(v, policies::policy<>());
  185. }
  186. template <class T, class Policy>
  187. inline typename std::enable_if<std::is_constructible<int, T>::value, int>::type
  188. iconvert(const T& v, const Policy&)
  189. {
  190. return static_cast<int>(v);
  191. }
  192. template <class T, class Policy>
  193. inline typename std::enable_if<!std::is_constructible<int, T>::value, int>::type
  194. iconvert(const T& v, const Policy& pol)
  195. {
  196. using boost::math::itrunc;
  197. return itrunc(v, pol);
  198. }
  199. template <class T, class Policy>
  200. inline typename std::enable_if<std::is_constructible<long, T>::value, long>::type
  201. lconvert(const T& v, const Policy&)
  202. {
  203. return static_cast<long>(v);
  204. }
  205. template <class T, class Policy>
  206. inline typename std::enable_if<!std::is_constructible<long, T>::value, long>::type
  207. lconvert(const T& v, const Policy& pol)
  208. {
  209. using boost::math::ltrunc;
  210. return ltrunc(v, pol);
  211. }
  212. template <class T, class Policy>
  213. inline typename std::enable_if<std::is_constructible<long long, T>::value, long long>::type
  214. llconvertert(const T& v, const Policy&)
  215. {
  216. return static_cast<long long>(v);
  217. }
  218. template <class T, class Policy>
  219. inline typename std::enable_if<!std::is_constructible<long long, T>::value, long long>::type
  220. llconvertert(const T& v, const Policy& pol)
  221. {
  222. using boost::math::lltrunc;
  223. return lltrunc(v, pol);
  224. }
  225. }} // namespaces
  226. #endif // BOOST_MATH_TRUNC_HPP