mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2025-05-12 09:33:30 +00:00
fix fmod
This commit is contained in:
parent
56cb7702ea
commit
3498e9214f
4 changed files with 123 additions and 23 deletions
|
@ -3,23 +3,66 @@
|
||||||
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#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/detail/float_compute.hpp>
|
#include <sprout/math/detail/float_compute.hpp>
|
||||||
#include <sprout/math/isnan.hpp>
|
#include <sprout/math/isnan.hpp>
|
||||||
#include <sprout/math/signbit.hpp>
|
#include <sprout/math/signbit.hpp>
|
||||||
#include <sprout/math/trunc.hpp>
|
#include <sprout/math/fabs.hpp>
|
||||||
|
#include <sprout/math/ilogb.hpp>
|
||||||
|
#include <sprout/math/scalbn.hpp>
|
||||||
#include <sprout/type_traits/enabler_if.hpp>
|
#include <sprout/type_traits/enabler_if.hpp>
|
||||||
#include <sprout/type_traits/float_promote.hpp>
|
#include <sprout/type_traits/float_promote.hpp>
|
||||||
|
|
||||||
namespace sprout {
|
namespace sprout {
|
||||||
namespace math {
|
namespace math {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
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))
|
||||||
|
;
|
||||||
|
}
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline SPROUT_CONSTEXPR T
|
inline SPROUT_CONSTEXPR T
|
||||||
fmod_impl(T x, T y) {
|
fmod_impl(T x, T y) {
|
||||||
return x - sprout::math::trunc(x / y) * y;
|
return sprout::math::detail::fmod_impl_1(x, sprout::math::fabs(y), sprout::math::fabs(x));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<
|
template<
|
||||||
|
@ -34,9 +77,9 @@ namespace sprout {
|
||||||
: std::numeric_limits<FloatType>::quiet_NaN()
|
: std::numeric_limits<FloatType>::quiet_NaN()
|
||||||
: y
|
: y
|
||||||
: sprout::math::isnan(x) ? x
|
: sprout::math::isnan(x) ? x
|
||||||
|
: x == 0 && y != 0 ? x
|
||||||
: x == std::numeric_limits<FloatType>::infinity() || x == -std::numeric_limits<FloatType>::infinity() || y == 0
|
: x == std::numeric_limits<FloatType>::infinity() || x == -std::numeric_limits<FloatType>::infinity() || y == 0
|
||||||
? -std::numeric_limits<FloatType>::quiet_NaN()
|
? -std::numeric_limits<FloatType>::quiet_NaN()
|
||||||
: x == 0 ? x
|
|
||||||
: y == std::numeric_limits<FloatType>::infinity() || y == -std::numeric_limits<FloatType>::infinity() ? x
|
: y == std::numeric_limits<FloatType>::infinity() || y == -std::numeric_limits<FloatType>::infinity() ? x
|
||||||
: static_cast<FloatType>(sprout::math::detail::fmod_impl(
|
: 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>(x),
|
||||||
|
@ -58,7 +101,11 @@ namespace sprout {
|
||||||
return sprout::math::detail::fmod(static_cast<type>(x), static_cast<type>(y));
|
return sprout::math::detail::fmod(static_cast<type>(x), static_cast<type>(y));
|
||||||
}
|
}
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
//
|
||||||
|
// issue:
|
||||||
|
// fmod(-NaN, -NaN) returns -NaN .
|
||||||
|
// # returns +NaN . ( same as fmod(+NaN, +NaN) )
|
||||||
|
//
|
||||||
using sprout::math::detail::fmod;
|
using sprout::math::detail::fmod;
|
||||||
} // namespace math
|
} // namespace math
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,9 @@
|
||||||
#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/detail/float_compute.hpp>
|
||||||
|
#include <sprout/math/isnan.hpp>
|
||||||
|
#include <sprout/math/signbit.hpp>
|
||||||
#include <sprout/math/iround.hpp>
|
#include <sprout/math/iround.hpp>
|
||||||
#include <sprout/type_traits/enabler_if.hpp>
|
#include <sprout/type_traits/enabler_if.hpp>
|
||||||
#include <sprout/type_traits/float_promote.hpp>
|
#include <sprout/type_traits/float_promote.hpp>
|
||||||
|
@ -13,6 +16,12 @@
|
||||||
namespace sprout {
|
namespace sprout {
|
||||||
namespace math {
|
namespace math {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
template<typename R, typename T>
|
||||||
|
inline SPROUT_CONSTEXPR T
|
||||||
|
quotient_impl(T x, T y) {
|
||||||
|
return sprout::math::iround<R>(x / y);
|
||||||
|
}
|
||||||
|
|
||||||
template<
|
template<
|
||||||
typename R = int,
|
typename R = int,
|
||||||
typename FloatType,
|
typename FloatType,
|
||||||
|
@ -20,14 +29,17 @@ namespace sprout {
|
||||||
>
|
>
|
||||||
inline SPROUT_CONSTEXPR R
|
inline SPROUT_CONSTEXPR R
|
||||||
quotient(FloatType x, FloatType y) {
|
quotient(FloatType x, FloatType y) {
|
||||||
return x == std::numeric_limits<FloatType>::infinity() || x == -std::numeric_limits<FloatType>::infinity() || y == 0
|
return sprout::math::isnan(y) || sprout::math::isnan(x) ? R(0)
|
||||||
? std::numeric_limits<FloatType>::quiet_NaN()
|
: x == 0 && y != 0 ? R(0)
|
||||||
: x == 0 ? FloatType(0)
|
: x == std::numeric_limits<FloatType>::infinity() || x == -std::numeric_limits<FloatType>::infinity() || y == 0
|
||||||
: y == std::numeric_limits<FloatType>::infinity() || y == -std::numeric_limits<FloatType>::infinity() ? FloatType(0)
|
? R(0)
|
||||||
: sprout::math::iround<R>(x / y)
|
: y == std::numeric_limits<FloatType>::infinity() || y == -std::numeric_limits<FloatType>::infinity() ? R(0)
|
||||||
|
: static_cast<FloatType>(sprout::math::detail::quotient_impl<R>(
|
||||||
|
static_cast<typename sprout::math::detail::float_compute<FloatType>::type>(x),
|
||||||
|
static_cast<typename sprout::math::detail::float_compute<FloatType>::type>(y)
|
||||||
|
))
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<
|
template<
|
||||||
typename R = int,
|
typename R = int,
|
||||||
typename ArithmeticType1,
|
typename ArithmeticType1,
|
||||||
|
|
|
@ -6,6 +6,9 @@
|
||||||
#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/detail/float_compute.hpp>
|
||||||
|
#include <sprout/math/isnan.hpp>
|
||||||
|
#include <sprout/math/signbit.hpp>
|
||||||
#include <sprout/math/quotient.hpp>
|
#include <sprout/math/quotient.hpp>
|
||||||
#include <sprout/type_traits/enabler_if.hpp>
|
#include <sprout/type_traits/enabler_if.hpp>
|
||||||
#include <sprout/type_traits/float_promote.hpp>
|
#include <sprout/type_traits/float_promote.hpp>
|
||||||
|
@ -14,17 +17,20 @@
|
||||||
namespace sprout {
|
namespace sprout {
|
||||||
namespace math {
|
namespace math {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
template<typename R, typename FloatType, typename Pair>
|
||||||
|
inline SPROUT_CONSTEXPR sprout::pair<FloatType, R>
|
||||||
|
rem_quo_ret(Pair const& p) {
|
||||||
|
typedef sprout::pair<FloatType, R> type;
|
||||||
|
return type(static_cast<FloatType>(p.first), p.second);
|
||||||
|
}
|
||||||
|
|
||||||
template<typename R, typename T>
|
template<typename R, typename T>
|
||||||
inline SPROUT_CONSTEXPR sprout::pair<T, R>
|
inline SPROUT_CONSTEXPR sprout::pair<T, R>
|
||||||
rem_quo_impl(T x, T y, R quo) {
|
rem_quo_impl(T x, T y, R quo) {
|
||||||
typedef sprout::pair<T, R> type;
|
typedef sprout::pair<T, R> type;
|
||||||
return x == std::numeric_limits<T>::infinity() || x == -std::numeric_limits<T>::infinity() || y == 0
|
return type(x - quo * y, quo);
|
||||||
? type(std::numeric_limits<T>::quiet_NaN(), quo)
|
|
||||||
: x == 0 ? type(T(0), quo)
|
|
||||||
: y == std::numeric_limits<T>::infinity() || y == -std::numeric_limits<T>::infinity() ? type(x, quo)
|
|
||||||
: type(x - quo * y, quo)
|
|
||||||
;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<
|
template<
|
||||||
typename R = int,
|
typename R = int,
|
||||||
typename FloatType,
|
typename FloatType,
|
||||||
|
@ -32,9 +38,23 @@ namespace sprout {
|
||||||
>
|
>
|
||||||
inline SPROUT_CONSTEXPR sprout::pair<FloatType, R>
|
inline SPROUT_CONSTEXPR sprout::pair<FloatType, R>
|
||||||
rem_quo(FloatType x, FloatType y) {
|
rem_quo(FloatType x, FloatType y) {
|
||||||
return sprout::math::detail::rem_quo_impl(x, y, sprout::quotient(x, y));
|
typedef sprout::pair<FloatType, R> type;
|
||||||
|
return sprout::math::isnan(y)
|
||||||
|
? sprout::math::isnan(x)
|
||||||
|
? sprout::math::signbit(y) || sprout::math::signbit(x) ? type(-std::numeric_limits<FloatType>::quiet_NaN(), R(0))
|
||||||
|
: type(std::numeric_limits<FloatType>::quiet_NaN(), R(0))
|
||||||
|
: type(y, R(0))
|
||||||
|
: x == 0 && y != 0 ? type(FloatType(0), R(0))
|
||||||
|
: x == std::numeric_limits<FloatType>::infinity() || x == -std::numeric_limits<FloatType>::infinity() || y == 0
|
||||||
|
? type(std::numeric_limits<FloatType>::quiet_NaN(), R(0))
|
||||||
|
: y == std::numeric_limits<FloatType>::infinity() || y == -std::numeric_limits<FloatType>::infinity() ? type(x, R(0))
|
||||||
|
: sprout::math::detail::rem_quo_ret<R, FloatType>(sprout::math::detail::rem_quo_impl(
|
||||||
|
static_cast<typename sprout::math::detail::float_compute<FloatType>::type>(x),
|
||||||
|
static_cast<typename sprout::math::detail::float_compute<FloatType>::type>(y),
|
||||||
|
sprout::math::quotient<R>(x, y)
|
||||||
|
))
|
||||||
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<
|
template<
|
||||||
typename R = int,
|
typename R = int,
|
||||||
typename ArithmeticType1,
|
typename ArithmeticType1,
|
||||||
|
|
|
@ -6,6 +6,9 @@
|
||||||
#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/detail/float_compute.hpp>
|
||||||
|
#include <sprout/math/isnan.hpp>
|
||||||
|
#include <sprout/math/signbit.hpp>
|
||||||
#include <sprout/math/round.hpp>
|
#include <sprout/math/round.hpp>
|
||||||
#include <sprout/type_traits/enabler_if.hpp>
|
#include <sprout/type_traits/enabler_if.hpp>
|
||||||
#include <sprout/type_traits/float_promote.hpp>
|
#include <sprout/type_traits/float_promote.hpp>
|
||||||
|
@ -13,20 +16,38 @@
|
||||||
namespace sprout {
|
namespace sprout {
|
||||||
namespace math {
|
namespace math {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
template<typename T>
|
||||||
|
inline SPROUT_CONSTEXPR T
|
||||||
|
remainder_impl(T x, T y) {
|
||||||
|
return x - sprout::math::round(x / y) * y;
|
||||||
|
}
|
||||||
|
|
||||||
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 FloatType
|
inline SPROUT_CONSTEXPR FloatType
|
||||||
remainder(FloatType x, FloatType y) {
|
remainder(FloatType x, FloatType y) {
|
||||||
return x == std::numeric_limits<FloatType>::infinity() || x == -std::numeric_limits<FloatType>::infinity() || y == 0
|
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
|
||||||
|
: x == 0 && y != 0 ? x
|
||||||
|
: x == std::numeric_limits<FloatType>::infinity() || x == -std::numeric_limits<FloatType>::infinity() || y == 0
|
||||||
? std::numeric_limits<FloatType>::quiet_NaN()
|
? std::numeric_limits<FloatType>::quiet_NaN()
|
||||||
: x == 0 ? FloatType(0)
|
|
||||||
: y == std::numeric_limits<FloatType>::infinity() || y == -std::numeric_limits<FloatType>::infinity() ? x
|
: y == std::numeric_limits<FloatType>::infinity() || y == -std::numeric_limits<FloatType>::infinity() ? x
|
||||||
: x - sprout::math::round(x / y) * y
|
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||||
|
: std::remainder(x, y)
|
||||||
|
#else
|
||||||
|
: static_cast<FloatType>(sprout::math::detail::remainder_impl(
|
||||||
|
static_cast<typename sprout::math::detail::float_compute<FloatType>::type>(x),
|
||||||
|
static_cast<typename sprout::math::detail::float_compute<FloatType>::type>(y)
|
||||||
|
))
|
||||||
|
#endif
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<
|
template<
|
||||||
typename ArithmeticType1,
|
typename ArithmeticType1,
|
||||||
typename ArithmeticType2,
|
typename ArithmeticType2,
|
||||||
|
|
Loading…
Add table
Reference in a new issue