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

@ -1,9 +1,11 @@
#ifndef SPROUT_MATH_FDIM_HPP
#define SPROUT_MATH_FDIM_HPP
#include <limits>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/math/detail/config.hpp>
#include <sprout/math/isnan.hpp>
#include <sprout/type_traits/float_promote.hpp>
#include <sprout/type_traits/enabler_if.hpp>
@ -16,9 +18,19 @@ namespace sprout {
>
inline SPROUT_CONSTEXPR FloatType
fdim(FloatType x, FloatType y) {
return x > y ? x - y : 0;
return sprout::math::isnan(y) ? y
: sprout::math::isnan(x) ? x
: x == std::numeric_limits<FloatType>::infinity() ? std::numeric_limits<FloatType>::infinity()
: x == -std::numeric_limits<FloatType>::infinity() ? FloatType(0)
: y == std::numeric_limits<FloatType>::infinity() ? FloatType(0)
: y == -std::numeric_limits<FloatType>::infinity() ? std::numeric_limits<FloatType>::infinity()
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
: std::fdim(x, y)
#else
: x > y ? x - y : FloatType(0)
#endif
;
}
template<
typename ArithmeticType1,
typename ArithmeticType2,
@ -33,7 +45,7 @@ namespace sprout {
}
} // namespace detail
using NS_SPROUT_MATH_DETAIL::fdim;
using sprout::math::detail::fdim;
} // namespace math
using sprout::math::fdim;

View file

@ -1,9 +1,13 @@
#ifndef SPROUT_MATH_FMA_HPP
#define SPROUT_MATH_FMA_HPP
#include <limits>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/math/detail/config.hpp>
#include <sprout/math/isnan.hpp>
#include <sprout/math/isinf.hpp>
#include <sprout/math/signbit.hpp>
#include <sprout/type_traits/float_promote.hpp>
#include <sprout/type_traits/enabler_if.hpp>
@ -16,9 +20,20 @@ namespace sprout {
>
inline SPROUT_CONSTEXPR FloatType
fma(FloatType x, FloatType y, FloatType z) {
return x * y + z;
return sprout::math::isnan(x) ? x
: sprout::math::isnan(y) ? y
: sprout::math::isnan(z) ? z
: sprout::math::isinf(x) && y == 0 ? std::numeric_limits<FloatType>::quiet_NaN()
: sprout::math::isinf(y) && x == 0 ? std::numeric_limits<FloatType>::quiet_NaN()
: (sprout::math::isinf(x) || sprout::math::isinf(y)) && sprout::math::isinf(z) && (sprout::math::signbit(x) ^ sprout::math::signbit(y) ^ sprout::math::signbit(z))
? std::numeric_limits<FloatType>::quiet_NaN()
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
: std::fma(x, y, z)
#else
: x * y + z
#endif
;
}
template<
typename ArithmeticType1,
typename ArithmeticType2,
@ -40,7 +55,7 @@ namespace sprout {
}
} // namespace detail
using NS_SPROUT_MATH_DETAIL::fma;
using sprout::math::detail::fma;
} // namespace math
using sprout::math::fma;

View file

@ -1,6 +1,7 @@
#ifndef SPROUT_MATH_FMAX_HPP
#define SPROUT_MATH_FMAX_HPP
#include <limits>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/math/detail/config.hpp>
@ -17,7 +18,19 @@ namespace sprout {
>
inline SPROUT_CONSTEXPR FloatType
fmax(FloatType x, FloatType y) {
return x < y && !sprout::math::isnan(y) ? y : x;
return sprout::math::isnan(y) ? x
: sprout::math::isnan(x) ? y
: y == -std::numeric_limits<FloatType>::infinity() ? x
: x == std::numeric_limits<FloatType>::infinity() ? x
: x == -std::numeric_limits<FloatType>::infinity() ? y
: y == std::numeric_limits<FloatType>::infinity() ? y
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
: x == 0 && y == 0 ? x
: std::fmax(x, y)
#else
: x < y ? y : x
#endif
;
}
template<
typename ArithmeticType1,
@ -33,7 +46,7 @@ namespace sprout {
}
} // namespace detail
using NS_SPROUT_MATH_DETAIL::fmax;
using sprout::math::detail::fmax;
} // namespace math
using sprout::math::fmax;

View file

@ -1,6 +1,7 @@
#ifndef SPROUT_MATH_FMIN_HPP
#define SPROUT_MATH_FMIN_HPP
#include <limits>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/math/detail/config.hpp>
@ -17,7 +18,19 @@ namespace sprout {
>
inline SPROUT_CONSTEXPR FloatType
fmin(FloatType x, FloatType y) {
return y < x && !sprout::math::isnan(y) ? y : x;
return sprout::math::isnan(y) ? x
: sprout::math::isnan(x) ? y
: x == -std::numeric_limits<FloatType>::infinity() ? x
: y == std::numeric_limits<FloatType>::infinity() ? x
: y == -std::numeric_limits<FloatType>::infinity() ? y
: x == std::numeric_limits<FloatType>::infinity() ? y
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
: x == 0 && y == 0 ? x
: std::fmin(x, y)
#else
: y < x ? y : x
#endif
;
}
template<
typename ArithmeticType1,
@ -33,7 +46,7 @@ namespace sprout {
}
} // namespace detail
using NS_SPROUT_MATH_DETAIL::fmin;
using sprout::math::detail::fmin;
} // namespace math
using sprout::math::fmin;

