mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-12-23 21:25:49 +00:00
add lround, llround
This commit is contained in:
parent
77f058a34c
commit
36fe60bec3
7 changed files with 140 additions and 28 deletions
|
@ -7,6 +7,7 @@
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <sprout/config.hpp>
|
#include <sprout/config.hpp>
|
||||||
#include <sprout/math/detail/config.hpp>
|
#include <sprout/math/detail/config.hpp>
|
||||||
|
#include <sprout/math/isinf.hpp>
|
||||||
#include <sprout/type_traits/enabler_if.hpp>
|
#include <sprout/type_traits/enabler_if.hpp>
|
||||||
|
|
||||||
namespace sprout {
|
namespace sprout {
|
||||||
|
@ -15,8 +16,7 @@ namespace sprout {
|
||||||
template<typename FloatType>
|
template<typename FloatType>
|
||||||
inline SPROUT_CONSTEXPR FloatType
|
inline SPROUT_CONSTEXPR FloatType
|
||||||
ceil_impl(FloatType x, FloatType x0) {
|
ceil_impl(FloatType x, FloatType x0) {
|
||||||
return x - x0 < std::numeric_limits<FloatType>::epsilon()
|
return x - x0 < std::numeric_limits<FloatType>::epsilon() ? x0
|
||||||
? x0
|
|
||||||
: x0 + 1
|
: x0 + 1
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
@ -26,11 +26,11 @@ namespace sprout {
|
||||||
>
|
>
|
||||||
inline SPROUT_CONSTEXPR FloatType
|
inline SPROUT_CONSTEXPR FloatType
|
||||||
ceil(FloatType x) {
|
ceil(FloatType x) {
|
||||||
return std::numeric_limits<std::uintmax_t>::max() < x || std::numeric_limits<std::uintmax_t>::max() < -x
|
return sprout::math::isinf(x) ? x
|
||||||
? throw std::domain_error("ceil: Sorry, not implemented.")
|
: std::numeric_limits<std::uintmax_t>::max() < x || std::numeric_limits<std::uintmax_t>::max() < -x
|
||||||
: x < 0
|
? throw std::domain_error("ceil: Sorry, not implemented.")
|
||||||
? -static_cast<FloatType>(static_cast<std::uintmax_t>(-x))
|
: x < 0 ? -static_cast<FloatType>(static_cast<std::uintmax_t>(-x))
|
||||||
: sprout::math::detail::ceil_impl(x, static_cast<FloatType>(static_cast<std::uintmax_t>(x)))
|
: sprout::math::detail::ceil_impl(x, static_cast<FloatType>(static_cast<std::uintmax_t>(x)))
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <sprout/config.hpp>
|
#include <sprout/config.hpp>
|
||||||
#include <sprout/math/detail/config.hpp>
|
#include <sprout/math/detail/config.hpp>
|
||||||
|
#include <sprout/math/isinf.hpp>
|
||||||
#include <sprout/type_traits/enabler_if.hpp>
|
#include <sprout/type_traits/enabler_if.hpp>
|
||||||
|
|
||||||
namespace sprout {
|
namespace sprout {
|
||||||
|
@ -15,8 +16,7 @@ namespace sprout {
|
||||||
template<typename FloatType>
|
template<typename FloatType>
|
||||||
inline SPROUT_CONSTEXPR FloatType
|
inline SPROUT_CONSTEXPR FloatType
|
||||||
floor_impl(FloatType x, FloatType x0) {
|
floor_impl(FloatType x, FloatType x0) {
|
||||||
return x0 - x < std::numeric_limits<FloatType>::epsilon()
|
return x0 - x < std::numeric_limits<FloatType>::epsilon() ? x0
|
||||||
? x0
|
|
||||||
: x0 - 1
|
: x0 - 1
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
@ -26,11 +26,11 @@ namespace sprout {
|
||||||
>
|
>
|
||||||
inline SPROUT_CONSTEXPR FloatType
|
inline SPROUT_CONSTEXPR FloatType
|
||||||
floor(FloatType x) {
|
floor(FloatType x) {
|
||||||
return std::numeric_limits<std::uintmax_t>::max() < x || std::numeric_limits<std::uintmax_t>::max() < -x
|
return sprout::math::isinf(x) ? x
|
||||||
? throw std::domain_error("floor: Sorry, not implemented.")
|
: std::numeric_limits<std::uintmax_t>::max() < x || std::numeric_limits<std::uintmax_t>::max() < -x
|
||||||
: x < 0
|
? throw std::domain_error("floor: Sorry, not implemented.")
|
||||||
? sprout::math::detail::floor_impl(x, -static_cast<FloatType>(static_cast<std::uintmax_t>(-x)))
|
: x < 0 ? sprout::math::detail::floor_impl(x, -static_cast<FloatType>(static_cast<std::uintmax_t>(-x)))
|
||||||
: static_cast<FloatType>(static_cast<std::uintmax_t>(x))
|
: static_cast<FloatType>(static_cast<std::uintmax_t>(x))
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
55
sprout/math/llround.hpp
Normal file
55
sprout/math/llround.hpp
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
#ifndef SPROUT_MATH_LLROUND_HPP
|
||||||
|
#define SPROUT_MATH_LLROUND_HPP
|
||||||
|
|
||||||
|
#include <limits>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <sprout/config.hpp>
|
||||||
|
#include <sprout/math/detail/config.hpp>
|
||||||
|
#include <sprout/type_traits/enabler_if.hpp>
|
||||||
|
|
||||||
|
namespace sprout {
|
||||||
|
namespace math {
|
||||||
|
namespace detail {
|
||||||
|
template<typename FloatType>
|
||||||
|
inline SPROUT_CONSTEXPR long long
|
||||||
|
llround_impl_positive(FloatType x, long long x0) {
|
||||||
|
return x - x0 < FloatType(0.5) ? x0
|
||||||
|
: x0 + 1
|
||||||
|
;
|
||||||
|
}
|
||||||
|
template<typename FloatType>
|
||||||
|
inline SPROUT_CONSTEXPR long long
|
||||||
|
llround_impl_nagative(FloatType x, long long x0) {
|
||||||
|
return x0 - x < FloatType(0.5) ? x0
|
||||||
|
: x0 - 1
|
||||||
|
;
|
||||||
|
}
|
||||||
|
template<
|
||||||
|
typename FloatType,
|
||||||
|
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||||
|
>
|
||||||
|
inline SPROUT_CONSTEXPR long long
|
||||||
|
llround(FloatType x) {
|
||||||
|
return x < 0 ? sprout::math::detail::llround_impl_nagative(x, static_cast<long long>(x))
|
||||||
|
: sprout::math::detail::llround_impl_positive(x, static_cast<long long>(x))
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<
|
||||||
|
typename IntType,
|
||||||
|
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||||
|
>
|
||||||
|
inline SPROUT_CONSTEXPR long long
|
||||||
|
llround(IntType x) {
|
||||||
|
return static_cast<long long>(x);
|
||||||
|
}
|
||||||
|
} // namespacedetailmath
|
||||||
|
|
||||||
|
using NS_SPROUT_MATH_DETAIL::llround;
|
||||||
|
} // namespace math
|
||||||
|
|
||||||
|
using sprout::math::llround;
|
||||||
|
} // namespace sprout
|
||||||
|
|
||||||
|
#endif // #ifndef SPROUT_MATH_LLROUND_HPP
|
55
sprout/math/lround.hpp
Normal file
55
sprout/math/lround.hpp
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
#ifndef SPROUT_MATH_LROUND_HPP
|
||||||
|
#define SPROUT_MATH_LROUND_HPP
|
||||||
|
|
||||||
|
#include <limits>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <sprout/config.hpp>
|
||||||
|
#include <sprout/math/detail/config.hpp>
|
||||||
|
#include <sprout/type_traits/enabler_if.hpp>
|
||||||
|
|
||||||
|
namespace sprout {
|
||||||
|
namespace math {
|
||||||
|
namespace detail {
|
||||||
|
template<typename FloatType>
|
||||||
|
inline SPROUT_CONSTEXPR long
|
||||||
|
lround_impl_positive(FloatType x, long x0) {
|
||||||
|
return x - x0 < FloatType(0.5) ? x0
|
||||||
|
: x0 + 1
|
||||||
|
;
|
||||||
|
}
|
||||||
|
template<typename FloatType>
|
||||||
|
inline SPROUT_CONSTEXPR long
|
||||||
|
lround_impl_nagative(FloatType x, long x0) {
|
||||||
|
return x0 - x < FloatType(0.5) ? x0
|
||||||
|
: x0 - 1
|
||||||
|
;
|
||||||
|
}
|
||||||
|
template<
|
||||||
|
typename FloatType,
|
||||||
|
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||||
|
>
|
||||||
|
inline SPROUT_CONSTEXPR long
|
||||||
|
lround(FloatType x) {
|
||||||
|
return x < 0 ? sprout::math::detail::lround_impl_nagative(x, static_cast<long>(x))
|
||||||
|
: sprout::math::detail::lround_impl_positive(x, static_cast<long>(x))
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<
|
||||||
|
typename IntType,
|
||||||
|
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||||
|
>
|
||||||
|
inline SPROUT_CONSTEXPR long
|
||||||
|
lround(IntType x) {
|
||||||
|
return static_cast<long>(x);
|
||||||
|
}
|
||||||
|
} // namespacedetailmath
|
||||||
|
|
||||||
|
using NS_SPROUT_MATH_DETAIL::lround;
|
||||||
|
} // namespace math
|
||||||
|
|
||||||
|
using sprout::math::lround;
|
||||||
|
} // namespace sprout
|
||||||
|
|
||||||
|
#endif // #ifndef SPROUT_MATH_LROUND_HPP
|
|
@ -6,5 +6,7 @@
|
||||||
#include <sprout/math/floor.hpp>
|
#include <sprout/math/floor.hpp>
|
||||||
#include <sprout/math/trunc.hpp>
|
#include <sprout/math/trunc.hpp>
|
||||||
#include <sprout/math/round.hpp>
|
#include <sprout/math/round.hpp>
|
||||||
|
#include <sprout/math/lround.hpp>
|
||||||
|
#include <sprout/math/llround.hpp>
|
||||||
|
|
||||||
#endif // #ifndef SPROUT_MATH_NEAREST_HPP
|
#endif // #ifndef SPROUT_MATH_NEAREST_HPP
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <sprout/config.hpp>
|
#include <sprout/config.hpp>
|
||||||
#include <sprout/math/detail/config.hpp>
|
#include <sprout/math/detail/config.hpp>
|
||||||
|
#include <sprout/math/isinf.hpp>
|
||||||
#include <sprout/type_traits/enabler_if.hpp>
|
#include <sprout/type_traits/enabler_if.hpp>
|
||||||
|
|
||||||
namespace sprout {
|
namespace sprout {
|
||||||
|
@ -15,16 +16,14 @@ namespace sprout {
|
||||||
template<typename FloatType>
|
template<typename FloatType>
|
||||||
inline SPROUT_CONSTEXPR FloatType
|
inline SPROUT_CONSTEXPR FloatType
|
||||||
round_impl_positive(FloatType x, FloatType x0) {
|
round_impl_positive(FloatType x, FloatType x0) {
|
||||||
return x - x0 < FloatType(0.5)
|
return x - x0 < FloatType(0.5) ? x0
|
||||||
? x0
|
|
||||||
: x0 + 1
|
: x0 + 1
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
template<typename FloatType>
|
template<typename FloatType>
|
||||||
inline SPROUT_CONSTEXPR FloatType
|
inline SPROUT_CONSTEXPR FloatType
|
||||||
round_impl_nagative(FloatType x, FloatType x0) {
|
round_impl_nagative(FloatType x, FloatType x0) {
|
||||||
return x0 - x < FloatType(0.5)
|
return x0 - x < FloatType(0.5) ? x0
|
||||||
? x0
|
|
||||||
: x0 - 1
|
: x0 - 1
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
@ -34,11 +33,11 @@ namespace sprout {
|
||||||
>
|
>
|
||||||
inline SPROUT_CONSTEXPR FloatType
|
inline SPROUT_CONSTEXPR FloatType
|
||||||
round(FloatType x) {
|
round(FloatType x) {
|
||||||
return std::numeric_limits<std::uintmax_t>::max() < x || std::numeric_limits<std::uintmax_t>::max() < -x
|
return sprout::math::isinf(x) ? x
|
||||||
? throw std::domain_error("round: Sorry, not implemented.")
|
: std::numeric_limits<std::uintmax_t>::max() < x || std::numeric_limits<std::uintmax_t>::max() < -x
|
||||||
: x < 0
|
? throw std::domain_error("round: Sorry, not implemented.")
|
||||||
? sprout::math::detail::round_impl_nagative(x, -static_cast<FloatType>(static_cast<std::uintmax_t>(-x)))
|
: x < 0 ? sprout::math::detail::round_impl_nagative(x, -static_cast<FloatType>(static_cast<std::uintmax_t>(-x)))
|
||||||
: sprout::math::detail::round_impl_positive(x, static_cast<FloatType>(static_cast<std::uintmax_t>(x)))
|
: sprout::math::detail::round_impl_positive(x, static_cast<FloatType>(static_cast<std::uintmax_t>(x)))
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <sprout/config.hpp>
|
#include <sprout/config.hpp>
|
||||||
#include <sprout/math/detail/config.hpp>
|
#include <sprout/math/detail/config.hpp>
|
||||||
|
#include <sprout/math/isinf.hpp>
|
||||||
#include <sprout/type_traits/enabler_if.hpp>
|
#include <sprout/type_traits/enabler_if.hpp>
|
||||||
|
|
||||||
namespace sprout {
|
namespace sprout {
|
||||||
|
@ -18,11 +19,11 @@ namespace sprout {
|
||||||
>
|
>
|
||||||
inline SPROUT_CONSTEXPR FloatType
|
inline SPROUT_CONSTEXPR FloatType
|
||||||
trunc(FloatType x) {
|
trunc(FloatType x) {
|
||||||
return std::numeric_limits<std::uintmax_t>::max() < x || std::numeric_limits<std::uintmax_t>::max() < -x
|
return sprout::math::isinf(x) ? x
|
||||||
? throw std::domain_error("trunc: Sorry, not implemented.")
|
: std::numeric_limits<std::uintmax_t>::max() < x || std::numeric_limits<std::uintmax_t>::max() < -x
|
||||||
: x < 0
|
? throw std::domain_error("trunc: Sorry, not implemented.")
|
||||||
? -static_cast<FloatType>(static_cast<std::uintmax_t>(-x))
|
: x < 0 ? -static_cast<FloatType>(static_cast<std::uintmax_t>(-x))
|
||||||
: static_cast<FloatType>(static_cast<std::uintmax_t>(x))
|
: static_cast<FloatType>(static_cast<std::uintmax_t>(x))
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue