Sprout/sprout/math/iround.hpp

90 lines
2.9 KiB
C++
Raw Normal View History

2013-02-10 02:54:54 +00:00
#ifndef SPROUT_MATH_IROUND_HPP
#define SPROUT_MATH_IROUND_HPP
#include <limits>
#include <type_traits>
#include <stdexcept>
#include <sprout/config.hpp>
#include <sprout/math/detail/config.hpp>
2013-05-06 14:57:38 +00:00
#include <sprout/math/isnan.hpp>
#include <sprout/math/isinf.hpp>
2013-02-10 02:54:54 +00:00
#include <sprout/type_traits/enabler_if.hpp>
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
# include <sprout/math/round.hpp>
#else
# include <sprout/math/equal_to.hpp>
#endif
namespace sprout {
namespace math {
namespace detail {
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
template<typename To, typename FloatType>
inline SPROUT_CONSTEXPR To
iround_impl(FloatType x) {
return std::numeric_limits<To>::max() < x || std::numeric_limits<To>::min() > x
2013-03-18 10:12:21 +00:00
? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("iround: large float rounding."), static_cast<To>(x))
2013-02-10 02:54:54 +00:00
: static_cast<To>(x)
;
}
template<
typename To = int,
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value && std::is_integral<To>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR To
iround(FloatType x) {
2013-05-06 14:57:38 +00:00
return sprout::math::isnan(x) || sprout::math::isinf(x) ? std::numeric_limits<To>::min()
: sprout::math::detail::iround_impl<To>(sprout::math::round(x))
;
2013-02-10 02:54:54 +00:00
}
#else
template<typename To, typename FloatType>
inline SPROUT_CONSTEXPR To
iround_impl_positive(FloatType x, To x0) {
return x - x0 < FloatType(0.5) ? x0
: x0 + 1
;
}
template<typename To, typename FloatType>
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<std::is_floating_point<FloatType>::value && std::is_integral<To>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR To
iround(FloatType x) {
2013-05-06 14:57:38 +00:00
return sprout::math::isnan(x) || sprout::math::isinf(x) ? std::numeric_limits<To>::min()
: x == 0 ? To(0)
2013-02-14 09:02:43 +00:00
: std::numeric_limits<To>::max() < x || std::numeric_limits<To>::min() > x
2013-03-18 10:12:21 +00:00
? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("iround: large float irounding."), x)
2013-02-10 02:54:54 +00:00
: x < 0 ? sprout::math::detail::iround_impl_nagative(x, static_cast<To>(x))
: sprout::math::detail::iround_impl_positive(x, static_cast<To>(x))
;
}
#endif
template<
typename To = int,
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value && std::is_integral<To>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR To
iround(IntType x) {
return sprout::math::detail::iround(static_cast<double>(x));
}
} // namespace detail
using sprout::math::detail::iround;
} // namespace math
using sprout::math::iround;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_IROUND_HPP