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