/*============================================================================= Copyright (c) 2011-2019 Bolero MURAKAMI https://github.com/bolero-MURAKAMI/Sprout Distributed under 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 SPROUT_MATH_ITRUNC_HPP #define SPROUT_MATH_ITRUNC_HPP #include #include #include #include #include #include #include #include #if SPROUT_USE_BUILTIN_CMATH_FUNCTION # include #endif namespace sprout { namespace math { #if SPROUT_USE_BUILTIN_CMATH_FUNCTION namespace detail { template inline SPROUT_CONSTEXPR To itrunc_impl(FloatType x) { return sprout::numeric_limits::max() < x || sprout::numeric_limits::min() > x ? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("itrunc: large float rounding."), static_cast(x)) : static_cast(x) ; } } // namespace detail // // itrunc // 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::isnan(x) || sprout::math::isinf(x) ? sprout::numeric_limits::min() : sprout::math::detail::itrunc_impl(sprout::math::trunc(x)) ; } #else // // itrunc // 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::isnan(x) || sprout::math::isinf(x) ? sprout::numeric_limits::min() : x == 0 ? To(0) : sprout::numeric_limits::max() < x || sprout::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::itrunc(static_cast(x)); } } // namespace math using sprout::math::itrunc; } // namespace sprout #endif // #ifndef SPROUT_MATH_ITRUNC_HPP