trunc.hpp 3.4 KB

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