2012-12-02 08:06:37 +00:00
|
|
|
#ifndef SPROUT_MATH_FMOD_HPP
|
|
|
|
#define SPROUT_MATH_FMOD_HPP
|
|
|
|
|
2013-02-14 09:02:43 +00:00
|
|
|
#include <limits>
|
2012-12-02 08:06:37 +00:00
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
2013-05-05 15:22:08 +00:00
|
|
|
#include <sprout/math/detail/config.hpp>
|
|
|
|
#include <sprout/math/detail/float_compute.hpp>
|
|
|
|
#include <sprout/math/isnan.hpp>
|
|
|
|
#include <sprout/math/signbit.hpp>
|
2013-05-07 04:40:18 +00:00
|
|
|
#include <sprout/math/fabs.hpp>
|
|
|
|
#include <sprout/math/ilogb.hpp>
|
|
|
|
#include <sprout/math/scalbn.hpp>
|
2012-12-02 08:06:37 +00:00
|
|
|
#include <sprout/type_traits/enabler_if.hpp>
|
|
|
|
#include <sprout/type_traits/float_promote.hpp>
|
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace math {
|
|
|
|
namespace detail {
|
2013-05-07 04:40:18 +00:00
|
|
|
template<typename T>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
|
|
|
fmod_impl_6(T x, T y, T x1) {
|
|
|
|
return x1 >= y
|
|
|
|
? x < 0 ? -(x1 - y) : x1 - y
|
|
|
|
: x < 0 ? -x1 : x1
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
|
|
|
fmod_impl_5(T x, T y, T x1, T y1, T z, int iscy, int idiff, int i) {
|
|
|
|
return i != idiff
|
|
|
|
? z >= 0
|
|
|
|
? sprout::math::detail::fmod_impl_5(x, y, z + z, y1, z + z - y1, iscy, idiff, i + 1)
|
|
|
|
: sprout::math::detail::fmod_impl_5(x, y, x1 + x1, y1, x1 + x1 - y1, iscy, idiff, i + 1)
|
|
|
|
: sprout::math::detail::fmod_impl_6(x, y, sprout::math::scalbn(x1, iscy))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
|
|
|
fmod_impl_4(T x, T y, T x1, T y1, int iscy, int idiff, int i) {
|
|
|
|
return sprout::math::detail::fmod_impl_5(x, y, x1, y1, x1 - y1, iscy, idiff, i);
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
|
|
|
fmod_impl_3(T x, T y, T x1, int iscx, int iscy, int idiff) {
|
|
|
|
return idiff ? sprout::math::detail::fmod_impl_4(x, y, sprout::math::scalbn(x1, -iscx), sprout::math::scalbn(y, -iscy), iscy, idiff, 0)
|
|
|
|
: sprout::math::detail::fmod_impl_6(x, y, x1)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
|
|
|
fmod_impl_2(T x, T y, T x1, int iscx, int iscy) {
|
|
|
|
return sprout::math::detail::fmod_impl_3(x, y, x1, iscx, iscy, iscx - iscy);
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
|
|
|
fmod_impl_1(T x, T y, T x1) {
|
|
|
|
return y > x1 ? x
|
|
|
|
: sprout::math::detail::fmod_impl_2(x, y, x1, sprout::math::ilogb(x1), sprout::math::ilogb(y))
|
|
|
|
;
|
|
|
|
}
|
2013-05-05 15:22:08 +00:00
|
|
|
template<typename T>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
|
|
|
fmod_impl(T x, T y) {
|
2013-05-07 04:40:18 +00:00
|
|
|
return sprout::math::detail::fmod_impl_1(x, sprout::math::fabs(y), sprout::math::fabs(x));
|
2013-05-05 15:22:08 +00:00
|
|
|
}
|
|
|
|
|
2012-12-02 08:06:37 +00:00
|
|
|
template<
|
|
|
|
typename FloatType,
|
|
|
|
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
|
|
|
>
|
|
|
|
inline SPROUT_CONSTEXPR FloatType
|
|
|
|
fmod(FloatType x, FloatType y) {
|
2013-05-05 15:22:08 +00:00
|
|
|
return sprout::math::isnan(y)
|
|
|
|
? sprout::math::isnan(x)
|
|
|
|
? sprout::math::signbit(y) || sprout::math::signbit(x) ? -std::numeric_limits<FloatType>::quiet_NaN()
|
|
|
|
: std::numeric_limits<FloatType>::quiet_NaN()
|
|
|
|
: y
|
|
|
|
: sprout::math::isnan(x) ? x
|
2013-05-07 04:40:18 +00:00
|
|
|
: x == 0 && y != 0 ? x
|
2013-05-05 15:22:08 +00:00
|
|
|
: x == std::numeric_limits<FloatType>::infinity() || x == -std::numeric_limits<FloatType>::infinity() || y == 0
|
|
|
|
? -std::numeric_limits<FloatType>::quiet_NaN()
|
2013-02-14 09:02:43 +00:00
|
|
|
: y == std::numeric_limits<FloatType>::infinity() || y == -std::numeric_limits<FloatType>::infinity() ? x
|
2013-05-05 15:22:08 +00:00
|
|
|
: static_cast<FloatType>(sprout::math::detail::fmod_impl(
|
|
|
|
static_cast<typename sprout::math::detail::float_compute<FloatType>::type>(x),
|
|
|
|
static_cast<typename sprout::math::detail::float_compute<FloatType>::type>(y)
|
|
|
|
))
|
2012-12-02 08:06:37 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<
|
|
|
|
typename ArithmeticType1,
|
|
|
|
typename ArithmeticType2,
|
|
|
|
typename sprout::enabler_if<
|
|
|
|
std::is_arithmetic<ArithmeticType1>::value && std::is_arithmetic<ArithmeticType2>::value
|
|
|
|
>::type = sprout::enabler
|
|
|
|
>
|
|
|
|
inline SPROUT_CONSTEXPR typename sprout::float_promote<ArithmeticType1, ArithmeticType2>::type
|
|
|
|
fmod(ArithmeticType1 x, ArithmeticType2 y) {
|
|
|
|
typedef typename sprout::float_promote<ArithmeticType1, ArithmeticType2>::type type;
|
|
|
|
return sprout::math::detail::fmod(static_cast<type>(x), static_cast<type>(y));
|
|
|
|
}
|
|
|
|
} // namespace detail
|
2013-05-07 04:40:18 +00:00
|
|
|
//
|
|
|
|
// issue:
|
|
|
|
// fmod(-NaN, -NaN) returns -NaN .
|
|
|
|
// # returns +NaN . ( same as fmod(+NaN, +NaN) )
|
|
|
|
//
|
2012-12-02 08:06:37 +00:00
|
|
|
using sprout::math::detail::fmod;
|
|
|
|
} // namespace math
|
|
|
|
|
|
|
|
using sprout::math::fmod;
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_MATH_FMOD_HPP
|