#ifndef SPROUT_MATH_ITRUNC_HPP #define SPROUT_MATH_ITRUNC_HPP #include #include #include #include #include #include #if SPROUT_USE_BUILTIN_CMATH_FUNCTION # include #endif namespace sprout { namespace math { namespace detail { #if SPROUT_USE_BUILTIN_CMATH_FUNCTION template inline SPROUT_CONSTEXPR To itrunc_impl(FloatType x) { return std::numeric_limits::max() < x || std::numeric_limits::min() > x ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("itrunc: 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 itrunc(FloatType x) { return sprout::math::detail::itrunc_impl(sprout::math::trunc(x)); } #else template< typename To = int, typename FloatType, typename sprout::enabler_if::value && std::is_integral::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR To itrunc(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("itrunc: large float rounding."), static_cast(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 itrunc(IntType x) { return sprout::math::detail::itrunc(static_cast(x)); } } // namespace detail using sprout::math::detail::itrunc; } // namespace math using sprout::math::itrunc; } // namespace sprout #endif // #ifndef SPROUT_MATH_ITRUNC_HPP