180 lines · plain
1// (C) Copyright Matt Borland 2021.2// Use, modification and distribution are subject to the3// Boost Software License, Version 1.0. (See accompanying file4// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)5 6#ifndef BOOST_MATH_CCMATH_ROUND_HPP7#define BOOST_MATH_CCMATH_ROUND_HPP8 9#include <stdexcept>10#include <boost/math/ccmath/detail/config.hpp>11 12#ifdef BOOST_MATH_NO_CCMATH13#error "The header <boost/math/round.hpp> can only be used in C++17 and later."14#endif15 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 21namespace boost::math::ccmath {22 23namespace 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.27template <typename T>28inline constexpr T round_impl(T arg) noexcept29{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 else43 {44 return iptr;45 }46}47 48template <typename ReturnType, typename T>49inline 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 else60 {61 throw std::domain_error("Rounded value cannot be represented by a long type without overflow");62 }63 }64 else65 {66 return static_cast<ReturnType>(rounded_arg);67 }68}69 70} // Namespace detail71 72template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>73inline constexpr Real round(Real arg) noexcept74{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 else83 {84 using std::round;85 return round(arg);86 }87}88 89template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>90inline constexpr double round(Z arg) noexcept91{92 return boost::math::ccmath::round(static_cast<double>(arg));93}94 95inline constexpr float roundf(float arg) noexcept96{97 return boost::math::ccmath::round(arg);98}99 100#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS101inline constexpr long double roundl(long double arg) noexcept102{103 return boost::math::ccmath::round(arg);104}105#endif106 107template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>108inline 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 else118 {119 using std::lround;120 return lround(arg);121 }122}123 124template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>125inline constexpr long lround(Z arg)126{127 return boost::math::ccmath::lround(static_cast<double>(arg));128}129 130inline constexpr long lroundf(float arg)131{132 return boost::math::ccmath::lround(arg);133}134 135#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS136inline constexpr long lroundl(long double arg)137{138 return boost::math::ccmath::lround(arg);139}140#endif141 142template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>143inline 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 else153 {154 using std::llround;155 return llround(arg);156 }157}158 159template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>160inline constexpr long llround(Z arg)161{162 return boost::math::ccmath::llround(static_cast<double>(arg));163}164 165inline constexpr long long llroundf(float arg)166{167 return boost::math::ccmath::llround(arg);168}169 170#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS171inline constexpr long long llroundl(long double arg)172{173 return boost::math::ccmath::llround(arg);174}175#endif176 177} // Namespaces178 179#endif // BOOST_MATH_CCMATH_ROUND_HPP180