This commit is contained in:
bolero-MURAKAMI 2013-05-07 13:40:18 +09:00
parent 56cb7702ea
commit 3498e9214f
4 changed files with 123 additions and 23 deletions

View file

@ -3,23 +3,66 @@
#include <limits>
#include <type_traits>
#include <stdexcept>
#include <sprout/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/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/float_promote.hpp>
namespace sprout {
namespace math {
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>
inline SPROUT_CONSTEXPR T
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<
@ -34,9 +77,9 @@ namespace sprout {
: 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()
: x == 0 ? x
: y == std::numeric_limits<FloatType>::infinity() || y == -std::numeric_limits<FloatType>::infinity() ? x
: static_cast<FloatType>(sprout::math::detail::fmod_impl(
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));
}
} // namespace detail
//
// issue:
// fmod(-NaN, -NaN) returns -NaN .
// # returns +NaN . ( same as fmod(+NaN, +NaN) )
//
using sprout::math::detail::fmod;
} // namespace math

View file

@ -6,6 +6,9 @@
#include <stdexcept>
#include <sprout/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/type_traits/enabler_if.hpp>
#include <sprout/type_traits/float_promote.hpp>
@ -13,6 +16,12 @@
namespace sprout {
namespace math {
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<
typename R = int,
typename FloatType,
@ -20,14 +29,17 @@ namespace sprout {
>
inline SPROUT_CONSTEXPR R
quotient(FloatType x, FloatType y) {
return x == std::numeric_limits<FloatType>::infinity() || x == -std::numeric_limits<FloatType>::infinity() || y == 0
? std::numeric_limits<FloatType>::quiet_NaN()
: x == 0 ? FloatType(0)
: y == std::numeric_limits<FloatType>::infinity() || y == -std::numeric_limits<FloatType>::infinity() ? FloatType(0)
: sprout::math::iround<R>(x / y)
return sprout::math::isnan(y) || sprout::math::isnan(x) ? R(0)
: x == 0 && y != 0 ? R(0)
: x == std::numeric_limits<FloatType>::infinity() || x == -std::numeric_limits<FloatType>::infinity() || y == 0
? R(0)
: 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<
typename R = int,
typename ArithmeticType1,

View file

@ -6,6 +6,9 @@
#include <stdexcept>
#include <sprout/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/type_traits/enabler_if.hpp>
#include <sprout/type_traits/float_promote.hpp>
@ -14,17 +17,20 @@
namespace sprout {
namespace math {
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>
inline SPROUT_CONSTEXPR sprout::pair<T, R>
rem_quo_impl(T x, T y, R quo) {
typedef sprout::pair<T, R> type;
return x == std::numeric_limits<T>::infinity() || x == -std::numeric_limits<T>::infinity() || y == 0
? 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)
;
return type(x - quo * y, quo);
}
template<
typename R = int,
typename FloatType,
@ -32,9 +38,23 @@ namespace sprout {
>
inline SPROUT_CONSTEXPR sprout::pair<FloatType, R>
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<
typename R = int,
typename ArithmeticType1,

View file

@ -6,6 +6,9 @@
#include <stdexcept>
#include <sprout/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/type_traits/enabler_if.hpp>
#include <sprout/type_traits/float_promote.hpp>
@ -13,20 +16,38 @@
namespace sprout {
namespace math {
namespace detail {
template<typename T>
inline SPROUT_CONSTEXPR T
remainder_impl(T x, T y) {
return x - sprout::math::round(x / y) * y;
}
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
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()
: x == 0 ? FloatType(0)
: 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<
typename ArithmeticType1,
typename ArithmeticType2,