// (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 #include #include #include #include #include #include #include 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 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 inline constexpr ReturnType int_round_impl(T arg) { const T rounded_arg = round_impl(arg); if(rounded_arg > static_cast((std::numeric_limits::max)())) { if constexpr (std::is_same_v) { 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(rounded_arg); } } } // Namespace detail template , 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 , bool> = true> inline constexpr double round(Z arg) noexcept { return boost::math::ccmath::round(static_cast(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 , 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(arg); } else { using std::lround; return lround(arg); } } template , bool> = true> inline constexpr long lround(Z arg) { return boost::math::ccmath::lround(static_cast(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 , 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(arg); } else { using std::llround; return llround(arg); } } template , bool> = true> inline constexpr long llround(Z arg) { return boost::math::ccmath::llround(static_cast(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