round.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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_ROUND_HPP
  6. #define BOOST_MATH_ROUND_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 round(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::round<%1%>(%1%)", 0, static_cast<result_type>(v), static_cast<result_type>(v), pol);
  21. return v < 0 ? static_cast<result_type>(ceil(v - 0.5f)) : static_cast<result_type>(floor(v + 0.5f));
  22. }
  23. template <class T>
  24. inline typename tools::promote_args<T>::type round(const T& v)
  25. {
  26. return round(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 iround(const T& v, const Policy& pol)
  39. {
  40. BOOST_MATH_STD_USING
  41. T r = boost::math::round(v, pol);
  42. if((r > (std::numeric_limits<int>::max)()) || (r < (std::numeric_limits<int>::min)()))
  43. return static_cast<int>(policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", 0, v, 0, pol));
  44. return static_cast<int>(r);
  45. }
  46. template <class T>
  47. inline int iround(const T& v)
  48. {
  49. return iround(v, policies::policy<>());
  50. }
  51. template <class T, class Policy>
  52. inline long lround(const T& v, const Policy& pol)
  53. {
  54. BOOST_MATH_STD_USING
  55. T r = boost::math::round(v, pol);
  56. if((r > (std::numeric_limits<long>::max)()) || (r < (std::numeric_limits<long>::min)()))
  57. return static_cast<long int>(policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", 0, v, 0L, pol));
  58. return static_cast<long int>(r);
  59. }
  60. template <class T>
  61. inline long lround(const T& v)
  62. {
  63. return lround(v, policies::policy<>());
  64. }
  65. #ifdef BOOST_HAS_LONG_LONG
  66. template <class T, class Policy>
  67. inline boost::long_long_type llround(const T& v, const Policy& pol)
  68. {
  69. BOOST_MATH_STD_USING
  70. T r = boost::math::round(v, pol);
  71. if((r > (std::numeric_limits<boost::long_long_type>::max)()) || (r < (std::numeric_limits<boost::long_long_type>::min)()))
  72. return static_cast<boost::long_long_type>(policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", 0, v, static_cast<boost::long_long_type>(0), pol));
  73. return static_cast<boost::long_long_type>(r);
  74. }
  75. template <class T>
  76. inline boost::long_long_type llround(const T& v)
  77. {
  78. return llround(v, policies::policy<>());
  79. }
  80. #endif
  81. }} // namespaces
  82. #endif // BOOST_MATH_ROUND_HPP