]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/math/special_functions/round.hpp
bump version to 18.2.4-pve3
[ceph.git] / ceph / src / boost / 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/math_fwd.hpp>
16 #include <boost/math/special_functions/fpclassify.hpp>
17
18 namespace boost{ namespace math{
19
20 namespace detail{
21
22 template <class T, class Policy>
23 inline typename tools::promote_args<T>::type round(const T& v, const Policy& pol, const std::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::round<%1%>(%1%)", 0, static_cast<result_type>(v), static_cast<result_type>(v), pol);
29 //
30 // The logic here is rather convoluted, but avoids a number of traps,
31 // see discussion here https://github.com/boostorg/math/pull/8
32 //
33 if (-0.5 < v && v < 0.5)
34 {
35 // special case to avoid rounding error on the direct
36 // predecessor of +0.5 resp. the direct successor of -0.5 in
37 // IEEE floating point types
38 return static_cast<result_type>(0);
39 }
40 else if (v > 0)
41 {
42 // subtract v from ceil(v) first in order to avoid rounding
43 // errors on largest representable integer numbers
44 result_type c(ceil(v));
45 return 0.5 < c - v ? c - 1 : c;
46 }
47 else
48 {
49 // see former branch
50 result_type f(floor(v));
51 return 0.5 < v - f ? f + 1 : f;
52 }
53 }
54 template <class T, class Policy>
55 inline typename tools::promote_args<T>::type round(const T& v, const Policy&, const std::true_type&)
56 {
57 return v;
58 }
59
60 } // namespace detail
61
62 template <class T, class Policy>
63 inline typename tools::promote_args<T>::type round(const T& v, const Policy& pol)
64 {
65 return detail::round(v, pol, std::integral_constant<bool, detail::is_integer_for_rounding<T>::value>());
66 }
67 template <class T>
68 inline typename tools::promote_args<T>::type round(const T& v)
69 {
70 return round(v, policies::policy<>());
71 }
72 //
73 // The following functions will not compile unless T has an
74 // implicit conversion to the integer types. For user-defined
75 // number types this will likely not be the case. In that case
76 // these functions should either be specialized for the UDT in
77 // question, or else overloads should be placed in the same
78 // namespace as the UDT: these will then be found via argument
79 // dependent lookup. See our concept archetypes for examples.
80 //
81 // Non-standard numeric limits syntax "(std::numeric_limits<int>::max)()"
82 // is to avoid macro substiution from MSVC
83 // https://stackoverflow.com/questions/27442885/syntax-error-with-stdnumeric-limitsmax
84 //
85 template <class T, class Policy>
86 inline int iround(const T& v, const Policy& pol)
87 {
88 BOOST_MATH_STD_USING
89 typedef typename tools::promote_args<T>::type result_type;
90 T r = boost::math::round(v, pol);
91 if(r > static_cast<result_type>((std::numeric_limits<int>::max)()) || r < static_cast<result_type>((std::numeric_limits<int>::min)()))
92 return static_cast<int>(policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", 0, v, 0, pol));
93 return static_cast<int>(r);
94 }
95 template <class T>
96 inline int iround(const T& v)
97 {
98 return iround(v, policies::policy<>());
99 }
100
101 template <class T, class Policy>
102 inline long lround(const T& v, const Policy& pol)
103 {
104 BOOST_MATH_STD_USING
105 typedef typename tools::promote_args<T>::type result_type;
106 T r = boost::math::round(v, pol);
107 if(r > static_cast<result_type>((std::numeric_limits<long>::max)()) || r < static_cast<result_type>((std::numeric_limits<long>::min)()))
108 return static_cast<long int>(policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", 0, v, 0L, pol));
109 return static_cast<long int>(r);
110 }
111 template <class T>
112 inline long lround(const T& v)
113 {
114 return lround(v, policies::policy<>());
115 }
116
117 template <class T, class Policy>
118 inline long long llround(const T& v, const Policy& pol)
119 {
120 BOOST_MATH_STD_USING
121 typedef typename tools::promote_args<T>::type result_type;
122 T r = boost::math::round(v, pol);
123 if(r > static_cast<result_type>((std::numeric_limits<long long>::max)()) ||
124 r < static_cast<result_type>((std::numeric_limits<long long>::min)()))
125 {
126 return static_cast<long long>(policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", 0, v, static_cast<long long>(0), pol));
127 }
128 return static_cast<long long>(r);
129 }
130 template <class T>
131 inline long long llround(const T& v)
132 {
133 return llround(v, policies::policy<>());
134 }
135
136 }} // namespaces
137
138 #endif // BOOST_MATH_ROUND_HPP