]> git.donarmstrong.com Git - rsem.git/blob - boost/math/special_functions/round.hpp
Updated boost to v1.55.0
[rsem.git] / boost / math / special_functions / round.hpp
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_ROUND_HPP
7 #define BOOST_MATH_ROUND_HPP
8
9 #ifdef _MSC_VER
10 #pragma once
11 #endif
12
13 #include <boost/math/tools/config.hpp>
14 #include <boost/math/policies/error_handling.hpp>
15 #include <boost/math/special_functions/fpclassify.hpp>
16
17 namespace boost{ namespace math{
18
19 template <class T, class Policy>
20 inline typename tools::promote_args<T>::type round(const T& v, const Policy& pol)
21 {
22    BOOST_MATH_STD_USING
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));
27 }
28 template <class T>
29 inline typename tools::promote_args<T>::type round(const T& v)
30 {
31    return round(v, policies::policy<>());
32 }
33 //
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.
41 //
42 template <class T, class Policy>
43 inline int iround(const T& v, const Policy& pol)
44 {
45    BOOST_MATH_STD_USING
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);
50 }
51 template <class T>
52 inline int iround(const T& v)
53 {
54    return iround(v, policies::policy<>());
55 }
56
57 template <class T, class Policy>
58 inline long lround(const T& v, const Policy& pol)
59 {
60    BOOST_MATH_STD_USING
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);
65 }
66 template <class T>
67 inline long lround(const T& v)
68 {
69    return lround(v, policies::policy<>());
70 }
71
72 #ifdef BOOST_HAS_LONG_LONG
73
74 template <class T, class Policy>
75 inline boost::long_long_type llround(const T& v, const Policy& pol)
76 {
77    BOOST_MATH_STD_USING
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);
82 }
83 template <class T>
84 inline boost::long_long_type llround(const T& v)
85 {
86    return llround(v, policies::policy<>());
87 }
88
89 #endif
90
91 }} // namespaces
92
93 #endif // BOOST_MATH_ROUND_HPP