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