add finite check: is_integer, is_odd, is_even

This commit is contained in:
bolero-MURAKAMI 2013-05-09 01:15:44 +09:00
parent 9ff74b52eb
commit 3f434dd7e1
6 changed files with 34 additions and 8 deletions

View file

@ -1,7 +1,6 @@
#ifndef SPROUT_MATH_FLOAT2_EXPONENT_HPP #ifndef SPROUT_MATH_FLOAT2_EXPONENT_HPP
#define SPROUT_MATH_FLOAT2_EXPONENT_HPP #define SPROUT_MATH_FLOAT2_EXPONENT_HPP
#include <climits>
#include <limits> #include <limits>
#include <type_traits> #include <type_traits>
#include <sprout/config.hpp> #include <sprout/config.hpp>

View file

@ -1,7 +1,6 @@
#ifndef SPROUT_MATH_FLOAT2_SIG_EXP_HPP #ifndef SPROUT_MATH_FLOAT2_SIG_EXP_HPP
#define SPROUT_MATH_FLOAT2_SIG_EXP_HPP #define SPROUT_MATH_FLOAT2_SIG_EXP_HPP
#include <climits>
#include <limits> #include <limits>
#include <type_traits> #include <type_traits>
#include <sprout/config.hpp> #include <sprout/config.hpp>

View file

@ -4,18 +4,27 @@
#include <type_traits> #include <type_traits>
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/type_traits/enabler_if.hpp> #include <sprout/type_traits/enabler_if.hpp>
#include <sprout/math/isfinite.hpp>
#include <sprout/math/fmod.hpp> #include <sprout/math/fmod.hpp>
namespace sprout { namespace sprout {
namespace math { namespace math {
namespace detail { namespace detail {
template<typename T>
inline SPROUT_CONSTEXPR bool
is_even_unchecked(T x) {
return sprout::math::fmod(x, T(2)) == T(0);
}
template< template<
typename FloatType, typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
> >
inline SPROUT_CONSTEXPR bool inline SPROUT_CONSTEXPR bool
is_even(FloatType x) { is_even(FloatType x) {
return sprout::math::fmod(x, FloatType(2)) == FloatType(0); return sprout::math::isfinite(x)
&& sprout::math::detail::is_even_unchecked(x)
;
} }
} // namespace detail } // namespace detail

View file

@ -4,18 +4,27 @@
#include <type_traits> #include <type_traits>
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/type_traits/enabler_if.hpp> #include <sprout/type_traits/enabler_if.hpp>
#include <sprout/math/isfinite.hpp>
#include <sprout/math/trunc.hpp> #include <sprout/math/trunc.hpp>
namespace sprout { namespace sprout {
namespace math { namespace math {
namespace detail { namespace detail {
template<typename T>
inline SPROUT_CONSTEXPR bool
is_integer_unchecked(T x) {
return x == sprout::math::trunc(x);
}
template< template<
typename FloatType, typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
> >
inline SPROUT_CONSTEXPR bool inline SPROUT_CONSTEXPR bool
is_integer(FloatType x) { is_integer(FloatType x) {
return x == sprout::math::trunc(x); return sprout::math::isfinite(x)
&& sprout::math::detail::is_integer_unchecked(x)
;
} }
} // namespace detail } // namespace detail

View file

@ -4,18 +4,27 @@
#include <type_traits> #include <type_traits>
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/type_traits/enabler_if.hpp> #include <sprout/type_traits/enabler_if.hpp>
#include <sprout/math/isfinite.hpp>
#include <sprout/math/fmod.hpp> #include <sprout/math/fmod.hpp>
namespace sprout { namespace sprout {
namespace math { namespace math {
namespace detail { namespace detail {
template<typename T>
inline SPROUT_CONSTEXPR bool
is_odd_unchecked(T x) {
return sprout::math::fmod(x, T(2)) == T(1);
}
template< template<
typename FloatType, typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
> >
inline SPROUT_CONSTEXPR bool inline SPROUT_CONSTEXPR bool
is_odd(FloatType x) { is_odd(FloatType x) {
return sprout::math::fmod(x, FloatType(2)) == FloatType(1); return sprout::math::isfinite(x)
&& sprout::math::detail::is_odd_unchecked(x)
;
} }
} // namespace detail } // namespace detail

View file

@ -38,6 +38,7 @@ namespace sprout {
init = false; init = false;
} }
public: public:
// 20.5.4.1, constructors
SPROUT_CONSTEXPR optional() SPROUT_NOEXCEPT SPROUT_CONSTEXPR optional() SPROUT_NOEXCEPT
: init(false) : init(false)
{} {}
@ -61,7 +62,7 @@ namespace sprout {
: init(v.is_initialized()) : init(v.is_initialized())
, val(v.is_initialized() ? holder_type(*v) : holder_type()) , val(v.is_initialized() ? holder_type(*v) : holder_type())
{} {}
// 20.5.4.3, assignment
optional& operator=(sprout::nullopt_t v) SPROUT_NOEXCEPT { optional& operator=(sprout::nullopt_t v) SPROUT_NOEXCEPT {
assign(v); assign(v);
return *this; return *this;
@ -106,14 +107,14 @@ namespace sprout {
void reset(argument_type v) { void reset(argument_type v) {
assign(v); assign(v);
} }
// 20.5.4.4, swap
void swap(optional& other) void swap(optional& other)
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(sprout::swap(val, other.val))) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(sprout::swap(val, other.val)))
{ {
sprout::swap(init, other.init); sprout::swap(init, other.init);
sprout::swap(val, other.val); sprout::swap(val, other.val);
} }
// 20.5.4.5, observers
SPROUT_CONSTEXPR reference_const_type operator*() const { SPROUT_CONSTEXPR reference_const_type operator*() const {
return (SPROUT_ASSERT(is_initialized()), true) ? val.get() return (SPROUT_ASSERT(is_initialized()), true) ? val.get()
: val.get() : val.get()