trunc.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. namespace boost{ namespace math{ namespace detail{
  15. template <class T, class Policy>
  16. inline typename tools::promote_args<T>::type trunc(const T& v, const Policy& pol, const mpl::false_&)
  17. {
  18. BOOST_MATH_STD_USING
  19. typedef typename tools::promote_args<T>::type result_type;
  20. if(!(boost::math::isfinite)(v))
  21. return policies::raise_rounding_error("boost::math::trunc<%1%>(%1%)", 0, static_cast<result_type>(v), static_cast<result_type>(v), pol);
  22. return (v >= 0) ? static_cast<result_type>(floor(v)) : static_cast<result_type>(ceil(v));
  23. }
  24. template <class T, class Policy>
  25. inline typename tools::promote_args<T>::type trunc(const T& v, const Policy&, const mpl::true_&)
  26. {
  27. return v;
  28. }
  29. }
  30. template <class T, class Policy>
  31. inline typename tools::promote_args<T>::type trunc(const T& v, const Policy& pol)
  32. {
  33. return detail::trunc(v, pol, mpl::bool_<detail::is_integer_for_rounding<T>::value>());
  34. }
  35. template <class T>
  36. inline typename tools::promote_args<T>::type trunc(const T& v)
  37. {
  38. return trunc(v, policies::policy<>());
  39. }
  40. //
  41. // The following functions will not compile unless T has an
  42. // implicit convertion to the integer types. For user-defined
  43. // number types this will likely not be the case. In that case
  44. // these functions should either be specialized for the UDT in
  45. // question, or else overloads should be placed in the same
  46. // namespace as the UDT: these will then be found via argument
  47. // dependent lookup. See our concept archetypes for examples.
  48. //
  49. template <class T, class Policy>
  50. inline int itrunc(const T& v, const Policy& pol)
  51. {
  52. BOOST_MATH_STD_USING
  53. typedef typename tools::promote_args<T>::type result_type;
  54. result_type r = boost::math::trunc(v, pol);
  55. if((r > (std::numeric_limits<int>::max)()) || (r < (std::numeric_limits<int>::min)()))
  56. return static_cast<int>(policies::raise_rounding_error("boost::math::itrunc<%1%>(%1%)", 0, static_cast<result_type>(v), 0, pol));
  57. return static_cast<int>(r);
  58. }
  59. template <class T>
  60. inline int itrunc(const T& v)
  61. {
  62. return itrunc(v, policies::policy<>());
  63. }
  64. template <class T, class Policy>
  65. inline long ltrunc(const T& v, const Policy& pol)
  66. {
  67. BOOST_MATH_STD_USING
  68. typedef typename tools::promote_args<T>::type result_type;
  69. result_type r = boost::math::trunc(v, pol);
  70. if((r > (std::numeric_limits<long>::max)()) || (r < (std::numeric_limits<long>::min)()))
  71. return static_cast<long>(policies::raise_rounding_error("boost::math::ltrunc<%1%>(%1%)", 0, static_cast<result_type>(v), 0L, pol));
  72. return static_cast<long>(r);
  73. }
  74. template <class T>
  75. inline long ltrunc(const T& v)
  76. {
  77. return ltrunc(v, policies::policy<>());
  78. }
  79. #ifdef BOOST_HAS_LONG_LONG
  80. template <class T, class Policy>
  81. inline boost::long_long_type lltrunc(const T& v, const Policy& pol)
  82. {
  83. BOOST_MATH_STD_USING
  84. typedef typename tools::promote_args<T>::type result_type;
  85. result_type r = boost::math::trunc(v, pol);
  86. if((r > (std::numeric_limits<boost::long_long_type>::max)()) || (r < (std::numeric_limits<boost::long_long_type>::min)()))
  87. 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));
  88. return static_cast<boost::long_long_type>(r);
  89. }
  90. template <class T>
  91. inline boost::long_long_type lltrunc(const T& v)
  92. {
  93. return lltrunc(v, policies::policy<>());
  94. }
  95. #endif
  96. }} // namespaces
  97. #endif // BOOST_MATH_TRUNC_HPP