trunc.hpp 5.7 KB

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