Debian Codegraph @

third-party/boost-math/include/boost/math/ccmath/round.hpp

raw
1 // (C) Copyright Matt Borland 2021.
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_CCMATH_ROUND_HPP
7 #define BOOST_MATH_CCMATH_ROUND_HPP
8
9 #include <stdexcept>
10 #include <boost/math/ccmath/detail/config.hpp>
11
12 #ifdef BOOST_MATH_NO_CCMATH
13 #error "The header <boost/math/round.hpp> can only be used in C++17 and later."
14 #endif
15
16 #include <boost/math/ccmath/abs.hpp>
17 #include <boost/math/ccmath/isinf.hpp>
18 #include <boost/math/ccmath/isnan.hpp>
19 #include <boost/math/ccmath/modf.hpp>
20
21 namespace boost::math::ccmath {
22
23 namespace detail {
24
25 // Computes the nearest integer value to arg (in floating-point format),
26 // rounding halfway cases away from zero, regardless of the current rounding mode.
27 template <typename T>
28 inline constexpr T round_impl(T arg) noexcept
29 {
30 T iptr = 0;
31 const T x = boost::math::ccmath::modf(arg, &iptr);
32 constexpr T half = T(1)/2;
33
34 if(x >= half && iptr > 0)
35 {
36 return iptr + 1;
37 }
38 else if(boost::math::ccmath::abs(x) >= half && iptr < 0)
39 {
40 return iptr - 1;
41 }
42 else
43 {
44 return iptr;
45 }
46 }
47
48 template <typename ReturnType, typename T>
49 inline constexpr ReturnType int_round_impl(T arg)
50 {
51 const T rounded_arg = round_impl(arg);
52
53 if(rounded_arg > static_cast<T>((std::numeric_limits<ReturnType>::max)()))
54 {
55 if constexpr (std::is_same_v<ReturnType, long long>)
56 {
57 throw std::domain_error("Rounded value cannot be represented by a long long type without overflow");
58 }
59 else
60 {
61 throw std::domain_error("Rounded value cannot be represented by a long type without overflow");
62 }
63 }
64 else
65 {
66 return static_cast<ReturnType>(rounded_arg);
67 }
68 }
69
70 } // Namespace detail
71
72 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
73 inline constexpr Real round(Real arg) noexcept
74 {
75 if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
76 {
77 return boost::math::ccmath::abs(arg) == Real(0) ? arg :
78 boost::math::ccmath::isinf(arg) ? arg :
79 boost::math::ccmath::isnan(arg) ? arg :
80 boost::math::ccmath::detail::round_impl(arg);
81 }
82 else
83 {
84 using std::round;
85 return round(arg);
86 }
87 }
88
89 template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
90 inline constexpr double round(Z arg) noexcept
91 {
92 return boost::math::ccmath::round(static_cast<double>(arg));
93 }
94
95 inline constexpr float roundf(float arg) noexcept
96 {
97 return boost::math::ccmath::round(arg);
98 }
99
100 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
101 inline constexpr long double roundl(long double arg) noexcept
102 {
103 return boost::math::ccmath::round(arg);
104 }
105 #endif
106
107 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
108 inline constexpr long lround(Real arg)
109 {
110 if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
111 {
112 return boost::math::ccmath::abs(arg) == Real(0) ? 0l :
113 boost::math::ccmath::isinf(arg) ? 0l :
114 boost::math::ccmath::isnan(arg) ? 0l :
115 boost::math::ccmath::detail::int_round_impl<long>(arg);
116 }
117 else
118 {
119 using std::lround;
120 return lround(arg);
121 }
122 }
123
124 template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
125 inline constexpr long lround(Z arg)
126 {
127 return boost::math::ccmath::lround(static_cast<double>(arg));
128 }
129
130 inline constexpr long lroundf(float arg)
131 {
132 return boost::math::ccmath::lround(arg);
133 }
134
135 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
136 inline constexpr long lroundl(long double arg)
137 {
138 return boost::math::ccmath::lround(arg);
139 }
140 #endif
141
142 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
143 inline constexpr long long llround(Real arg)
144 {
145 if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
146 {
147 return boost::math::ccmath::abs(arg) == Real(0) ? 0ll :
148 boost::math::ccmath::isinf(arg) ? 0ll :
149 boost::math::ccmath::isnan(arg) ? 0ll :
150 boost::math::ccmath::detail::int_round_impl<long long>(arg);
151 }
152 else
153 {
154 using std::llround;
155 return llround(arg);
156 }
157 }
158
159 template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
160 inline constexpr long llround(Z arg)
161 {
162 return boost::math::ccmath::llround(static_cast<double>(arg));
163 }
164
165 inline constexpr long long llroundf(float arg)
166 {
167 return boost::math::ccmath::llround(arg);
168 }
169
170 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
171 inline constexpr long long llroundl(long double arg)
172 {
173 return boost::math::ccmath::llround(arg);
174 }
175 #endif
176
177 } // Namespaces
178
179 #endif // BOOST_MATH_CCMATH_ROUND_HPP