View file

@ -77,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),

View file

@ -17,7 +17,7 @@ namespace sprout {
>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR bool
greater_equal_equal(FloatType1 x, FloatType2 y) {
greater_equal(FloatType1 x, FloatType2 y) {
return sprout::math::equal_to(x, y) || x > y;
}
@ -62,7 +62,7 @@ namespace sprout {
}
} // namespace detail
//
// greater_equal_equal
// greater_equal
//
template<
typename T, typename U,

View file

@ -12,7 +12,7 @@
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
# include <sprout/math/ceil.hpp>
#else
# include <sprout/math/equal_to.hpp>
# include <sprout/math/less_equal.hpp>
#endif
namespace sprout {
@ -42,7 +42,7 @@ namespace sprout {
template<typename To, typename FloatType>
inline SPROUT_CONSTEXPR To
iceil_impl(FloatType x, To x0) {
return sprout::math::equal_to(x, x0) ? x0
return sprout::math::less_equal(x, x0) ? x0
: x0 + 1
;
}

View file

@ -12,7 +12,7 @@
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
# include <sprout/math/floor.hpp>
#else
# include <sprout/math/equal_to.hpp>
# include <sprout/math/greater_equal.hpp>
#endif
namespace sprout {
@ -42,7 +42,7 @@ namespace sprout {
template<typename To, typename FloatType>
inline SPROUT_CONSTEXPR To
ifloor_impl(FloatType x, To x0) {
return sprout::math::equal_to(x, x0) ? x0
return sprout::math::greater_equal(x, x0) ? x0
: x0 - 1
;
}

View file

@ -18,7 +18,7 @@ namespace sprout {
>
inline SPROUT_CONSTEXPR bool
less_equal(FloatType1 x, FloatType2 y) {
return sprout::math::not_equal_to(x, y) || x < y;
return sprout::math::equal_to(x, y) || x < y;
}
template<

View file

@ -2,8 +2,8 @@
#define SPROUT_MATH_MINMAX_HPP
#include <sprout/config.hpp>
#include <sprout/math/fdim.hpp>
#include <sprout/math/fmax.hpp>
#include <sprout/math/fmin.hpp>
#include <sprout/math/fdim.hpp>
#endif // #ifndef SPROUT_MATH_MINMAX_HPP

View file

@ -3,25 +3,17 @@
#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/iround.hpp>
#include <sprout/math/rem_quo.hpp>
#include <sprout/type_traits/enabler_if.hpp>
#include <sprout/type_traits/float_promote.hpp>
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,
@ -29,15 +21,13 @@ namespace sprout {
>
inline SPROUT_CONSTEXPR R
quotient(FloatType x, FloatType 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
return y == 0 ? R(0)
: sprout::math::isnan(y) || sprout::math::isnan(x) ? R(0)
: x == std::numeric_limits<FloatType>::infinity() || x == -std::numeric_limits<FloatType>::infinity()
? R(0)
: x == 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)
))
: sprout::math::rem_quo<R>(x, y).second
;
}
template<

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

View file

@ -3,48 +3,38 @@
#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/round.hpp>
#include <sprout/math/rem_quo.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
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 sprout::math::isnan(y)
return y == 0 ? -std::numeric_limits<FloatType>::quiet_NaN()
: sprout::math::isnan(y)
? sprout::math::isnan(x)
? sprout::math::signbit(y) || sprout::math::signbit(x) ? -std::numeric_limits<FloatType>::quiet_NaN()
? 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 == std::numeric_limits<FloatType>::infinity() || x == -std::numeric_limits<FloatType>::infinity()
? -std::numeric_limits<FloatType>::quiet_NaN()
: x == 0 ? x
: y == std::numeric_limits<FloatType>::infinity() || y == -std::numeric_limits<FloatType>::infinity() ? x
#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)
))
: sprout::math::rem_quo(x, y).first
#endif
;
}
@ -61,8 +51,12 @@ namespace sprout {
return sprout::math::detail::remainder(static_cast<type>(x), static_cast<type>(y));
}
} // namespace detail
using NS_SPROUT_MATH_DETAIL::remainder;
//
// issue:
// remainder(-NaN, -NaN) returns -NaN .
// # returns +NaN . ( same as remainder(+NaN, +NaN) )
//
using sprout::math::detail::remainder;
} // namespace math
using sprout::math::remainder;