File indexing completed on 2025-01-18 09:42:16
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 using std::floor;
0027 using std::ceil;
0028
0029 return (arg > 0) ? floor(arg) : ceil(arg);
0030 }
0031
0032 }
0033
0034 #ifdef BOOST_MP_MATH_AVAILABLE
0035
0036 template <typename T>
0037 inline long long lltrunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg)
0038 {
0039 return boost::math::lltrunc(arg);
0040 }
0041
0042 template <typename T>
0043 inline int itrunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg)
0044 {
0045 return boost::math::itrunc(arg);
0046 }
0047
0048 #else
0049
0050 template <typename T>
0051 inline long long lltrunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg)
0052 {
0053 T t = boost::multiprecision::detail::impl::trunc(arg);
0054 if (t > LLONG_MAX)
0055 {
0056 BOOST_MP_THROW_EXCEPTION(std::domain_error("arg cannot be converted into a long long"));
0057 }
0058
0059 return static_cast<long long>(t);
0060 }
0061
0062 template <typename T>
0063 inline int itrunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg)
0064 {
0065 T t = boost::multiprecision::detail::impl::trunc(arg);
0066 if (t > static_cast<T>(INT_MAX))
0067 {
0068 BOOST_MP_THROW_EXCEPTION(std::domain_error("arg cannot be converted into an int"));
0069 }
0070
0071 return static_cast<int>(t);
0072 }
0073
0074 #endif
0075
0076 }}}
0077
0078 #endif