File indexing completed on 2025-01-18 09:40:23
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_MATH_ROUND_HPP
0008 #define BOOST_MATH_ROUND_HPP
0009
0010 #ifdef _MSC_VER
0011 #pragma once
0012 #endif
0013
0014 #include <boost/math/tools/config.hpp>
0015 #include <boost/math/ccmath/detail/config.hpp>
0016 #include <boost/math/policies/error_handling.hpp>
0017 #include <boost/math/special_functions/math_fwd.hpp>
0018 #include <boost/math/special_functions/fpclassify.hpp>
0019 #include <type_traits>
0020 #include <limits>
0021 #include <cmath>
0022
0023 #if !defined(BOOST_MATH_NO_CCMATH) && !defined(BOOST_MATH_NO_CONSTEXPR_DETECTION)
0024 #include <boost/math/ccmath/ldexp.hpp>
0025 # define BOOST_MATH_HAS_CONSTEXPR_LDEXP
0026 #endif
0027
0028 namespace boost{ namespace math{
0029
0030 namespace detail{
0031
0032 template <class T, class Policy>
0033 inline tools::promote_args_t<T> round(const T& v, const Policy& pol, const std::false_type&)
0034 {
0035 BOOST_MATH_STD_USING
0036 using result_type = tools::promote_args_t<T>;
0037
0038 if(!(boost::math::isfinite)(v))
0039 {
0040 return policies::raise_rounding_error("boost::math::round<%1%>(%1%)", nullptr, static_cast<result_type>(v), static_cast<result_type>(v), pol);
0041 }
0042
0043
0044
0045
0046 if (T(-0.5) < v && v < T(0.5))
0047 {
0048
0049
0050
0051 return static_cast<result_type>(0);
0052 }
0053 else if (v > 0)
0054 {
0055
0056
0057 result_type c(ceil(v));
0058 return T(0.5) < c - v ? c - 1 : c;
0059 }
0060 else
0061 {
0062
0063 result_type f(floor(v));
0064 return T(0.5) < v - f ? f + 1 : f;
0065 }
0066 }
0067 template <class T, class Policy>
0068 inline tools::promote_args_t<T> round(const T& v, const Policy&, const std::true_type&)
0069 {
0070 return v;
0071 }
0072
0073 }
0074
0075 template <class T, class Policy>
0076 inline tools::promote_args_t<T> round(const T& v, const Policy& pol)
0077 {
0078 return detail::round(v, pol, std::integral_constant<bool, detail::is_integer_for_rounding<T>::value>());
0079 }
0080 template <class T>
0081 inline tools::promote_args_t<T> round(const T& v)
0082 {
0083 return round(v, policies::policy<>());
0084 }
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098 template <class T, class Policy>
0099 inline int iround(const T& v, const Policy& pol)
0100 {
0101 BOOST_MATH_STD_USING
0102 using result_type = tools::promote_args_t<T>;
0103
0104 result_type r = boost::math::round(v, pol);
0105
0106 #ifdef BOOST_MATH_HAS_CONSTEXPR_LDEXP
0107 if constexpr (std::is_arithmetic_v<result_type>
0108 #ifdef BOOST_MATH_FLOAT128_TYPE
0109 && !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type>
0110 #endif
0111 )
0112 {
0113 constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits);
0114
0115 if (r >= max_val || r < -max_val)
0116 {
0117 return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", nullptr, v, static_cast<int>(0), pol));
0118 }
0119 }
0120 else
0121 {
0122 static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits);
0123
0124 if (r >= max_val || r < -max_val)
0125 {
0126 return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", nullptr, v, static_cast<int>(0), pol));
0127 }
0128 }
0129 #else
0130 static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits);
0131
0132 if (r >= max_val || r < -max_val)
0133 {
0134 return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", nullptr, v, static_cast<int>(0), pol));
0135 }
0136 #endif
0137
0138 return static_cast<int>(r);
0139 }
0140 template <class T>
0141 inline int iround(const T& v)
0142 {
0143 return iround(v, policies::policy<>());
0144 }
0145
0146 template <class T, class Policy>
0147 inline long lround(const T& v, const Policy& pol)
0148 {
0149 BOOST_MATH_STD_USING
0150 using result_type = tools::promote_args_t<T>;
0151
0152 result_type r = boost::math::round(v, pol);
0153
0154 #ifdef BOOST_MATH_HAS_CONSTEXPR_LDEXP
0155 if constexpr (std::is_arithmetic_v<result_type>
0156 #ifdef BOOST_MATH_FLOAT128_TYPE
0157 && !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type>
0158 #endif
0159 )
0160 {
0161 constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits);
0162
0163 if (r >= max_val || r < -max_val)
0164 {
0165 return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", nullptr, v, static_cast<long>(0), pol));
0166 }
0167 }
0168 else
0169 {
0170 static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits);
0171
0172 if (r >= max_val || r < -max_val)
0173 {
0174 return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", nullptr, v, static_cast<long>(0), pol));
0175 }
0176 }
0177 #else
0178 static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits);
0179
0180 if (r >= max_val || r < -max_val)
0181 {
0182 return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", nullptr, v, static_cast<long>(0), pol));
0183 }
0184 #endif
0185
0186 return static_cast<long>(r);
0187 }
0188 template <class T>
0189 inline long lround(const T& v)
0190 {
0191 return lround(v, policies::policy<>());
0192 }
0193
0194 template <class T, class Policy>
0195 inline long long llround(const T& v, const Policy& pol)
0196 {
0197 BOOST_MATH_STD_USING
0198 using result_type = boost::math::tools::promote_args_t<T>;
0199
0200 result_type r = boost::math::round(v, pol);
0201
0202 #ifdef BOOST_MATH_HAS_CONSTEXPR_LDEXP
0203 if constexpr (std::is_arithmetic_v<result_type>
0204 #ifdef BOOST_MATH_FLOAT128_TYPE
0205 && !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type>
0206 #endif
0207 )
0208 {
0209 constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits);
0210
0211 if (r >= max_val || r < -max_val)
0212 {
0213 return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", nullptr, v, static_cast<long long>(0), pol));
0214 }
0215 }
0216 else
0217 {
0218 static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits);
0219
0220 if (r >= max_val || r < -max_val)
0221 {
0222 return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", nullptr, v, static_cast<long long>(0), pol));
0223 }
0224 }
0225 #else
0226 static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits);
0227
0228 if (r >= max_val || r < -max_val)
0229 {
0230 return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", nullptr, v, static_cast<long long>(0), pol));
0231 }
0232 #endif
0233
0234 return static_cast<long long>(r);
0235 }
0236 template <class T>
0237 inline long long llround(const T& v)
0238 {
0239 return llround(v, policies::policy<>());
0240 }
0241
0242 }}
0243
0244 #endif