mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2025-07-14 15:04:09 +00:00
add sprout/math/comparison.hpp
This commit is contained in:
parent
d51bd06eda
commit
1c65d59971
20 changed files with 379 additions and 109 deletions
58
sprout/math/equal_to.hpp
Normal file
58
sprout/math/equal_to.hpp
Normal file
|
@ -0,0 +1,58 @@
|
|||
#ifndef SPROUT_MATH_EQUAL_TO_HPP
|
||||
#define SPROUT_MATH_EQUAL_TO_HPP
|
||||
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/algorithm/max.hpp>
|
||||
#include <sprout/math/abs.hpp>
|
||||
#include <sprout/math/float_promote.hpp>
|
||||
#include <sprout/type_traits/enabler_if.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace math {
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR T
|
||||
max3(T x, T y, T z) {
|
||||
return sprout::max(sprout::max(x, y), z);
|
||||
}
|
||||
|
||||
template<
|
||||
typename FloatType,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
equal_to(FloatType x, FloatType y) {
|
||||
return x == y
|
||||
|| sprout::abs(x - y)
|
||||
<= std::numeric_limits<FloatType>::epsilon() * sprout::math::detail::max3(std::abs(x), std::abs(y), FloatType(1.0))
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename IntType,
|
||||
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
equal_to(IntType x, IntType y) {
|
||||
return x == y;
|
||||
}
|
||||
} // namespace detail
|
||||
//
|
||||
// equal_to
|
||||
//
|
||||
template<
|
||||
typename T1,
|
||||
typename T2,
|
||||
typename sprout::enabler_if<std::is_arithmetic<T1>::value && std::is_arithmetic<T2>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
equal_to(T1 x, T2 y) {
|
||||
typedef typename sprout::math::float_promote<T1, T2>::type promoted;
|
||||
return sprout::math::detail::equal_to<promoted>(x, y);
|
||||
}
|
||||
} // namespace math
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_MATH_EQUAL_TO_HPP
|
Loading…
Add table
Add a link
Reference in a new issue