mirror of
https://github.com/bolero-MURAKAMI/Sprout
synced 2025-10-05 13:00:00 +00:00
add floor, ceil, trunc, round
This commit is contained in:
parent
c6fb8af0c5
commit
77f058a34c
12 changed files with 236 additions and 11 deletions
53
sprout/math/ceil.hpp
Normal file
53
sprout/math/ceil.hpp
Normal file
|
@ -0,0 +1,53 @@
|
|||
#ifndef SPROUT_MATH_CEIL_HPP
|
||||
#define SPROUT_MATH_CEIL_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#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 FloatType
|
||||
ceil_impl(FloatType x, FloatType x0) {
|
||||
return x - x0 < std::numeric_limits<FloatType>::epsilon()
|
||||
? x0
|
||||
: x0 + 1
|
||||
;
|
||||
}
|
||||
template<
|
||||
typename FloatType,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
ceil(FloatType x) {
|
||||
return std::numeric_limits<std::uintmax_t>::max() < x || std::numeric_limits<std::uintmax_t>::max() < -x
|
||||
? throw std::domain_error("ceil: Sorry, not implemented.")
|
||||
: 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)))
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename IntType,
|
||||
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR double
|
||||
ceil(IntType x) {
|
||||
return sprout::math::detail::ceil(static_cast<double>(x));
|
||||
}
|
||||
} // namespacedetailmath
|
||||
|
||||
using NS_SPROUT_MATH_DETAIL::ceil;
|
||||
} // namespace math
|
||||
|
||||
using sprout::math::ceil;
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_MATH_CEIL_HPP
|
53
sprout/math/floor.hpp
Normal file
53
sprout/math/floor.hpp
Normal file
|
@ -0,0 +1,53 @@
|
|||
#ifndef SPROUT_MATH_FLOOR_HPP
|
||||
#define SPROUT_MATH_FLOOR_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#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 FloatType
|
||||
floor_impl(FloatType x, FloatType x0) {
|
||||
return x0 - x < std::numeric_limits<FloatType>::epsilon()
|
||||
? x0
|
||||
: x0 - 1
|
||||
;
|
||||
}
|
||||
template<
|
||||
typename FloatType,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
floor(FloatType x) {
|
||||
return std::numeric_limits<std::uintmax_t>::max() < x || std::numeric_limits<std::uintmax_t>::max() < -x
|
||||
? throw std::domain_error("floor: Sorry, not implemented.")
|
||||
: 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))
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename IntType,
|
||||
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR double
|
||||
floor(IntType x) {
|
||||
return sprout::math::detail::floor(static_cast<double>(x));
|
||||
}
|
||||
} // namespacedetailmath
|
||||
|
||||
using NS_SPROUT_MATH_DETAIL::floor;
|
||||
} // namespace math
|
||||
|
||||
using sprout::math::floor;
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_MATH_FLOOR_HPP
|
|
@ -8,6 +8,7 @@
|
|||
#include <sprout/math/exponential.hpp>
|
||||
#include <sprout/math/power.hpp>
|
||||
#include <sprout/math/operations.hpp>
|
||||
#include <sprout/math/nearest.hpp>
|
||||
#include <sprout/math/common_factor.hpp>
|
||||
#include <sprout/math/factorial.hpp>
|
||||
#include <sprout/math/bernoulli.hpp>
|
||||
|
|
10
sprout/math/nearest.hpp
Normal file
10
sprout/math/nearest.hpp
Normal file
|
@ -0,0 +1,10 @@
|
|||
#ifndef SPROUT_MATH_NEAREST_HPP
|
||||
#define SPROUT_MATH_NEAREST_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/ceil.hpp>
|
||||
#include <sprout/math/floor.hpp>
|
||||
#include <sprout/math/trunc.hpp>
|
||||
#include <sprout/math/round.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_MATH_NEAREST_HPP
|
61
sprout/math/round.hpp
Normal file
61
sprout/math/round.hpp
Normal file
|
@ -0,0 +1,61 @@
|
|||
#ifndef SPROUT_MATH_ROUND_HPP
|
||||
#define SPROUT_MATH_ROUND_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#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 FloatType
|
||||
round_impl_positive(FloatType x, FloatType x0) {
|
||||
return x - x0 < FloatType(0.5)
|
||||
? x0
|
||||
: x0 + 1
|
||||
;
|
||||
}
|
||||
template<typename FloatType>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
round_impl_nagative(FloatType x, FloatType 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 FloatType
|
||||
round(FloatType x) {
|
||||
return std::numeric_limits<std::uintmax_t>::max() < x || std::numeric_limits<std::uintmax_t>::max() < -x
|
||||
? throw std::domain_error("round: Sorry, not implemented.")
|
||||
: 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)))
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename IntType,
|
||||
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR double
|
||||
round(IntType x) {
|
||||
return sprout::math::detail::round(static_cast<double>(x));
|
||||
}
|
||||
} // namespacedetailmath
|
||||
|
||||
using NS_SPROUT_MATH_DETAIL::round;
|
||||
} // namespace math
|
||||
|
||||
using sprout::math::round;
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_MATH_ROUND_HPP
|
45
sprout/math/trunc.hpp
Normal file
45
sprout/math/trunc.hpp
Normal file
|
@ -0,0 +1,45 @@
|
|||
#ifndef SPROUT_MATH_TRUNC_HPP
|
||||
#define SPROUT_MATH_TRUNC_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#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,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
trunc(FloatType x) {
|
||||
return std::numeric_limits<std::uintmax_t>::max() < x || std::numeric_limits<std::uintmax_t>::max() < -x
|
||||
? throw std::domain_error("trunc: Sorry, not implemented.")
|
||||
: x < 0
|
||||
? -static_cast<FloatType>(static_cast<std::uintmax_t>(-x))
|
||||
: static_cast<FloatType>(static_cast<std::uintmax_t>(x))
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename IntType,
|
||||
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR double
|
||||
trunc(IntType x) {
|
||||
return sprout::math::detail::trunc(static_cast<double>(x));
|
||||
}
|
||||
} // namespacedetailmath
|
||||
|
||||
using NS_SPROUT_MATH_DETAIL::trunc;
|
||||
} // namespace math
|
||||
|
||||
using sprout::math::trunc;
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_MATH_TRUNC_HPP
|
Loading…
Add table
Add a link
Reference in a new issue