trunc.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright John Maddock 2007.
  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_TRUNC_HPP
  6. #define BOOST_MATH_TRUNC_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <type_traits>
  11. #include <boost/math/special_functions/math_fwd.hpp>
  12. #include <boost/math/tools/config.hpp>
  13. #include <boost/math/policies/error_handling.hpp>
  14. #include <boost/math/special_functions/fpclassify.hpp>
  15. namespace boost{ namespace math{ namespace detail{
  16. template <class T, class Policy>
  17. inline tools::promote_args_t<T> trunc(const T& v, const Policy& pol, const std::false_type&)
  18. {
  19. BOOST_MATH_STD_USING
  20. using result_type = tools::promote_args_t<T>;
  21. if(!(boost::math::isfinite)(v))
  22. {
  23. return policies::raise_rounding_error("boost::math::trunc<%1%>(%1%)", nullptr, static_cast<result_type>(v), static_cast<result_type>(v), pol);
  24. }
  25. return (v >= 0) ? static_cast<result_type>(floor(v)) : static_cast<result_type>(ceil(v));
  26. }
  27. template <class T, class Policy>
  28. inline tools::promote_args_t<T> trunc(const T& v, const Policy&, const std::true_type&)
  29. {
  30. return v;
  31. }
  32. }
  33. template <class T, class Policy>
  34. inline tools::promote_args_t<T> trunc(const T& v, const Policy& pol)
  35. {
  36. return detail::trunc(v, pol, std::integral_constant<bool, detail::is_integer_for_rounding<T>::value>());
  37. }
  38. template <class T>
  39. inline tools::promote_args_t<T> trunc(const T& v)
  40. {
  41. return trunc(v, policies::policy<>());
  42. }
  43. //
  44. // The following functions will not compile unless T has an
  45. // implicit conversion to the integer types. For user-defined
  46. // number types this will likely not be the case. In that case
  47. // these functions should either be specialized for the UDT in
  48. // question, or else overloads should be placed in the same
  49. // namespace as the UDT: these will then be found via argument
  50. // dependent lookup. See our concept archetypes for examples.
  51. //
  52. // Non-standard numeric limits syntax "(std::numeric_limits<int>::max)()"
  53. // is to avoid macro substiution from MSVC
  54. // https://stackoverflow.com/questions/27442885/syntax-error-with-stdnumeric-limitsmax
  55. //
  56. template <class T, class Policy>
  57. inline int itrunc(const T& v, const Policy& pol)
  58. {
  59. BOOST_MATH_STD_USING
  60. using result_type = tools::promote_args_t<T>;
  61. result_type r = boost::math::trunc(v, pol);
  62. if(r > static_cast<result_type>((std::numeric_limits<int>::max)()) || r < static_cast<result_type>((std::numeric_limits<int>::min)()))
  63. {
  64. return static_cast<int>(policies::raise_rounding_error("boost::math::itrunc<%1%>(%1%)", nullptr, static_cast<result_type>(v), 0, pol));
  65. }
  66. return static_cast<int>(r);
  67. }
  68. template <class T>
  69. inline int itrunc(const T& v)
  70. {
  71. return itrunc(v, policies::policy<>());
  72. }
  73. template <class T, class Policy>
  74. inline long ltrunc(const T& v, const Policy& pol)
  75. {
  76. BOOST_MATH_STD_USING
  77. using result_type = tools::promote_args_t<T>;
  78. result_type r = boost::math::trunc(v, pol);
  79. if(r > static_cast<result_type>((std::numeric_limits<long>::max)()) || r < static_cast<result_type>((std::numeric_limits<long>::min)()))
  80. {
  81. return static_cast<long>(policies::raise_rounding_error("boost::math::ltrunc<%1%>(%1%)", nullptr, static_cast<result_type>(v), 0L, pol));
  82. }
  83. return static_cast<long>(r);
  84. }
  85. template <class T>
  86. inline long ltrunc(const T& v)
  87. {
  88. return ltrunc(v, policies::policy<>());
  89. }
  90. template <class T, class Policy>
  91. inline long long lltrunc(const T& v, const Policy& pol)
  92. {
  93. BOOST_MATH_STD_USING
  94. using result_type = tools::promote_args_t<T>;
  95. result_type r = boost::math::trunc(v, pol);
  96. if(r > static_cast<result_type>((std::numeric_limits<long long>::max)()) ||
  97. r < static_cast<result_type>((std::numeric_limits<long long>::min)()))
  98. {
  99. return static_cast<long long>(policies::raise_rounding_error("boost::math::lltrunc<%1%>(%1%)", nullptr, v, static_cast<long long>(0), pol));
  100. }
  101. return static_cast<long long>(r);
  102. }
  103. template <class T>
  104. inline long long lltrunc(const T& v)
  105. {
  106. return lltrunc(v, policies::policy<>());
  107. }
  108. template <class T, class Policy>
  109. inline typename std::enable_if<std::is_constructible<int, T>::value, int>::type
  110. iconvert(const T& v, const Policy&)
  111. {
  112. return static_cast<int>(v);
  113. }
  114. template <class T, class Policy>
  115. inline typename std::enable_if<!std::is_constructible<int, T>::value, int>::type
  116. iconvert(const T& v, const Policy& pol)
  117. {
  118. using boost::math::itrunc;
  119. return itrunc(v, pol);
  120. }
  121. template <class T, class Policy>
  122. inline typename std::enable_if<std::is_constructible<long, T>::value, long>::type
  123. lconvert(const T& v, const Policy&)
  124. {
  125. return static_cast<long>(v);
  126. }
  127. template <class T, class Policy>
  128. inline typename std::enable_if<!std::is_constructible<long, T>::value, long>::type
  129. lconvert(const T& v, const Policy& pol)
  130. {
  131. using boost::math::ltrunc;
  132. return ltrunc(v, pol);
  133. }
  134. template <class T, class Policy>
  135. inline typename std::enable_if<std::is_constructible<long long, T>::value, long long>::type
  136. llconvertert(const T& v, const Policy&)
  137. {
  138. return static_cast<long long>(v);
  139. }
  140. template <class T, class Policy>
  141. inline typename std::enable_if<!std::is_constructible<long long, T>::value, long long>::type
  142. llconvertert(const T& v, const Policy& pol)
  143. {
  144. using boost::math::lltrunc;
  145. return lltrunc(v, pol);
  146. }
  147. }} // namespaces
  148. #endif // BOOST_MATH_TRUNC_HPP