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:
parent
3498e9214f
commit
9247693c63
13 changed files with 159 additions and 67 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue