File indexing completed on 2025-07-15 08:40:20
0001
0002
0003
0004
0005
0006 #ifndef BOOST_MP_DETAIL_FUNCTIONS_TRUNC_HPP
0007 #define BOOST_MP_DETAIL_FUNCTIONS_TRUNC_HPP
0008
0009 #include <cmath>
0010 #include <limits>
0011 #include <stdexcept>
0012 #include <boost/multiprecision/detail/standalone_config.hpp>
0013 #include <boost/multiprecision/detail/no_exceptions_support.hpp>
0014
0015 #ifdef BOOST_MP_MATH_AVAILABLE
0016 #include <boost/math/special_functions/trunc.hpp>
0017 #endif
0018
0019 namespace boost { namespace multiprecision { namespace detail {
0020
0021 namespace impl {
0022
0023 template <typename T>
0024 inline T trunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg)
0025 {
0026 if (arg > 0)
0027 {
0028 using std::floor;
0029
0030 return floor(arg);
0031 }
0032
0033 using std::ceil;
0034
0035 return ceil(arg);}
0036 }
0037
0038 #ifdef BOOST_MP_MATH_AVAILABLE
0039
0040 template <typename T>
0041 inline long long lltrunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg)
0042 {
0043 return boost::math::lltrunc(arg);
0044 }
0045
0046 template <typename T>
0047 inline int itrunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg)
0048 {
0049 return boost::math::itrunc(arg);
0050 }
0051
0052 #else
0053
0054 template <typename T>
0055 inline long long lltrunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg)
0056 {
0057 T t = boost::multiprecision::detail::impl::trunc(arg);
0058 if (t > LLONG_MAX)
0059 {
0060 BOOST_MP_THROW_EXCEPTION(std::domain_error("arg cannot be converted into a long long"));
0061 }
0062
0063 return static_cast<long long>(t);
0064 }
0065
0066 template <typename T>
0067 inline int itrunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg)
0068 {
0069 T t = boost::multiprecision::detail::impl::trunc(arg);
0070 if (t > static_cast<T>(INT_MAX))
0071 {
0072 BOOST_MP_THROW_EXCEPTION(std::domain_error("arg cannot be converted into an int"));
0073 }
0074
0075 return static_cast<int>(t);
0076 }
0077
0078 #endif
0079
0080 }}}
0081
0082 #endif