Chris@16: // Copyright John Maddock 2007. Chris@16: // Use, modification and distribution are subject to the Chris@16: // Boost Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_MATH_ROUND_HPP Chris@16: #define BOOST_MATH_ROUND_HPP Chris@16: Chris@16: #ifdef _MSC_VER Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: #include Chris@16: Chris@16: namespace boost{ namespace math{ Chris@16: Chris@101: namespace detail{ Chris@101: Chris@101: template Chris@101: inline typename tools::promote_args::type round(const T& v, const Policy& pol, const mpl::false_) Chris@101: { Chris@101: BOOST_MATH_STD_USING Chris@101: typedef typename tools::promote_args::type result_type; Chris@101: if(!(boost::math::isfinite)(v)) Chris@101: return policies::raise_rounding_error("boost::math::round<%1%>(%1%)", 0, static_cast(v), static_cast(v), pol); Chris@101: // Chris@101: // The logic here is rather convoluted, but avoids a number of traps, Chris@101: // see discussion here https://github.com/boostorg/math/pull/8 Chris@101: // Chris@101: if (-0.5 < v && v < 0.5) Chris@101: { Chris@101: // special case to avoid rounding error on the direct Chris@101: // predecessor of +0.5 resp. the direct successor of -0.5 in Chris@101: // IEEE floating point types Chris@101: return 0; Chris@101: } Chris@101: else if (v > 0) Chris@101: { Chris@101: // subtract v from ceil(v) first in order to avoid rounding Chris@101: // errors on largest representable integer numbers Chris@101: result_type c(ceil(v)); Chris@101: return 0.5 < c - v ? c - 1 : c; Chris@101: } Chris@101: else Chris@101: { Chris@101: // see former branch Chris@101: result_type f(floor(v)); Chris@101: return 0.5 < v - f ? f + 1 : f; Chris@101: } Chris@101: } Chris@101: template Chris@101: inline typename tools::promote_args::type round(const T& v, const Policy&, const mpl::true_) Chris@101: { Chris@101: return v; Chris@101: } Chris@101: Chris@101: } // namespace detail Chris@101: Chris@16: template Chris@16: inline typename tools::promote_args::type round(const T& v, const Policy& pol) Chris@16: { Chris@101: return detail::round(v, pol, mpl::bool_::value>()); Chris@16: } Chris@16: template Chris@16: inline typename tools::promote_args::type round(const T& v) Chris@16: { Chris@16: return round(v, policies::policy<>()); Chris@16: } Chris@16: // Chris@16: // The following functions will not compile unless T has an Chris@16: // implicit convertion to the integer types. For user-defined Chris@16: // number types this will likely not be the case. In that case Chris@16: // these functions should either be specialized for the UDT in Chris@16: // question, or else overloads should be placed in the same Chris@16: // namespace as the UDT: these will then be found via argument Chris@16: // dependent lookup. See our concept archetypes for examples. Chris@16: // Chris@16: template Chris@16: inline int iround(const T& v, const Policy& pol) Chris@16: { Chris@16: BOOST_MATH_STD_USING Chris@16: T r = boost::math::round(v, pol); Chris@16: if((r > (std::numeric_limits::max)()) || (r < (std::numeric_limits::min)())) Chris@16: return static_cast(policies::raise_rounding_error("boost::math::iround<%1%>(%1%)", 0, v, 0, pol)); Chris@16: return static_cast(r); Chris@16: } Chris@16: template Chris@16: inline int iround(const T& v) Chris@16: { Chris@16: return iround(v, policies::policy<>()); Chris@16: } Chris@16: Chris@16: template Chris@16: inline long lround(const T& v, const Policy& pol) Chris@16: { Chris@16: BOOST_MATH_STD_USING Chris@16: T r = boost::math::round(v, pol); Chris@16: if((r > (std::numeric_limits::max)()) || (r < (std::numeric_limits::min)())) Chris@16: return static_cast(policies::raise_rounding_error("boost::math::lround<%1%>(%1%)", 0, v, 0L, pol)); Chris@16: return static_cast(r); Chris@16: } Chris@16: template Chris@16: inline long lround(const T& v) Chris@16: { Chris@16: return lround(v, policies::policy<>()); Chris@16: } Chris@16: Chris@16: #ifdef BOOST_HAS_LONG_LONG Chris@16: Chris@16: template Chris@16: inline boost::long_long_type llround(const T& v, const Policy& pol) Chris@16: { Chris@16: BOOST_MATH_STD_USING Chris@16: T r = boost::math::round(v, pol); Chris@16: if((r > (std::numeric_limits::max)()) || (r < (std::numeric_limits::min)())) Chris@16: return static_cast(policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", 0, v, static_cast(0), pol)); Chris@16: return static_cast(r); Chris@16: } Chris@16: template Chris@16: inline boost::long_long_type llround(const T& v) Chris@16: { Chris@16: return llround(v, policies::policy<>()); Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: }} // namespaces Chris@16: Chris@16: #endif // BOOST_MATH_ROUND_HPP