trunc.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 mpl::false_&)
  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 mpl::true_&)
  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, mpl::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 convertion 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. template <class T, class Policy>
  52. inline int itrunc(const T& v, const Policy& pol)
  53. {
  54. BOOST_MATH_STD_USING
  55. typedef typename tools::promote_args<T>::type result_type;
  56. result_type r = boost::math::trunc(v, pol);
  57. if((r > (std::numeric_limits<int>::max)()) || (r < (std::numeric_limits<int>::min)()))
  58. return static_cast<int>(policies::raise_rounding_error("boost::math::itrunc<%1%>(%1%)", 0, static_cast<result_type>(v), 0, pol));
  59. return static_cast<int>(r);
  60. }
  61. template <class T>
  62. inline int itrunc(const T& v)
  63. {
  64. return itrunc(v, policies::policy<>());
  65. }
  66. template <class T, class Policy>
  67. inline long ltrunc(const T& v, const Policy& pol)
  68. {
  69. BOOST_MATH_STD_USING
  70. typedef typename tools::promote_args<T>::type result_type;
  71. result_type r = boost::math::trunc(v, pol);
  72. if((r > (std::numeric_limits<long>::max)()) || (r < (std::numeric_limits<long>::min)()))
  73. return static_cast<long>(policies::raise_rounding_error("boost::math::ltrunc<%1%>(%1%)", 0, static_cast<result_type>(v), 0L, pol));
  74. return static_cast<long>(r);
  75. }
  76. template <class T>
  77. inline long ltrunc(const T& v)
  78. {
  79. return ltrunc(v, policies::policy<>());
  80. }
  81. #ifdef BOOST_HAS_LONG_LONG
  82. template <class T, class Policy>
  83. inline boost::long_long_type lltrunc(const T& v, const Policy& pol)
  84. {
  85. BOOST_MATH_STD_USING
  86. typedef typename tools::promote_args<T>::type result_type;
  87. result_type r = boost::math::trunc(v, pol);
  88. if((r > (std::numeric_limits<boost::long_long_type>::max)()) || (r < (std::numeric_limits<boost::long_long_type>::min)()))
  89. 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));
  90. return static_cast<boost::long_long_type>(r);
  91. }
  92. template <class T>
  93. inline boost::long_long_type lltrunc(const T& v)
  94. {
  95. return lltrunc(v, policies::policy<>());
  96. }
  97. #endif
  98. template <class T, class Policy>
  99. inline typename boost::enable_if_c<boost::is_constructible<int, T>::value, int>::type
  100. iconvert(const T& v, const Policy&)
  101. {
  102. return static_cast<int>(v);
  103. }
  104. template <class T, class Policy>
  105. inline typename boost::disable_if_c<boost::is_constructible<int, T>::value, int>::type
  106. iconvert(const T& v, const Policy& pol)
  107. {
  108. using boost::math::itrunc;
  109. return itrunc(v, pol);
  110. }
  111. template <class T, class Policy>
  112. inline typename boost::enable_if_c<boost::is_constructible<long, T>::value, long>::type
  113. lconvert(const T& v, const Policy&)
  114. {
  115. return static_cast<long>(v);
  116. }
  117. template <class T, class Policy>
  118. inline typename boost::disable_if_c<boost::is_constructible<long, T>::value, long>::type
  119. lconvert(const T& v, const Policy& pol)
  120. {
  121. using boost::math::ltrunc;
  122. return ltrunc(v, pol);
  123. }
  124. #ifdef BOOST_HAS_LONG_LONG
  125. template <class T, class Policy>
  126. inline typename boost::enable_if_c<boost::is_constructible<boost::long_long_type, T>::value, boost::long_long_type>::type
  127. llconvertert(const T& v, const Policy&)
  128. {
  129. return static_cast<boost::long_long_type>(v);
  130. }
  131. template <class T, class Policy>
  132. inline typename boost::disable_if_c<boost::is_constructible<boost::long_long_type, T>::value, boost::long_long_type>::type
  133. llconvertert(const T& v, const Policy& pol)
  134. {
  135. using boost::math::lltrunc;
  136. return lltrunc(v, pol);
  137. }
  138. #endif
  139. }} // namespaces
  140. #endif // BOOST_MATH_TRUNC_HPP