2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
|
|
|
Copyright (c) 2011-2013 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)
|
|
|
|
=============================================================================*/
|
2013-02-10 02:54:54 +00:00
|
|
|
#ifndef SPROUT_MATH_ICEIL_HPP
|
|
|
|
#define SPROUT_MATH_ICEIL_HPP
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <sprout/config.hpp>
|
2013-08-06 15:15:09 +00:00
|
|
|
#include <sprout/limits.hpp>
|
2013-02-10 02:54:54 +00:00
|
|
|
#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/ceil.hpp>
|
|
|
|
#else
|
2013-05-07 16:46:40 +00:00
|
|
|
# include <sprout/math/less_equal.hpp>
|
2013-02-10 02:54:54 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace math {
|
|
|
|
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
2013-10-25 03:29:16 +00:00
|
|
|
namespace detail {
|
2013-02-10 02:54:54 +00:00
|
|
|
template<typename To, typename FloatType>
|
|
|
|
inline SPROUT_CONSTEXPR To
|
|
|
|
iceil_impl(FloatType x) {
|
2013-08-06 15:15:09 +00:00
|
|
|
return sprout::numeric_limits<To>::max() < x || sprout::numeric_limits<To>::min() > x
|
2013-03-18 10:12:21 +00:00
|
|
|
? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("iceil: large float rounding."), static_cast<To>(x))
|
2013-02-10 02:54:54 +00:00
|
|
|
: static_cast<To>(x)
|
|
|
|
;
|
|
|
|
}
|
2013-10-25 03:29:16 +00:00
|
|
|
} // namespace detail
|
|
|
|
//
|
|
|
|
// iceil
|
|
|
|
//
|
|
|
|
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
|
|
|
|
iceil(FloatType x) {
|
|
|
|
return sprout::math::isnan(x) || sprout::math::isinf(x) ? sprout::numeric_limits<To>::min()
|
|
|
|
: sprout::math::detail::iceil_impl<To>(sprout::math::ceil(x))
|
|
|
|
;
|
|
|
|
}
|
2013-02-10 02:54:54 +00:00
|
|
|
#else
|
2013-10-25 03:29:16 +00:00
|
|
|
namespace detail {
|
2013-02-10 02:54:54 +00:00
|
|
|
template<typename To, typename FloatType>
|
|
|
|
inline SPROUT_CONSTEXPR To
|
|
|
|
iceil_impl(FloatType x, To x0) {
|
2013-05-07 16:46:40 +00:00
|
|
|
return sprout::math::less_equal(x, x0) ? x0
|
2013-02-10 02:54:54 +00:00
|
|
|
: x0 + 1
|
|
|
|
;
|
|
|
|
}
|
|
|
|
} // namespace detail
|
2013-10-25 03:29:16 +00:00
|
|
|
//
|
|
|
|
// iceil
|
|
|
|
//
|
|
|
|
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
|
|
|
|
iceil(FloatType x) {
|
|
|
|
return sprout::math::isnan(x) || sprout::math::isinf(x) ? sprout::numeric_limits<To>::min()
|
|
|
|
: x == 0 ? To(0)
|
|
|
|
: sprout::numeric_limits<To>::max() < x || sprout::numeric_limits<To>::min() > x
|
|
|
|
? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("iceil: large float rounding."), static_cast<To>(x))
|
|
|
|
: sprout::math::detail::iceil_impl(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
|
|
|
|
iceil(IntType x) {
|
|
|
|
return sprout::math::iceil<To>(static_cast<double>(x));
|
|
|
|
}
|
2013-02-10 02:54:54 +00:00
|
|
|
} // namespace math
|
|
|
|
|
|
|
|
using sprout::math::iceil;
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_MATH_ICEIL_HPP
|