1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2025-08-03 12:49:50 +00:00

fix math functions

This commit is contained in:
bolero-MURAKAMI 2013-05-08 01:46:40 +09:00
parent 3498e9214f
commit 9247693c63
13 changed files with 159 additions and 67 deletions

View file

@ -3,13 +3,14 @@
#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/quotient.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>
#include <sprout/utility/pair/pair.hpp>
@ -26,9 +27,58 @@ namespace sprout {
template<typename R, typename T>
inline SPROUT_CONSTEXPR sprout::pair<T, R>
rem_quo_impl(T x, T y, R quo) {
rem_quo_impl_7(T x, T y, int k, R iquo, T x1, T z) {
typedef sprout::pair<T, R> type;
return type(x - quo * y, quo);
return (z > y) || ((z == y) && ((iquo & 1) != 0))
? x < 0 ? type(-(x1 - y), ((iquo + 1) & 0x7) * k) : type(x1 - y, ((iquo + 1) & 0x7) * k)
: x < 0 ? type(-x1, (iquo & 0x7) * k) : type(x1, (iquo & 0x7) * k)
;
}
template<typename R, typename T>
inline SPROUT_CONSTEXPR sprout::pair<T, R>
rem_quo_impl_6(T x, T y, int k, R iquo, T x1) {
return x1 >= y
? sprout::math::detail::rem_quo_impl_7(x, y, k, iquo + 1, x1 - y, (x1 - y) + (x1 - y))
: sprout::math::detail::rem_quo_impl_7(x, y, k, iquo, x1, x1 + x1)
;
}
template<typename R, typename T>
inline SPROUT_CONSTEXPR sprout::pair<T, R>
rem_quo_impl_5(T x, T y, int k, R iquo, T x1, T y1, T z, int iscy, int idiff, int i) {
return i != idiff
? z >= 0
? sprout::math::detail::rem_quo_impl_5(x, y, k, iquo + iquo + 2, z + z, y1, z + z - y1, iscy, idiff, i + 1)
: sprout::math::detail::rem_quo_impl_5(x, y, k, iquo + iquo, x1 + x1, y1, x1 + x1 - y1, iscy, idiff, i + 1)
: sprout::math::detail::rem_quo_impl_6(x, y, k, iquo, sprout::math::scalbn(x1, iscy))
;
}
template<typename R, typename T>
inline SPROUT_CONSTEXPR sprout::pair<T, R>
rem_quo_impl_4(T x, T y, int k, R iquo, T x1, T y1, int iscy, int idiff, int i) {
return sprout::math::detail::rem_quo_impl_5(x, y, k, iquo, x1, y1, x1 - y1, iscy, idiff, i);
}
template<typename R, typename T>
inline SPROUT_CONSTEXPR sprout::pair<T, R>
rem_quo_impl_3(T x, T y, int k, R iquo, T x1, int iscx, int iscy, int idiff) {
return idiff < 0 ? sprout::math::detail::rem_quo_impl_7(x, y, k, iquo, x1, x1 + x1)
: idiff ? sprout::math::detail::rem_quo_impl_4(x, y, k, iquo, sprout::math::scalbn(x1, -iscx), sprout::math::scalbn(y, -iscy), iscy, idiff, 0)
: sprout::math::detail::rem_quo_impl_6(x, y, k, iquo, x1)
;
}
template<typename R, typename T>
inline SPROUT_CONSTEXPR sprout::pair<T, R>
rem_quo_impl_2(T x, T y, int k, R iquo, T x1, int iscx, int iscy) {
return sprout::math::detail::rem_quo_impl_3(x, y, k, iquo, x1, iscx, iscy, iscx - iscy);
}
template<typename R, typename T>
inline SPROUT_CONSTEXPR sprout::pair<T, R>
rem_quo_impl_1(T x, T y, int k, R iquo, T x1) {
return sprout::math::detail::rem_quo_impl_2(x, y, k, iquo, x1, sprout::math::ilogb(x1), sprout::math::ilogb(y));
}
template<typename R, typename T>
inline SPROUT_CONSTEXPR sprout::pair<T, R>
rem_quo_impl(T x, T y, R iquo) {
return sprout::math::detail::rem_quo_impl_1(x, sprout::math::fabs(y), (sprout::math::signbit(x) ^ sprout::math::signbit(y) ? -1 : 1), iquo, sprout::math::fabs(x));
}
template<
@ -39,19 +89,20 @@ namespace sprout {
inline SPROUT_CONSTEXPR sprout::pair<FloatType, R>
rem_quo(FloatType x, FloatType y) {
typedef sprout::pair<FloatType, R> type;
return sprout::math::isnan(y)
return y == 0 ? type(-std::numeric_limits<FloatType>::quiet_NaN(), R(0))
: 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(sprout::math::signbit(y) && sprout::math::signbit(x) ? -std::numeric_limits<FloatType>::quiet_NaN() : 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))
: sprout::math::isnan(x) ? type(x, R(0))
: x == std::numeric_limits<FloatType>::infinity() || x == -std::numeric_limits<FloatType>::infinity()
? type(-std::numeric_limits<FloatType>::quiet_NaN(), R(0))
: x == 0 ? type(x, 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)
R(0)
))
;
}
@ -72,7 +123,11 @@ namespace sprout {
return sprout::math::detail::rem_quo(static_cast<type>(x), static_cast<type>(y));
}
} // namespace detail
//
// issue:
// rem_quo(-NaN, -NaN) returns {-NaN, _} .
// # returns {+NaN, _} . ( same as rem_quo(+NaN, +NaN) )
//
using sprout::math::detail::rem_quo;
} // namespace math