/////////////////////////////////////////////////////////////////////////////// // Copyright 2022 Matt Borland. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_MP_DETAIL_FUNCTIONS_TRUNC_HPP #define BOOST_MP_DETAIL_FUNCTIONS_TRUNC_HPP #include #include #include #include #include #ifdef BOOST_MP_MATH_AVAILABLE #include #endif namespace boost { namespace multiprecision { namespace detail { namespace impl { template inline T trunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg) { using std::floor; using std::ceil; return (arg > 0) ? floor(arg) : ceil(arg); } } // namespace impl #ifdef BOOST_MP_MATH_AVAILABLE template inline long long lltrunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg) { return boost::math::lltrunc(arg); } template inline int itrunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg) { return boost::math::itrunc(arg); } #else template inline long long lltrunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg) { if (arg > LLONG_MAX) { BOOST_MP_THROW_EXCEPTION(std::domain_error("arg cannot be converted into a long long")); } return static_cast(boost::multiprecision::detail::impl::trunc(arg)); } template inline int itrunc BOOST_PREVENT_MACRO_SUBSTITUTION (const T arg) { if (arg > static_cast(INT_MAX)) { BOOST_MP_THROW_EXCEPTION(std::domain_error("arg cannot be converted into an int")); } return static_cast(boost::multiprecision::detail::impl::trunc(arg)); } #endif }}} // Namespaces #endif // BOOST_MP_DETAIL_FUNCTIONS_TRUNC_HPP