| 1 |
// Copyright John Maddock 2007. |
| 2 |
// Copyright Matt Borland 2023. |
| 3 |
// Use, modification and distribution are subject to the |
| 4 |
// Boost Software License, Version 1.0. (See accompanying file |
| 5 |
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 |
|
| 7 |
#ifndef BOOST_MATH_ROUND_HPP |
| 8 |
#define BOOST_MATH_ROUND_HPP |
| 9 |
|
| 10 |
#ifdef _MSC_VER |
| 11 |
#pragma once |
| 12 |
#endif |
| 13 |
|
| 14 |
#include <boost/math/tools/config.hpp> |
| 15 |
|
| 16 |
#ifndef BOOST_MATH_HAS_NVRTC |
| 17 |
|
| 18 |
#include <boost/math/ccmath/detail/config.hpp> |
| 19 |
#include <boost/math/policies/error_handling.hpp> |
| 20 |
#include <boost/math/special_functions/math_fwd.hpp> |
| 21 |
#include <boost/math/special_functions/fpclassify.hpp> |
| 22 |
#include <type_traits> |
| 23 |
#include <limits> |
| 24 |
#include <cmath> |
| 25 |
|
| 26 |
#if !defined(BOOST_MATH_NO_CCMATH) && !defined(BOOST_MATH_NO_CONSTEXPR_DETECTION) |
| 27 |
#include <boost/math/ccmath/ldexp.hpp> |
| 28 |
# define BOOST_MATH_HAS_CONSTEXPR_LDEXP |
| 29 |
#endif |
| 30 |
|
| 31 |
namespace boost{ namespace math{ |
| 32 |
|
| 33 |
namespace detail{ |
| 34 |
|
| 35 |
template <class T, class Policy> |
| 36 |
BOOST_MATH_GPU_ENABLED inline tools::promote_args_t<T> round(const T& v, const Policy& pol, const std::false_type&) |
| 37 |
{ |
| 38 |
BOOST_MATH_STD_USING |
| 39 |
using result_type = tools::promote_args_t<T>; |
| 40 |
|
| 41 |
if(!(boost::math::isfinite)(v)) |
| 42 |
{ |
| 43 |
return policies::raise_rounding_error("boost::math::round<%1%>(%1%)", nullptr, static_cast<result_type>(v), static_cast<result_type>(v), pol); |
| 44 |
} |
| 45 |
// |
| 46 |
// The logic here is rather convoluted, but avoids a number of traps, |
| 47 |
// see discussion here https://github.com/boostorg/math/pull/8 |
| 48 |
// |
| 49 |
if (T(-0.5) < v && v < T(0.5)) |
| 50 |
{ |
| 51 |
// special case to avoid rounding error on the direct |
| 52 |
// predecessor of +0.5 resp. the direct successor of -0.5 in |
| 53 |
// IEEE floating point types |
| 54 |
return static_cast<result_type>(0); |
| 55 |
} |
| 56 |
else if (v > 0) |
| 57 |
{ |
| 58 |
// subtract v from ceil(v) first in order to avoid rounding |
| 59 |
// errors on largest representable integer numbers |
| 60 |
result_type c(ceil(v)); |
| 61 |
return T(0.5) < c - v ? c - 1 : c; |
| 62 |
} |
| 63 |
else |
| 64 |
{ |
| 65 |
// see former branch |
| 66 |
result_type f(floor(v)); |
| 67 |
return T(0.5) < v - f ? f + 1 : f; |
| 68 |
} |
| 69 |
} |
| 70 |
template <class T, class Policy> |
| 71 |
BOOST_MATH_GPU_ENABLED inline tools::promote_args_t<T> round(const T& v, const Policy&, const std::true_type&) |
| 72 |
{ |
| 73 |
return v; |
| 74 |
} |
| 75 |
|
| 76 |
} // namespace detail |
| 77 |
|
| 78 |
template <class T, class Policy> |
| 79 |
BOOST_MATH_GPU_ENABLED inline tools::promote_args_t<T> round(const T& v, const Policy& pol) |
| 80 |
{ |
| 81 |
return detail::round(v, pol, std::integral_constant<bool, detail::is_integer_for_rounding<T>::value>()); |
| 82 |
} |
| 83 |
template <class T> |
| 84 |
BOOST_MATH_GPU_ENABLED inline tools::promote_args_t<T> round(const T& v) |
| 85 |
{ |
| 86 |
return round(v, policies::policy<>()); |
| 87 |
} |
| 88 |
// |
| 89 |
// The following functions will not compile unless T has an |
| 90 |
// implicit conversion to the integer types. For user-defined |
| 91 |
// number types this will likely not be the case. In that case |
| 92 |
// these functions should either be specialized for the UDT in |
| 93 |
// question, or else overloads should be placed in the same |
| 94 |
// namespace as the UDT: these will then be found via argument |
| 95 |
// dependent lookup. See our concept archetypes for examples. |
| 96 |
// |
| 97 |
// Non-standard numeric limits syntax "(std::numeric_limits<int>::max)()" |
| 98 |
// is to avoid macro substiution from MSVC |
| 99 |
// https://stackoverflow.com/questions/27442885/syntax-error-with-stdnumeric-limitsmax |
| 100 |
// |
| 101 |
template <class T, class Policy> |
| 102 |
inline int iround(const T& v, const Policy& pol) |
| 103 |
{ |
| 104 |
BOOST_MATH_STD_USING |
| 105 |
using result_type = tools::promote_args_t<T>; |
| 106 |
|
| 107 |
result_type r = boost::math::round(v, pol); |
| 108 |
|
| 109 |
#if defined(BOOST_MATH_HAS_CONSTEXPR_LDEXP) && !defined(BOOST_MATH_HAS_GPU_SUPPORT) |
| 110 |
if constexpr (std::is_arithmetic_v<result_type> |
| 111 |
#ifdef BOOST_MATH_FLOAT128_TYPE |
| 112 |
&& !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type> |
| 113 |
#endif |
| 114 |
) |
| 115 |
{ |
| 116 |
constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits); |
| 117 |
|
| 118 |
if (r >= max_val || r < -max_val) |
| 119 |
{ |
| 120 |
return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", nullptr, v, static_cast<int>(0), pol)); |
| 121 |
} |
| 122 |
} |
| 123 |
else |
| 124 |
{ |
| 125 |
static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits); |
| 126 |
|
| 127 |
if (r >= max_val || r < -max_val) |
| 128 |
{ |
| 129 |
return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", nullptr, v, static_cast<int>(0), pol)); |
| 130 |
} |
| 131 |
} |
| 132 |
#else |
| 133 |
BOOST_MATH_STATIC_LOCAL_VARIABLE const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<int>::digits); |
| 134 |
|
| 135 |
if (r >= max_val || r < -max_val) |
| 136 |
{ |
| 137 |
return static_cast<int>(boost::math::policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", nullptr, v, static_cast<int>(0), pol)); |
| 138 |
} |
| 139 |
#endif |
| 140 |
|
| 141 |
return static_cast<int>(r); |
| 142 |
} |
| 143 |
template <class T> |
| 144 |
BOOST_MATH_GPU_ENABLED inline int iround(const T& v) |
| 145 |
{ |
| 146 |
return iround(v, policies::policy<>()); |
| 147 |
} |
| 148 |
|
| 149 |
template <class T, class Policy> |
| 150 |
BOOST_MATH_GPU_ENABLED inline long lround(const T& v, const Policy& pol) |
| 151 |
{ |
| 152 |
BOOST_MATH_STD_USING |
| 153 |
using result_type = tools::promote_args_t<T>; |
| 154 |
|
| 155 |
result_type r = boost::math::round(v, pol); |
| 156 |
|
| 157 |
#if defined(BOOST_MATH_HAS_CONSTEXPR_LDEXP) && !defined(BOOST_MATH_HAS_GPU_SUPPORT) |
| 158 |
if constexpr (std::is_arithmetic_v<result_type> |
| 159 |
#ifdef BOOST_MATH_FLOAT128_TYPE |
| 160 |
&& !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type> |
| 161 |
#endif |
| 162 |
) |
| 163 |
{ |
| 164 |
constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits); |
| 165 |
|
| 166 |
if (r >= max_val || r < -max_val) |
| 167 |
{ |
| 168 |
return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", nullptr, v, static_cast<long>(0), pol)); |
| 169 |
} |
| 170 |
} |
| 171 |
else |
| 172 |
{ |
| 173 |
static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits); |
| 174 |
|
| 175 |
if (r >= max_val || r < -max_val) |
| 176 |
{ |
| 177 |
return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", nullptr, v, static_cast<long>(0), pol)); |
| 178 |
} |
| 179 |
} |
| 180 |
#else |
| 181 |
BOOST_MATH_STATIC_LOCAL_VARIABLE const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long>::digits); |
| 182 |
|
| 183 |
if (r >= max_val || r < -max_val) |
| 184 |
{ |
| 185 |
return static_cast<long>(boost::math::policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", nullptr, v, static_cast<long>(0), pol)); |
| 186 |
} |
| 187 |
#endif |
| 188 |
|
| 189 |
return static_cast<long>(r); |
| 190 |
} |
| 191 |
template <class T> |
| 192 |
BOOST_MATH_GPU_ENABLED inline long lround(const T& v) |
| 193 |
{ |
| 194 |
return lround(v, policies::policy<>()); |
| 195 |
} |
| 196 |
|
| 197 |
template <class T, class Policy> |
| 198 |
BOOST_MATH_GPU_ENABLED inline long long llround(const T& v, const Policy& pol) |
| 199 |
{ |
| 200 |
BOOST_MATH_STD_USING |
| 201 |
using result_type = boost::math::tools::promote_args_t<T>; |
| 202 |
|
| 203 |
result_type r = boost::math::round(v, pol); |
| 204 |
|
| 205 |
#if defined(BOOST_MATH_HAS_CONSTEXPR_LDEXP) && !defined(BOOST_MATH_HAS_GPU_SUPPORT) |
| 206 |
if constexpr (std::is_arithmetic_v<result_type> |
| 207 |
#ifdef BOOST_MATH_FLOAT128_TYPE |
| 208 |
&& !std::is_same_v<BOOST_MATH_FLOAT128_TYPE, result_type> |
| 209 |
#endif |
| 210 |
) |
| 211 |
{ |
| 212 |
constexpr result_type max_val = boost::math::ccmath::ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits); |
| 213 |
|
| 214 |
if (r >= max_val || r < -max_val) |
| 215 |
{ |
| 216 |
return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", nullptr, v, static_cast<long long>(0), pol)); |
| 217 |
} |
| 218 |
} |
| 219 |
else |
| 220 |
{ |
| 221 |
static const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits); |
| 222 |
|
| 223 |
if (r >= max_val || r < -max_val) |
| 224 |
{ |
| 225 |
return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", nullptr, v, static_cast<long long>(0), pol)); |
| 226 |
} |
| 227 |
} |
| 228 |
#else |
| 229 |
BOOST_MATH_STATIC_LOCAL_VARIABLE const result_type max_val = ldexp(static_cast<result_type>(1), std::numeric_limits<long long>::digits); |
| 230 |
|
| 231 |
if (r >= max_val || r < -max_val) |
| 232 |
{ |
| 233 |
return static_cast<long long>(boost::math::policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", nullptr, v, static_cast<long long>(0), pol)); |
| 234 |
} |
| 235 |
#endif |
| 236 |
|
| 237 |
return static_cast<long long>(r); |
| 238 |
} |
| 239 |
template <class T> |
| 240 |
BOOST_MATH_GPU_ENABLED inline long long llround(const T& v) |
| 241 |
{ |
| 242 |
return llround(v, policies::policy<>()); |
| 243 |
} |
| 244 |
|
| 245 |
}} // namespaces |
| 246 |
|
| 247 |
#else // Specialized NVRTC overloads |
| 248 |
|
| 249 |
namespace boost { |
| 250 |
namespace math { |
| 251 |
|
| 252 |
template <typename T> |
| 253 |
BOOST_MATH_GPU_ENABLED T round(T x) |
| 254 |
{ |
| 255 |
return ::round(x); |
| 256 |
} |
| 257 |
|
| 258 |
template <> |
| 259 |
BOOST_MATH_GPU_ENABLED float round(float x) |
| 260 |
{ |
| 261 |
return ::roundf(x); |
| 262 |
} |
| 263 |
|
| 264 |
template <typename T, typename Policy> |
| 265 |
BOOST_MATH_GPU_ENABLED T round(T x, const Policy&) |
| 266 |
{ |
| 267 |
return ::round(x); |
| 268 |
} |
| 269 |
|
| 270 |
template <typename Policy> |
| 271 |
BOOST_MATH_GPU_ENABLED float round(float x, const Policy&) |
| 272 |
{ |
| 273 |
return ::roundf(x); |
| 274 |
} |
| 275 |
|
| 276 |
template <typename T> |
| 277 |
BOOST_MATH_GPU_ENABLED int iround(T x) |
| 278 |
{ |
| 279 |
return static_cast<int>(::lround(x)); |
| 280 |
} |
| 281 |
|
| 282 |
template <> |
| 283 |
BOOST_MATH_GPU_ENABLED int iround(float x) |
| 284 |
{ |
| 285 |
return static_cast<int>(::lroundf(x)); |
| 286 |
} |
| 287 |
|
| 288 |
template <typename T, typename Policy> |
| 289 |
BOOST_MATH_GPU_ENABLED int iround(T x, const Policy&) |
| 290 |
{ |
| 291 |
return static_cast<int>(::lround(x)); |
| 292 |
} |
| 293 |
|
| 294 |
template <typename Policy> |
| 295 |
BOOST_MATH_GPU_ENABLED int iround(float x, const Policy&) |
| 296 |
{ |
| 297 |
return static_cast<int>(::lroundf(x)); |
| 298 |
} |
| 299 |
|
| 300 |
template <typename T> |
| 301 |
BOOST_MATH_GPU_ENABLED long lround(T x) |
| 302 |
{ |
| 303 |
return ::lround(x); |
| 304 |
} |
| 305 |
|
| 306 |
template <> |
| 307 |
BOOST_MATH_GPU_ENABLED long lround(float x) |
| 308 |
{ |
| 309 |
return ::lroundf(x); |
| 310 |
} |
| 311 |
|
| 312 |
template <typename T, typename Policy> |
| 313 |
BOOST_MATH_GPU_ENABLED long lround(T x, const Policy&) |
| 314 |
{ |
| 315 |
return ::lround(x); |
| 316 |
} |
| 317 |
|
| 318 |
template <typename Policy> |
| 319 |
BOOST_MATH_GPU_ENABLED long lround(float x, const Policy&) |
| 320 |
{ |
| 321 |
return ::lroundf(x); |
| 322 |
} |
| 323 |
|
| 324 |
template <typename T> |
| 325 |
BOOST_MATH_GPU_ENABLED long long llround(T x) |
| 326 |
{ |
| 327 |
return ::llround(x); |
| 328 |
} |
| 329 |
|
| 330 |
template <> |
| 331 |
BOOST_MATH_GPU_ENABLED long long llround(float x) |
| 332 |
{ |
| 333 |
return ::llroundf(x); |
| 334 |
} |
| 335 |
|
| 336 |
template <typename T, typename Policy> |
| 337 |
BOOST_MATH_GPU_ENABLED long long llround(T x, const Policy&) |
| 338 |
{ |
| 339 |
return ::llround(x); |
| 340 |
} |
| 341 |
|
| 342 |
template <typename Policy> |
| 343 |
BOOST_MATH_GPU_ENABLED long long llround(float x, const Policy&) |
| 344 |
{ |
| 345 |
return ::llroundf(x); |
| 346 |
} |
| 347 |
|
| 348 |
} // Namespace math |
| 349 |
} // Namespace boost |
| 350 |
|
| 351 |
#endif // BOOST_MATH_HAS_NVRTC |
| 352 |
|
| 353 |
#endif // BOOST_MATH_ROUND_HPP |