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)
6 #ifndef BOOST_MATH_TRUNC_HPP
7 #define BOOST_MATH_TRUNC_HPP
13 #include <boost/math/tools/config.hpp>
14 #include <boost/math/policies/error_handling.hpp>
15 #include <boost/math/special_functions/fpclassify.hpp>
17 namespace boost{ namespace math{
19 template <class T, class Policy>
20 inline typename tools::promote_args<T>::type trunc(const T& v, const Policy& pol)
23 typedef typename tools::promote_args<T>::type result_type;
24 if(!(boost::math::isfinite)(v))
25 return policies::raise_rounding_error("boost::math::trunc<%1%>(%1%)", 0, static_cast<result_type>(v), static_cast<result_type>(v), pol);
26 return (v >= 0) ? static_cast<result_type>(floor(v)) : static_cast<result_type>(ceil(v));
29 inline typename tools::promote_args<T>::type trunc(const T& v)
31 return trunc(v, policies::policy<>());
34 // The following functions will not compile unless T has an
35 // implicit convertion to the integer types. For user-defined
36 // number types this will likely not be the case. In that case
37 // these functions should either be specialized for the UDT in
38 // question, or else overloads should be placed in the same
39 // namespace as the UDT: these will then be found via argument
40 // dependent lookup. See our concept archetypes for examples.
42 template <class T, class Policy>
43 inline int itrunc(const T& v, const Policy& pol)
46 typedef typename tools::promote_args<T>::type result_type;
47 result_type r = boost::math::trunc(v, pol);
48 if((r > (std::numeric_limits<int>::max)()) || (r < (std::numeric_limits<int>::min)()))
49 return static_cast<int>(policies::raise_rounding_error("boost::math::itrunc<%1%>(%1%)", 0, static_cast<result_type>(v), 0, pol));
50 return static_cast<int>(r);
53 inline int itrunc(const T& v)
55 return itrunc(v, policies::policy<>());
58 template <class T, class Policy>
59 inline long ltrunc(const T& v, const Policy& pol)
62 typedef typename tools::promote_args<T>::type result_type;
63 result_type r = boost::math::trunc(v, pol);
64 if((r > (std::numeric_limits<long>::max)()) || (r < (std::numeric_limits<long>::min)()))
65 return static_cast<long>(policies::raise_rounding_error("boost::math::ltrunc<%1%>(%1%)", 0, static_cast<result_type>(v), 0L, pol));
66 return static_cast<long>(r);
69 inline long ltrunc(const T& v)
71 return ltrunc(v, policies::policy<>());
74 #ifdef BOOST_HAS_LONG_LONG
76 template <class T, class Policy>
77 inline boost::long_long_type lltrunc(const T& v, const Policy& pol)
80 typedef typename tools::promote_args<T>::type result_type;
81 result_type r = boost::math::trunc(v, pol);
82 if((r > (std::numeric_limits<boost::long_long_type>::max)()) || (r < (std::numeric_limits<boost::long_long_type>::min)()))
83 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));
84 return static_cast<boost::long_long_type>(r);
87 inline boost::long_long_type lltrunc(const T& v)
89 return lltrunc(v, policies::policy<>());
96 #endif // BOOST_MATH_TRUNC_HPP