#ifndef SPROUT_MATH_IROUND_HPP #define SPROUT_MATH_IROUND_HPP #include #include #include #include #include #include #if SPROUT_USE_BUILTIN_CMATH_FUNCTION # include #else # include #endif namespace sprout { namespace math { namespace detail { #if SPROUT_USE_BUILTIN_CMATH_FUNCTION template inline SPROUT_CONSTEXPR To iround_impl(FloatType x) { return std::numeric_limits::max() < x || std::numeric_limits::min() > x ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("iround: large float rounding."), static_cast(x)) : static_cast(x) ; } template< typename To = int, typename FloatType, typename sprout::enabler_if::value && std::is_integral::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR To iround(FloatType x) { return sprout::math::detail::iround_impl(sprout::round(x)); } #else template inline SPROUT_CONSTEXPR To iround_impl_positive(FloatType x, To x0) { return x - x0 < FloatType(0.5) ? x0 : x0 + 1 ; } template inline SPROUT_CONSTEXPR To iround_impl_nagative(FloatType x, To x0) { return x0 - x < FloatType(0.5) ? x0 : x0 - 1 ; } template< typename To = int, typename FloatType, typename sprout::enabler_if::value && std::is_integral::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR To iround(FloatType x) { return x == 0 ? To(0) : std::numeric_limits::max() < x || std::numeric_limits::min() > x ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("iround: large float irounding."), x) : x < 0 ? sprout::math::detail::iround_impl_nagative(x, static_cast(x)) : sprout::math::detail::iround_impl_positive(x, static_cast(x)) ; } #endif template< typename To = int, typename IntType, typename sprout::enabler_if::value && std::is_integral::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR To iround(IntType x) { return sprout::math::detail::iround(static_cast(x)); } } // namespace detail using sprout::math::detail::iround; } // namespace math using sprout::math::iround; } // namespace sprout #endif // #ifndef SPROUT_MATH_IROUND_HPP