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)
6 #ifndef BOOST_MATH_CCMATH_ROUND_HPP
7 #define BOOST_MATH_CCMATH_ROUND_HPP
10 #include <type_traits>
12 #include <boost/math/tools/is_constant_evaluated.hpp>
13 #include <boost/math/ccmath/abs.hpp>
14 #include <boost/math/ccmath/isinf.hpp>
15 #include <boost/math/ccmath/isnan.hpp>
16 #include <boost/math/ccmath/modf.hpp>
18 namespace boost::math::ccmath {
22 // Computes the nearest integer value to arg (in floating-point format),
23 // rounding halfway cases away from zero, regardless of the current rounding mode.
25 inline constexpr T round_impl(T arg) noexcept
28 const T x = boost::math::ccmath::modf(arg, &iptr);
29 constexpr T half = T(1)/2;
31 if(x >= half && iptr > 0)
35 else if(boost::math::ccmath::abs(x) >= half && iptr < 0)
45 template <typename ReturnType, typename T>
46 inline constexpr ReturnType int_round_impl(T arg)
48 const T rounded_arg = round_impl(arg);
50 if(rounded_arg > static_cast<T>((std::numeric_limits<ReturnType>::max)()))
52 if constexpr (std::is_same_v<ReturnType, long long>)
54 throw std::domain_error("Rounded value cannot be represented by a long long type without overflow");
58 throw std::domain_error("Rounded value cannot be represented by a long type without overflow");
63 return static_cast<ReturnType>(rounded_arg);
69 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
70 inline constexpr Real round(Real arg) noexcept
72 if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
74 return boost::math::ccmath::abs(arg) == Real(0) ? arg :
75 boost::math::ccmath::isinf(arg) ? arg :
76 boost::math::ccmath::isnan(arg) ? arg :
77 boost::math::ccmath::detail::round_impl(arg);
86 template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
87 inline constexpr double round(Z arg) noexcept
89 return boost::math::ccmath::round(static_cast<double>(arg));
92 inline constexpr float roundf(float arg) noexcept
94 return boost::math::ccmath::round(arg);
97 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
98 inline constexpr long double roundl(long double arg) noexcept
100 return boost::math::ccmath::round(arg);
104 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
105 inline constexpr long lround(Real arg)
107 if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
109 return boost::math::ccmath::abs(arg) == Real(0) ? 0l :
110 boost::math::ccmath::isinf(arg) ? 0l :
111 boost::math::ccmath::isnan(arg) ? 0l :
112 boost::math::ccmath::detail::int_round_impl<long>(arg);
121 template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
122 inline constexpr long lround(Z arg)
124 return boost::math::ccmath::lround(static_cast<double>(arg));
127 inline constexpr long lroundf(float arg)
129 return boost::math::ccmath::lround(arg);
132 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
133 inline constexpr long lroundl(long double arg)
135 return boost::math::ccmath::lround(arg);
139 template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
140 inline constexpr long long llround(Real arg)
142 if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
144 return boost::math::ccmath::abs(arg) == Real(0) ? 0ll :
145 boost::math::ccmath::isinf(arg) ? 0ll :
146 boost::math::ccmath::isnan(arg) ? 0ll :
147 boost::math::ccmath::detail::int_round_impl<long long>(arg);
156 template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
157 inline constexpr long llround(Z arg)
159 return boost::math::ccmath::llround(static_cast<double>(arg));
162 inline constexpr long long llroundf(float arg)
164 return boost::math::ccmath::llround(arg);
167 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
168 inline constexpr long long llroundl(long double arg)
170 return boost::math::ccmath::llround(arg);
176 #endif // BOOST_MATH_CCMATH_ROUND_HPP