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_ROUND_HPP
7 #define BOOST_MATH_ROUND_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 round(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::round<%1%>(%1%)", 0, static_cast<result_type>(v), static_cast<result_type>(v), pol);
26 return v < 0 ? static_cast<result_type>(ceil(v - 0.5f)) : static_cast<result_type>(floor(v + 0.5f));
29 inline typename tools::promote_args<T>::type round(const T& v)
31 return round(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 iround(const T& v, const Policy& pol)
46 T r = boost::math::round(v, pol);
47 if((r > (std::numeric_limits<int>::max)()) || (r < (std::numeric_limits<int>::min)()))
48 return static_cast<int>(policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", 0, v, 0, pol));
49 return static_cast<int>(r);
52 inline int iround(const T& v)
54 return iround(v, policies::policy<>());
57 template <class T, class Policy>
58 inline long lround(const T& v, const Policy& pol)
61 T r = boost::math::round(v, pol);
62 if((r > (std::numeric_limits<long>::max)()) || (r < (std::numeric_limits<long>::min)()))
63 return static_cast<long int>(policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", 0, v, 0L, pol));
64 return static_cast<long int>(r);
67 inline long lround(const T& v)
69 return lround(v, policies::policy<>());
72 #ifdef BOOST_HAS_LONG_LONG
74 template <class T, class Policy>
75 inline boost::long_long_type llround(const T& v, const Policy& pol)
78 T r = boost::math::round(v, pol);
79 if((r > (std::numeric_limits<boost::long_long_type>::max)()) || (r < (std::numeric_limits<boost::long_long_type>::min)()))
80 return static_cast<boost::long_long_type>(policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", 0, v, static_cast<boost::long_long_type>(0), pol));
81 return static_cast<boost::long_long_type>(r);
84 inline boost::long_long_type llround(const T& v)
86 return llround(v, policies::policy<>());
93 #endif // BOOST_MATH_ROUND_HPP