Chris@16: // Copyright John Maddock 2007. Chris@16: // Use, modification and distribution are subject to the Chris@16: // Boost Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_MATH_TRUNC_HPP Chris@16: #define BOOST_MATH_TRUNC_HPP Chris@16: Chris@16: #ifdef _MSC_VER Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@101: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@101: namespace boost{ namespace math{ namespace detail{ Chris@16: Chris@16: template Chris@101: inline typename tools::promote_args::type trunc(const T& v, const Policy& pol, const mpl::false_&) Chris@16: { Chris@16: BOOST_MATH_STD_USING Chris@16: typedef typename tools::promote_args::type result_type; Chris@16: if(!(boost::math::isfinite)(v)) Chris@16: return policies::raise_rounding_error("boost::math::trunc<%1%>(%1%)", 0, static_cast(v), static_cast(v), pol); Chris@16: return (v >= 0) ? static_cast(floor(v)) : static_cast(ceil(v)); Chris@16: } Chris@101: Chris@101: template Chris@101: inline typename tools::promote_args::type trunc(const T& v, const Policy&, const mpl::true_&) Chris@101: { Chris@101: return v; Chris@101: } Chris@101: Chris@101: } Chris@101: Chris@101: template Chris@101: inline typename tools::promote_args::type trunc(const T& v, const Policy& pol) Chris@101: { Chris@101: return detail::trunc(v, pol, mpl::bool_::value>()); Chris@101: } Chris@16: template Chris@16: inline typename tools::promote_args::type trunc(const T& v) Chris@16: { Chris@16: return trunc(v, policies::policy<>()); Chris@16: } Chris@16: // Chris@16: // The following functions will not compile unless T has an Chris@16: // implicit convertion to the integer types. For user-defined Chris@16: // number types this will likely not be the case. In that case Chris@16: // these functions should either be specialized for the UDT in Chris@16: // question, or else overloads should be placed in the same Chris@16: // namespace as the UDT: these will then be found via argument Chris@16: // dependent lookup. See our concept archetypes for examples. Chris@16: // Chris@16: template Chris@16: inline int itrunc(const T& v, const Policy& pol) Chris@16: { Chris@16: BOOST_MATH_STD_USING Chris@16: typedef typename tools::promote_args::type result_type; Chris@16: result_type r = boost::math::trunc(v, pol); Chris@16: if((r > (std::numeric_limits::max)()) || (r < (std::numeric_limits::min)())) Chris@16: return static_cast(policies::raise_rounding_error("boost::math::itrunc<%1%>(%1%)", 0, static_cast(v), 0, pol)); Chris@16: return static_cast(r); Chris@16: } Chris@16: template Chris@16: inline int itrunc(const T& v) Chris@16: { Chris@16: return itrunc(v, policies::policy<>()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline long ltrunc(const T& v, const Policy& pol) Chris@16: { Chris@16: BOOST_MATH_STD_USING Chris@16: typedef typename tools::promote_args::type result_type; Chris@16: result_type r = boost::math::trunc(v, pol); Chris@16: if((r > (std::numeric_limits::max)()) || (r < (std::numeric_limits::min)())) Chris@16: return static_cast(policies::raise_rounding_error("boost::math::ltrunc<%1%>(%1%)", 0, static_cast(v), 0L, pol)); Chris@16: return static_cast(r); Chris@16: } Chris@16: template Chris@16: inline long ltrunc(const T& v) Chris@16: { Chris@16: return ltrunc(v, policies::policy<>()); Chris@16: } Chris@16: Chris@16: #ifdef BOOST_HAS_LONG_LONG Chris@16: Chris@16: template Chris@16: inline boost::long_long_type lltrunc(const T& v, const Policy& pol) Chris@16: { Chris@16: BOOST_MATH_STD_USING Chris@16: typedef typename tools::promote_args::type result_type; Chris@16: result_type r = boost::math::trunc(v, pol); Chris@16: if((r > (std::numeric_limits::max)()) || (r < (std::numeric_limits::min)())) Chris@16: return static_cast(policies::raise_rounding_error("boost::math::lltrunc<%1%>(%1%)", 0, v, static_cast(0), pol)); Chris@16: return static_cast(r); Chris@16: } Chris@16: template Chris@16: inline boost::long_long_type lltrunc(const T& v) Chris@16: { Chris@16: return lltrunc(v, policies::policy<>()); Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: }} // namespaces Chris@16: Chris@16: #endif // BOOST_MATH_TRUNC_HPP