round.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/math_fwd.hpp>
  13. #include <boost/math/special_functions/fpclassify.hpp>
  14. namespace boost{ namespace math{
  15. namespace detail{
  16. template <class T, class Policy>
  17. inline tools::promote_args_t<T> round(const T& v, const Policy& pol, const std::false_type&)
  18. {
  19. BOOST_MATH_STD_USING
  20. using result_type = tools::promote_args_t<T>;
  21. if(!(boost::math::isfinite)(v))
  22. {
  23. return policies::raise_rounding_error("boost::math::round<%1%>(%1%)", nullptr, static_cast<result_type>(v), static_cast<result_type>(v), pol);
  24. }
  25. //
  26. // The logic here is rather convoluted, but avoids a number of traps,
  27. // see discussion here https://github.com/boostorg/math/pull/8
  28. //
  29. if (-0.5 < v && v < 0.5)
  30. {
  31. // special case to avoid rounding error on the direct
  32. // predecessor of +0.5 resp. the direct successor of -0.5 in
  33. // IEEE floating point types
  34. return static_cast<result_type>(0);
  35. }
  36. else if (v > 0)
  37. {
  38. // subtract v from ceil(v) first in order to avoid rounding
  39. // errors on largest representable integer numbers
  40. result_type c(ceil(v));
  41. return 0.5 < c - v ? c - 1 : c;
  42. }
  43. else
  44. {
  45. // see former branch
  46. result_type f(floor(v));
  47. return 0.5 < v - f ? f + 1 : f;
  48. }
  49. }
  50. template <class T, class Policy>
  51. inline tools::promote_args_t<T> round(const T& v, const Policy&, const std::true_type&)
  52. {
  53. return v;
  54. }
  55. } // namespace detail
  56. template <class T, class Policy>
  57. inline tools::promote_args_t<T> round(const T& v, const Policy& pol)
  58. {
  59. return detail::round(v, pol, std::integral_constant<bool, detail::is_integer_for_rounding<T>::value>());
  60. }
  61. template <class T>
  62. inline tools::promote_args_t<T> round(const T& v)
  63. {
  64. return round(v, policies::policy<>());
  65. }
  66. //
  67. // The following functions will not compile unless T has an
  68. // implicit conversion to the integer types. For user-defined
  69. // number types this will likely not be the case. In that case
  70. // these functions should either be specialized for the UDT in
  71. // question, or else overloads should be placed in the same
  72. // namespace as the UDT: these will then be found via argument
  73. // dependent lookup. See our concept archetypes for examples.
  74. //
  75. // Non-standard numeric limits syntax "(std::numeric_limits<int>::max)()"
  76. // is to avoid macro substiution from MSVC
  77. // https://stackoverflow.com/questions/27442885/syntax-error-with-stdnumeric-limitsmax
  78. //
  79. template <class T, class Policy>
  80. inline int iround(const T& v, const Policy& pol)
  81. {
  82. BOOST_MATH_STD_USING
  83. using result_type = tools::promote_args_t<T>;
  84. T r = boost::math::round(v, pol);
  85. if(r > static_cast<result_type>((std::numeric_limits<int>::max)()) || r < static_cast<result_type>((std::numeric_limits<int>::min)()))
  86. {
  87. return static_cast<int>(policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", nullptr, v, 0, pol));
  88. }
  89. return static_cast<int>(r);
  90. }
  91. template <class T>
  92. inline int iround(const T& v)
  93. {
  94. return iround(v, policies::policy<>());
  95. }
  96. template <class T, class Policy>
  97. inline long lround(const T& v, const Policy& pol)
  98. {
  99. BOOST_MATH_STD_USING
  100. using result_type = tools::promote_args_t<T>;
  101. T r = boost::math::round(v, pol);
  102. if(r > static_cast<result_type>((std::numeric_limits<long>::max)()) || r < static_cast<result_type>((std::numeric_limits<long>::min)()))
  103. {
  104. return static_cast<long int>(policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", nullptr, v, 0L, pol));
  105. }
  106. return static_cast<long int>(r);
  107. }
  108. template <class T>
  109. inline long lround(const T& v)
  110. {
  111. return lround(v, policies::policy<>());
  112. }
  113. template <class T, class Policy>
  114. inline long long llround(const T& v, const Policy& pol)
  115. {
  116. BOOST_MATH_STD_USING
  117. using result_type = tools::promote_args_t<T>;
  118. T r = boost::math::round(v, pol);
  119. if(r > static_cast<result_type>((std::numeric_limits<long long>::max)()) ||
  120. r < static_cast<result_type>((std::numeric_limits<long long>::min)()))
  121. {
  122. return static_cast<long long>(policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", nullptr, v, static_cast<long long>(0), pol));
  123. }
  124. return static_cast<long long>(r);
  125. }
  126. template <class T>
  127. inline long long llround(const T& v)
  128. {
  129. return llround(v, policies::policy<>());
  130. }
  131. }} // namespaces
  132. #endif // BOOST_MATH_ROUND_HPP