| 1 | // Copyright John Maddock 2007. |
| 2 | // Copyright Matt Borland 2023. |
| 3 | // Use, modification and distribution are subject to the |
| 4 | // Boost Software License, Version 1.0. (See accompanying file |
| 5 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | #ifndef BOOST_MATH_ROUND_HPP |
| 8 | #define BOOST_MATH_ROUND_HPP |
| 9 | |
| 10 | #ifdef _MSC_VER |
| 11 | #pragma once |
| 12 | #endif |
| 13 | |
| 14 | #include <boost/math/tools/config.hpp> |
| 15 | #include <boost/math/policies/error_handling.hpp> |
| 16 | #include <boost/math/special_functions/math_fwd.hpp> |
| 17 | #include <boost/math/special_functions/fpclassify.hpp> |
| 18 | #include <type_traits> |
| 19 | #include <limits> |
| 20 | #include <cmath> |
| 21 | |
| 22 | #ifndef BOOST_NO_CXX17_IF_CONSTEXPR |
| 23 | #include <boost/math/ccmath/ldexp.hpp> |
| 24 | # if !defined(BOOST_MATH_NO_CONSTEXPR_DETECTION) |
| 25 | # define BOOST_MATH_HAS_CONSTEXPR_LDEXP |
| 26 | # endif |
| 27 | #endif |
| 28 | |
| 29 | namespace boost{ namespace math{ |
| 30 | |
| 31 | namespace detail{ |
| 32 | |
| 33 | template <class T, class Policy> |
| 34 | inline tools::promote_args_t<T> round(const T& v, const Policy& pol, const std::false_type&) |
| 35 | { |
| 36 | BOOST_MATH_STD_USING |
| 37 | using result_type = tools::promote_args_t<T>; |
| 38 | |
| 39 | if(!(boost::math::isfinite)(v)) |
| 40 | { |
| 41 | return policies::raise_rounding_error("boost::math::round<%1%>(%1%)" , nullptr, static_cast<result_type>(v), static_cast<result_type>(v), pol); |
| 42 | } |
| 43 | // |
| 44 | // The logic here is rather convoluted, but avoids a number of traps, |
| 45 | // see discussion here https://github.com/boostorg/math/pull/8 |
| 46 | // |
| 47 | if (T(-0.5) < v && v < T(0.5)) |
| 48 | { |
| 49 | // special case to avoid rounding error on the direct |
| 50 | // predecessor of +0.5 resp. the direct successor of -0.5 in |
| 51 | // IEEE floating point types |
| 52 | return static_cast<result_type>(0); |
| 53 | } |
| 54 | else if (v > 0) |
| 55 | { |
| 56 | // subtract v from ceil(v) first in order to avoid rounding |
| 57 | // errors on largest representable integer numbers |
| 58 | result_type c(ceil(v)); |
| 59 | return T(0.5) < c - v ? c - 1 : c; |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | // see former branch |
| 64 | result_type f(floor(v)); |
| 65 | return T(0.5) < v - f ? f + 1 : f; |
| 66 | } |
| 67 | } |
| 68 | template <class T, class Policy> |
| 69 | inline tools::promote_args_t<T> round(const T& v, const Policy&, const std::true_type&) |
| 70 | { |
| 71 | return v; |
| 72 | } |
| 73 | |
| 74 | } // namespace detail |
| 75 | |
| 76 | template <class T, class Policy> |
| 77 | inline tools::promote_args_t<T> round(const T& v, const Policy& pol) |
| 78 | { |
| 79 | return detail::round(v, pol, std::integral_constant<bool, detail::is_integer_for_rounding<T>::value>()); |
| 80 | } |
| 81 | template <class T> |
| 82 | inline tools::promote_args_t<T> round(const T& v) |
| 83 | { |
| 84 | return round(v, policies::policy<>()); |
| 85 | } |
| 86 | // |
| 87 | // The following functions will not compile unless T has an |
| 88 | // implicit conversion to the integer types. For user-defined |
| 89 | // number types this will likely not be the case. In that case |
| 90 | // these functions should either be specialized for the UDT in |
| 91 | // question, or else overloads should be placed in the same |
| 92 | // namespace as the UDT: these will then be found via argument |
| 93 | // dependent lookup. See our concept archetypes for examples. |
| 94 | // |
| 95 | // Non-standard numeric limits syntax "(std::numeric_limits<int>::max)()" |
| 96 | // is to avoid macro substiution from MSVC |
| 97 | // https://stackoverflow.com/questions/27442885/syntax-error-with-stdnumeric-limitsmax |
| 98 | // |
| 99 | template <class T, class Policy> |
| 100 | inline int iround(const T& v, const Policy& pol) |
| 101 | { |
| 102 | BOOST_MATH_STD_USING |
| 103 | using result_type = tools::promote_args_t<T>; |
| 104 | |
| 105 | result_type r = boost::math::round(v, pol); |
| 106 | |
| 107 | #ifdef BOOST_MATH_HAS_CONSTEXPR_LDEXP |
| 108 | if constexpr (std::is_arithmetic_v<result_type> |
| 109 | #ifdef BOOST_MATH_FLOAT128_TYPE |
| 110 | && !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type> |
| 111 | #endif |
| 112 | ) |
| 113 | { |
| 114 | constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits); |
| 115 | |
| 116 | if (r >= max_val || r < -max_val) |
| 117 | { |
| 118 | return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::iround<%1%>(%1%)" , nullptr, v, static_cast<int>(0), pol)); |
| 119 | } |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits); |
| 124 | |
| 125 | if (r >= max_val || r < -max_val) |
| 126 | { |
| 127 | return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::iround<%1%>(%1%)" , nullptr, v, static_cast<int>(0), pol)); |
| 128 | } |
| 129 | } |
| 130 | #else |
| 131 | static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits); |
| 132 | |
| 133 | if (r >= max_val || r < -max_val) |
| 134 | { |
| 135 | return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::iround<%1%>(%1%)" , nullptr, v, static_cast<int>(0), pol)); |
| 136 | } |
| 137 | #endif |
| 138 | |
| 139 | return static_cast<int>(r); |
| 140 | } |
| 141 | template <class T> |
| 142 | inline int iround(const T& v) |
| 143 | { |
| 144 | return iround(v, policies::policy<>()); |
| 145 | } |
| 146 | |
| 147 | template <class T, class Policy> |
| 148 | inline long lround(const T& v, const Policy& pol) |
| 149 | { |
| 150 | BOOST_MATH_STD_USING |
| 151 | using result_type = tools::promote_args_t<T>; |
| 152 | |
| 153 | result_type r = boost::math::round(v, pol); |
| 154 | |
| 155 | #ifdef BOOST_MATH_HAS_CONSTEXPR_LDEXP |
| 156 | if constexpr (std::is_arithmetic_v<result_type> |
| 157 | #ifdef BOOST_MATH_FLOAT128_TYPE |
| 158 | && !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type> |
| 159 | #endif |
| 160 | ) |
| 161 | { |
| 162 | constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits); |
| 163 | |
| 164 | if (r >= max_val || r < -max_val) |
| 165 | { |
| 166 | return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::lround<%1%>(%1%)" , nullptr, v, static_cast<long>(0), pol)); |
| 167 | } |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits); |
| 172 | |
| 173 | if (r >= max_val || r < -max_val) |
| 174 | { |
| 175 | return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::lround<%1%>(%1%)" , nullptr, v, static_cast<long>(0), pol)); |
| 176 | } |
| 177 | } |
| 178 | #else |
| 179 | static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits); |
| 180 | |
| 181 | if (r >= max_val || r < -max_val) |
| 182 | { |
| 183 | return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::lround<%1%>(%1%)" , nullptr, v, static_cast<long>(0), pol)); |
| 184 | } |
| 185 | #endif |
| 186 | |
| 187 | return static_cast<long>(r); |
| 188 | } |
| 189 | template <class T> |
| 190 | inline long lround(const T& v) |
| 191 | { |
| 192 | return lround(v, policies::policy<>()); |
| 193 | } |
| 194 | |
| 195 | template <class T, class Policy> |
| 196 | inline long long llround(const T& v, const Policy& pol) |
| 197 | { |
| 198 | BOOST_MATH_STD_USING |
| 199 | using result_type = boost::math::tools::promote_args_t<T>; |
| 200 | |
| 201 | result_type r = boost::math::round(v, pol); |
| 202 | |
| 203 | #ifdef BOOST_MATH_HAS_CONSTEXPR_LDEXP |
| 204 | if constexpr (std::is_arithmetic_v<result_type> |
| 205 | #ifdef BOOST_MATH_FLOAT128_TYPE |
| 206 | && !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type> |
| 207 | #endif |
| 208 | ) |
| 209 | { |
| 210 | constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits); |
| 211 | |
| 212 | if (r >= max_val || r < -max_val) |
| 213 | { |
| 214 | return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::llround<%1%>(%1%)" , nullptr, v, static_cast<long long>(0), pol)); |
| 215 | } |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits); |
| 220 | |
| 221 | if (r >= max_val || r < -max_val) |
| 222 | { |
| 223 | return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::llround<%1%>(%1%)" , nullptr, v, static_cast<long long>(0), pol)); |
| 224 | } |
| 225 | } |
| 226 | #else |
| 227 | static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits); |
| 228 | |
| 229 | if (r >= max_val || r < -max_val) |
| 230 | { |
| 231 | return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::llround<%1%>(%1%)" , nullptr, v, static_cast<long long>(0), pol)); |
| 232 | } |
| 233 | #endif |
| 234 | |
| 235 | return static_cast<long long>(r); |
| 236 | } |
| 237 | template <class T> |
| 238 | inline long long llround(const T& v) |
| 239 | { |
| 240 | return llround(v, policies::policy<>()); |
| 241 | } |
| 242 | |
| 243 | }} // namespaces |
| 244 | |
| 245 | #endif // BOOST_MATH_ROUND_HPP |
| 246 | |