| 1 | // Copyright John Maddock 2007. |
| 2 | // Use, modification and distribution are subject to the |
| 3 | // Boost Software License, Version 1.0. (See accompanying file |
| 4 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | #ifndef BOOST_MATH_TRUNC_HPP |
| 7 | #define BOOST_MATH_TRUNC_HPP |
| 8 | |
| 9 | #ifdef _MSC_VER |
| 10 | #pragma once |
| 11 | #endif |
| 12 | |
| 13 | #include <boost/math/special_functions/math_fwd.hpp> |
| 14 | #include <boost/math/tools/config.hpp> |
| 15 | #include <boost/math/policies/error_handling.hpp> |
| 16 | #include <boost/math/special_functions/fpclassify.hpp> |
| 17 | #include <boost/type_traits/is_constructible.hpp> |
| 18 | #include <boost/core/enable_if.hpp> |
| 19 | |
| 20 | namespace boost{ namespace math{ namespace detail{ |
| 21 | |
| 22 | template <class T, class Policy> |
| 23 | inline typename tools::promote_args<T>::type trunc(const T& v, const Policy& pol, const boost::false_type&) |
| 24 | { |
| 25 | BOOST_MATH_STD_USING |
| 26 | typedef typename tools::promote_args<T>::type result_type; |
| 27 | if(!(boost::math::isfinite)(v)) |
| 28 | return policies::raise_rounding_error("boost::math::trunc<%1%>(%1%)" , 0, static_cast<result_type>(v), static_cast<result_type>(v), pol); |
| 29 | return (v >= 0) ? static_cast<result_type>(floor(v)) : static_cast<result_type>(ceil(v)); |
| 30 | } |
| 31 | |
| 32 | template <class T, class Policy> |
| 33 | inline typename tools::promote_args<T>::type trunc(const T& v, const Policy&, const boost::true_type&) |
| 34 | { |
| 35 | return v; |
| 36 | } |
| 37 | |
| 38 | } |
| 39 | |
| 40 | template <class T, class Policy> |
| 41 | inline typename tools::promote_args<T>::type trunc(const T& v, const Policy& pol) |
| 42 | { |
| 43 | return detail::trunc(v, pol, boost::integral_constant<bool, detail::is_integer_for_rounding<T>::value>()); |
| 44 | } |
| 45 | template <class T> |
| 46 | inline typename tools::promote_args<T>::type trunc(const T& v) |
| 47 | { |
| 48 | return trunc(v, policies::policy<>()); |
| 49 | } |
| 50 | // |
| 51 | // The following functions will not compile unless T has an |
| 52 | // implicit conversion to the integer types. For user-defined |
| 53 | // number types this will likely not be the case. In that case |
| 54 | // these functions should either be specialized for the UDT in |
| 55 | // question, or else overloads should be placed in the same |
| 56 | // namespace as the UDT: these will then be found via argument |
| 57 | // dependent lookup. See our concept archetypes for examples. |
| 58 | // |
| 59 | template <class T, class Policy> |
| 60 | inline int itrunc(const T& v, const Policy& pol) |
| 61 | { |
| 62 | BOOST_MATH_STD_USING |
| 63 | typedef typename tools::promote_args<T>::type result_type; |
| 64 | result_type r = boost::math::trunc(v, pol); |
| 65 | if((r > (std::numeric_limits<int>::max)()) || (r < (std::numeric_limits<int>::min)())) |
| 66 | return static_cast<int>(policies::raise_rounding_error("boost::math::itrunc<%1%>(%1%)" , 0, static_cast<result_type>(v), 0, pol)); |
| 67 | return static_cast<int>(r); |
| 68 | } |
| 69 | template <class T> |
| 70 | inline int itrunc(const T& v) |
| 71 | { |
| 72 | return itrunc(v, policies::policy<>()); |
| 73 | } |
| 74 | |
| 75 | template <class T, class Policy> |
| 76 | inline long ltrunc(const T& v, const Policy& pol) |
| 77 | { |
| 78 | BOOST_MATH_STD_USING |
| 79 | typedef typename tools::promote_args<T>::type result_type; |
| 80 | result_type r = boost::math::trunc(v, pol); |
| 81 | if((r > (std::numeric_limits<long>::max)()) || (r < (std::numeric_limits<long>::min)())) |
| 82 | return static_cast<long>(policies::raise_rounding_error("boost::math::ltrunc<%1%>(%1%)" , 0, static_cast<result_type>(v), 0L, pol)); |
| 83 | return static_cast<long>(r); |
| 84 | } |
| 85 | template <class T> |
| 86 | inline long ltrunc(const T& v) |
| 87 | { |
| 88 | return ltrunc(v, policies::policy<>()); |
| 89 | } |
| 90 | |
| 91 | #ifdef BOOST_HAS_LONG_LONG |
| 92 | |
| 93 | template <class T, class Policy> |
| 94 | inline boost::long_long_type lltrunc(const T& v, const Policy& pol) |
| 95 | { |
| 96 | BOOST_MATH_STD_USING |
| 97 | typedef typename tools::promote_args<T>::type result_type; |
| 98 | result_type r = boost::math::trunc(v, pol); |
| 99 | if((r > (std::numeric_limits<boost::long_long_type>::max)()) || (r < (std::numeric_limits<boost::long_long_type>::min)())) |
| 100 | return static_cast<boost::long_long_type>(policies::raise_rounding_error("boost::math::lltrunc<%1%>(%1%)" , 0, v, static_cast<boost::long_long_type>(0), pol)); |
| 101 | return static_cast<boost::long_long_type>(r); |
| 102 | } |
| 103 | template <class T> |
| 104 | inline boost::long_long_type lltrunc(const T& v) |
| 105 | { |
| 106 | return lltrunc(v, policies::policy<>()); |
| 107 | } |
| 108 | |
| 109 | #endif |
| 110 | |
| 111 | template <class T, class Policy> |
| 112 | inline typename boost::enable_if_c<boost::is_constructible<int, T>::value, int>::type |
| 113 | iconvert(const T& v, const Policy&) |
| 114 | { |
| 115 | return static_cast<int>(v); |
| 116 | } |
| 117 | |
| 118 | template <class T, class Policy> |
| 119 | inline typename boost::disable_if_c<boost::is_constructible<int, T>::value, int>::type |
| 120 | iconvert(const T& v, const Policy& pol) |
| 121 | { |
| 122 | using boost::math::itrunc; |
| 123 | return itrunc(v, pol); |
| 124 | } |
| 125 | |
| 126 | template <class T, class Policy> |
| 127 | inline typename boost::enable_if_c<boost::is_constructible<long, T>::value, long>::type |
| 128 | lconvert(const T& v, const Policy&) |
| 129 | { |
| 130 | return static_cast<long>(v); |
| 131 | } |
| 132 | |
| 133 | template <class T, class Policy> |
| 134 | inline typename boost::disable_if_c<boost::is_constructible<long, T>::value, long>::type |
| 135 | lconvert(const T& v, const Policy& pol) |
| 136 | { |
| 137 | using boost::math::ltrunc; |
| 138 | return ltrunc(v, pol); |
| 139 | } |
| 140 | |
| 141 | #ifdef BOOST_HAS_LONG_LONG |
| 142 | |
| 143 | template <class T, class Policy> |
| 144 | inline typename boost::enable_if_c<boost::is_constructible<boost::long_long_type, T>::value, boost::long_long_type>::type |
| 145 | llconvertert(const T& v, const Policy&) |
| 146 | { |
| 147 | return static_cast<boost::long_long_type>(v); |
| 148 | } |
| 149 | |
| 150 | template <class T, class Policy> |
| 151 | inline typename boost::disable_if_c<boost::is_constructible<boost::long_long_type, T>::value, boost::long_long_type>::type |
| 152 | llconvertert(const T& v, const Policy& pol) |
| 153 | { |
| 154 | using boost::math::lltrunc; |
| 155 | return lltrunc(v, pol); |
| 156 | } |
| 157 | |
| 158 | #endif |
| 159 | |
| 160 | }} // namespaces |
| 161 | |
| 162 | #endif // BOOST_MATH_TRUNC_HPP |
| 163 | |