mirror of
https://github.com/bolero-MURAKAMI/Sprout
synced 2024-11-12 21:09:01 +00:00
fix math functions
This commit is contained in:
parent
7794e56192
commit
56cb7702ea
19 changed files with 157 additions and 69 deletions
|
@ -12,12 +12,12 @@ namespace sprout {
|
|||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
ceil(sprout::complex<T> const& x) {
|
||||
return sprout::complex<T>(sprout::ceil(x.real()), sprout::ceil(x.imag()));
|
||||
return sprout::complex<T>(sprout::math::ceil(x.real()), sprout::math::ceil(x.imag()));
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
floor(sprout::complex<T> const& x) {
|
||||
return sprout::complex<T>(sprout::floor(x.real()), sprout::floor(x.imag()));
|
||||
return sprout::complex<T>(sprout::math::floor(x.real()), sprout::math::floor(x.imag()));
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
|
@ -27,7 +27,7 @@ namespace sprout {
|
|||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
round(sprout::complex<T> const& x) {
|
||||
return sprout::complex<T>(sprout::round(x.real()), sprout::round(x.imag()));
|
||||
return sprout::complex<T>(sprout::math::round(x.real()), sprout::math::round(x.imag()));
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
|
|
|
@ -103,7 +103,7 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR int
|
||||
float_digit_of_impl(FloatType val) {
|
||||
return static_cast<int>((val - sprout::floor(val)) * 10);
|
||||
return static_cast<int>((val - sprout::math::floor(val)) * 10);
|
||||
}
|
||||
template<typename FloatType>
|
||||
inline SPROUT_CONSTEXPR int
|
||||
|
@ -120,7 +120,7 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
float_round_impl(FloatType val, FloatType p10) {
|
||||
return sprout::round(val * p10) / p10;
|
||||
return sprout::math::round(val * p10) / p10;
|
||||
}
|
||||
template<typename FloatType>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
|
|
|
@ -7,35 +7,48 @@
|
|||
#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/equal_to.hpp>
|
||||
#include <sprout/type_traits/enabler_if.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace math {
|
||||
namespace detail {
|
||||
template<typename FloatType>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
ceil_impl(FloatType x, FloatType x0) {
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR T
|
||||
ceil_impl_1(T x, T x0) {
|
||||
return sprout::math::equal_to(x, x0) ? x0
|
||||
: x0 + 1
|
||||
: x0 + T(1)
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR T
|
||||
ceil_impl(T x) {
|
||||
return x < 0 ? -static_cast<T>(static_cast<std::uintmax_t>(-x))
|
||||
: sprout::math::detail::ceil_impl_1(x, static_cast<T>(static_cast<std::uintmax_t>(x)))
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename FloatType,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
ceil(FloatType x) {
|
||||
return x == 0 ? FloatType(0)
|
||||
return sprout::math::isnan(x) ? x
|
||||
: x == std::numeric_limits<FloatType>::infinity() ? std::numeric_limits<FloatType>::infinity()
|
||||
: x == -std::numeric_limits<FloatType>::infinity() ? -std::numeric_limits<FloatType>::infinity()
|
||||
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
: std::ceil(x)
|
||||
#else
|
||||
: x == 0 ? x
|
||||
: std::numeric_limits<std::uintmax_t>::max() < x || std::numeric_limits<std::uintmax_t>::max() < -x
|
||||
? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("ceil: large float rounding."), x)
|
||||
: x < 0 ? -static_cast<FloatType>(static_cast<std::uintmax_t>(-x))
|
||||
: sprout::math::detail::ceil_impl(x, static_cast<FloatType>(static_cast<std::uintmax_t>(x)))
|
||||
: static_cast<FloatType>(sprout::math::detail::ceil_impl(static_cast<typename sprout::math::detail::float_compute<FloatType>::type>(x)))
|
||||
#endif
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename IntType,
|
||||
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||
|
@ -46,7 +59,7 @@ namespace sprout {
|
|||
}
|
||||
} // namespace detail
|
||||
|
||||
using NS_SPROUT_MATH_DETAIL::ceil;
|
||||
using sprout::math::detail::ceil;
|
||||
} // namespace math
|
||||
|
||||
using sprout::math::ceil;
|
||||
|
|
|
@ -7,35 +7,48 @@
|
|||
#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/equal_to.hpp>
|
||||
#include <sprout/type_traits/enabler_if.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace math {
|
||||
namespace detail {
|
||||
template<typename FloatType>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
floor_impl(FloatType x, FloatType x0) {
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR T
|
||||
floor_impl_1(T x, T x0) {
|
||||
return sprout::math::equal_to(x, x0) ? x0
|
||||
: x0 - 1
|
||||
: x0 - T(1)
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR T
|
||||
floor_impl(T x) {
|
||||
return x < 0 ? sprout::math::detail::floor_impl_1(x, -static_cast<T>(static_cast<std::uintmax_t>(-x)))
|
||||
: static_cast<T>(static_cast<std::uintmax_t>(x))
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename FloatType,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
floor(FloatType x) {
|
||||
return x == 0 ? FloatType(0)
|
||||
return sprout::math::isnan(x) ? x
|
||||
: x == std::numeric_limits<FloatType>::infinity() ? std::numeric_limits<FloatType>::infinity()
|
||||
: x == -std::numeric_limits<FloatType>::infinity() ? -std::numeric_limits<FloatType>::infinity()
|
||||
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
: std::floor(x)
|
||||
#else
|
||||
: x == 0 ? x
|
||||
: std::numeric_limits<std::uintmax_t>::max() < x || std::numeric_limits<std::uintmax_t>::max() < -x
|
||||
? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("floor: large float rounding."), x)
|
||||
: x < 0 ? sprout::math::detail::floor_impl(x, -static_cast<FloatType>(static_cast<std::uintmax_t>(-x)))
|
||||
: static_cast<FloatType>(static_cast<std::uintmax_t>(x))
|
||||
: static_cast<FloatType>(sprout::math::detail::floor_impl(static_cast<typename sprout::math::detail::float_compute<FloatType>::type>(x)))
|
||||
#endif
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename IntType,
|
||||
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||
|
@ -46,7 +59,7 @@ namespace sprout {
|
|||
}
|
||||
} // namespace detail
|
||||
|
||||
using NS_SPROUT_MATH_DETAIL::floor;
|
||||
using sprout::math::detail::floor;
|
||||
} // namespace math
|
||||
|
||||
using sprout::math::floor;
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
#include <stdexcept>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/detail/config.hpp>
|
||||
#include <sprout/math/isnan.hpp>
|
||||
#include <sprout/math/isinf.hpp>
|
||||
#include <sprout/type_traits/enabler_if.hpp>
|
||||
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
# include <sprout/math/ceil.hpp>
|
||||
|
@ -32,7 +34,9 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR To
|
||||
iceil(FloatType x) {
|
||||
return sprout::math::detail::iceil_impl<To>(sprout::ceil(x));
|
||||
return sprout::math::isnan(x) || sprout::math::isinf(x) ? std::numeric_limits<To>::min()
|
||||
: sprout::math::detail::iceil_impl<To>(sprout::math::ceil(x))
|
||||
;
|
||||
}
|
||||
#else
|
||||
template<typename To, typename FloatType>
|
||||
|
@ -49,7 +53,8 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR To
|
||||
iceil(FloatType x) {
|
||||
return x == 0 ? To(0)
|
||||
return sprout::math::isnan(x) || sprout::math::isinf(x) ? std::numeric_limits<To>::min()
|
||||
: x == 0 ? To(0)
|
||||
: std::numeric_limits<To>::max() < x || std::numeric_limits<To>::min() > x
|
||||
? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("iceil: large float rounding."), static_cast<To>(x))
|
||||
: sprout::math::detail::iceil_impl(x, static_cast<To>(x))
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
#include <stdexcept>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/detail/config.hpp>
|
||||
#include <sprout/math/isnan.hpp>
|
||||
#include <sprout/math/isinf.hpp>
|
||||
#include <sprout/type_traits/enabler_if.hpp>
|
||||
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
# include <sprout/math/floor.hpp>
|
||||
|
@ -32,7 +34,9 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR To
|
||||
ifloor(FloatType x) {
|
||||
return sprout::math::detail::ifloor_impl<To>(sprout::floor(x));
|
||||
return sprout::math::isnan(x) || sprout::math::isinf(x) ? std::numeric_limits<To>::min()
|
||||
: sprout::math::detail::ifloor_impl<To>(sprout::math::floor(x))
|
||||
;
|
||||
}
|
||||
#else
|
||||
template<typename To, typename FloatType>
|
||||
|
@ -49,7 +53,8 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR To
|
||||
ifloor(FloatType x) {
|
||||
return x == 0 ? To(0)
|
||||
return sprout::math::isnan(x) || sprout::math::isinf(x) ? std::numeric_limits<To>::min()
|
||||
: x == 0 ? To(0)
|
||||
: std::numeric_limits<To>::max() < x || std::numeric_limits<To>::min() > x
|
||||
? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("ifloor: large float rounding."), static_cast<To>(x))
|
||||
: sprout::math::detail::ifloor_impl(x, static_cast<To>(x))
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
#include <stdexcept>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/detail/config.hpp>
|
||||
#include <sprout/math/isnan.hpp>
|
||||
#include <sprout/math/isinf.hpp>
|
||||
#include <sprout/type_traits/enabler_if.hpp>
|
||||
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
# include <sprout/math/round.hpp>
|
||||
|
@ -32,7 +34,9 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR To
|
||||
iround(FloatType x) {
|
||||
return sprout::math::detail::iround_impl<To>(sprout::round(x));
|
||||
return sprout::math::isnan(x) || sprout::math::isinf(x) ? std::numeric_limits<To>::min()
|
||||
: sprout::math::detail::iround_impl<To>(sprout::math::round(x))
|
||||
;
|
||||
}
|
||||
#else
|
||||
template<typename To, typename FloatType>
|
||||
|
@ -56,7 +60,8 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR To
|
||||
iround(FloatType x) {
|
||||
return x == 0 ? To(0)
|
||||
return sprout::math::isnan(x) || sprout::math::isinf(x) ? std::numeric_limits<To>::min()
|
||||
: x == 0 ? To(0)
|
||||
: std::numeric_limits<To>::max() < x || std::numeric_limits<To>::min() > x
|
||||
? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("iround: large float irounding."), x)
|
||||
: x < 0 ? sprout::math::detail::iround_impl_nagative(x, static_cast<To>(x))
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
#include <stdexcept>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/detail/config.hpp>
|
||||
#include <sprout/math/isnan.hpp>
|
||||
#include <sprout/math/isinf.hpp>
|
||||
#include <sprout/type_traits/enabler_if.hpp>
|
||||
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
# include <sprout/math/trunc.hpp>
|
||||
|
@ -30,7 +32,9 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR To
|
||||
itrunc(FloatType x) {
|
||||
return sprout::math::detail::itrunc_impl<To>(sprout::math::trunc(x));
|
||||
return sprout::math::isnan(x) || sprout::math::isinf(x) ? std::numeric_limits<To>::min()
|
||||
: sprout::math::detail::itrunc_impl<To>(sprout::math::trunc(x))
|
||||
;
|
||||
}
|
||||
#else
|
||||
template<
|
||||
|
@ -40,7 +44,8 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR To
|
||||
itrunc(FloatType x) {
|
||||
return x == 0 ? To(0)
|
||||
return sprout::math::isnan(x) || sprout::math::isinf(x) ? std::numeric_limits<To>::min()
|
||||
: x == 0 ? To(0)
|
||||
: std::numeric_limits<To>::max() < x || std::numeric_limits<To>::min() > x
|
||||
? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("itrunc: large float rounding."), static_cast<To>(x))
|
||||
: static_cast<To>(x)
|
||||
|
|
|
@ -187,13 +187,13 @@ namespace sprout {
|
|||
: x <= 0 && sprout::math::is_integer(x) ? std::numeric_limits<FloatType>::infinity()
|
||||
: x == -std::numeric_limits<FloatType>::infinity() ? std::numeric_limits<FloatType>::infinity()
|
||||
: x == std::numeric_limits<FloatType>::infinity() ? std::numeric_limits<FloatType>::infinity()
|
||||
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
: std::lgamma(x)
|
||||
#else
|
||||
//#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
// : std::lgamma(x)
|
||||
//#else
|
||||
: x == 1 ? FloatType(0)
|
||||
: x == 2 ? FloatType(0)
|
||||
: static_cast<FloatType>(sprout::math::detail::lgamma_impl(static_cast<typename sprout::math::detail::float_compute<FloatType>::type>(x)))
|
||||
#endif
|
||||
//#endif
|
||||
;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
#ifndef SPROUT_MATH_LLROUND_HPP
|
||||
#define SPROUT_MATH_LLROUND_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/iround.hpp>
|
||||
#include <sprout/type_traits/enabler_if.hpp>
|
||||
|
||||
|
@ -16,20 +19,29 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR long long
|
||||
llround(FloatType x) {
|
||||
return sprout::iround<long long>(x);
|
||||
return sprout::math::isnan(x) || sprout::math::isinf(x) ? std::numeric_limits<long long>::min()
|
||||
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
: std::llround(x)
|
||||
#else
|
||||
: sprout::math::iround<long long>(x);
|
||||
#endif
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename IntType,
|
||||
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR long long
|
||||
llround(IntType x) {
|
||||
return sprout::iround<long long>(x);
|
||||
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
return std::llround(x);
|
||||
#else
|
||||
return sprout::math::iround<long long>(x);
|
||||
#endif
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
using NS_SPROUT_MATH_DETAIL::llround;
|
||||
using sprout::math::detail::llround;
|
||||
} // namespace math
|
||||
|
||||
using sprout::math::llround;
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
#ifndef SPROUT_MATH_LROUND_HPP
|
||||
#define SPROUT_MATH_LROUND_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/iround.hpp>
|
||||
#include <sprout/type_traits/enabler_if.hpp>
|
||||
|
||||
|
@ -16,20 +19,29 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR long
|
||||
lround(FloatType x) {
|
||||
return sprout::iround<long>(x);
|
||||
return sprout::math::isnan(x) || sprout::math::isinf(x) ? std::numeric_limits<long>::min()
|
||||
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
: std::lround(x)
|
||||
#else
|
||||
: sprout::math::iround<long>(x);
|
||||
#endif
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename IntType,
|
||||
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR long
|
||||
lround(IntType x) {
|
||||
return sprout::iround<long>(x);
|
||||
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
return std::lround(x);
|
||||
#else
|
||||
return sprout::math::iround<long>(x);
|
||||
#endif
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
using NS_SPROUT_MATH_DETAIL::lround;
|
||||
using sprout::math::detail::lround;
|
||||
} // namespace math
|
||||
|
||||
using sprout::math::lround;
|
||||
|
|
|
@ -24,7 +24,7 @@ namespace sprout {
|
|||
? 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::iround<R>(x / y)
|
||||
: sprout::math::iround<R>(x / y)
|
||||
;
|
||||
}
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ namespace sprout {
|
|||
? 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::round(x / y) * y
|
||||
: x - sprout::math::round(x / y) * y
|
||||
;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,41 +7,54 @@
|
|||
#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/type_traits/enabler_if.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace math {
|
||||
namespace detail {
|
||||
template<typename FloatType>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
round_impl_positive(FloatType x, FloatType x0) {
|
||||
return x - x0 < FloatType(0.5) ? x0
|
||||
: x0 + 1
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR T
|
||||
round_impl_positive(T x, T x0) {
|
||||
return x - x0 < T(0.5) ? x0
|
||||
: x0 + T(1)
|
||||
;
|
||||
}
|
||||
template<typename FloatType>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
round_impl_nagative(FloatType x, FloatType x0) {
|
||||
return x0 - x < FloatType(0.5) ? x0
|
||||
: x0 - 1
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR T
|
||||
round_impl_nagative(T x, T x0) {
|
||||
return x0 - x < T(0.5) ? x0
|
||||
: x0 - T(1)
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR T
|
||||
round_impl(T x) {
|
||||
return x < 0 ? sprout::math::detail::round_impl_nagative(x, -static_cast<T>(static_cast<std::uintmax_t>(-x)))
|
||||
: sprout::math::detail::round_impl_positive(x, static_cast<T>(static_cast<std::uintmax_t>(x)))
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename FloatType,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
round(FloatType x) {
|
||||
return x == 0 ? FloatType(0)
|
||||
return sprout::math::isnan(x) ? x
|
||||
: x == std::numeric_limits<FloatType>::infinity() ? std::numeric_limits<FloatType>::infinity()
|
||||
: x == -std::numeric_limits<FloatType>::infinity() ? -std::numeric_limits<FloatType>::infinity()
|
||||
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
: std::round(x)
|
||||
#else
|
||||
: x == 0 ? x
|
||||
: std::numeric_limits<std::uintmax_t>::max() < x || std::numeric_limits<std::uintmax_t>::max() < -x
|
||||
? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("round: large float rounding."), x)
|
||||
: x < 0 ? sprout::math::detail::round_impl_nagative(x, -static_cast<FloatType>(static_cast<std::uintmax_t>(-x)))
|
||||
: sprout::math::detail::round_impl_positive(x, static_cast<FloatType>(static_cast<std::uintmax_t>(x)))
|
||||
: static_cast<FloatType>(sprout::math::detail::round_impl(static_cast<typename sprout::math::detail::float_compute<FloatType>::type>(x)))
|
||||
#endif
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename IntType,
|
||||
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||
|
@ -52,7 +65,7 @@ namespace sprout {
|
|||
}
|
||||
} // namespace detail
|
||||
|
||||
using NS_SPROUT_MATH_DETAIL::round;
|
||||
using sprout::math::detail::round;
|
||||
} // namespace math
|
||||
|
||||
using sprout::math::round;
|
||||
|
|
|
@ -94,7 +94,7 @@ namespace sprout {
|
|||
return sprout::math::detail::tgamma_impl_1(
|
||||
x,
|
||||
sprout::clamp(
|
||||
sprout::iround(x - T(2)),
|
||||
sprout::math::iround(x - T(2)),
|
||||
-static_cast<int>(sprout::math::factorial_limit<T>()),
|
||||
static_cast<int>(sprout::math::factorial_limit<T>())
|
||||
)
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include <stdexcept>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/detail/config.hpp>
|
||||
#include <sprout/math/isnan.hpp>
|
||||
#include <sprout/type_traits/enabler_if.hpp>
|
||||
|
||||
namespace sprout {
|
||||
|
@ -18,16 +19,20 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
trunc(FloatType x) {
|
||||
return x == 0 ? FloatType(0)
|
||||
return sprout::math::isnan(x) ? x
|
||||
: x == std::numeric_limits<FloatType>::infinity() ? std::numeric_limits<FloatType>::infinity()
|
||||
: x == -std::numeric_limits<FloatType>::infinity() ? -std::numeric_limits<FloatType>::infinity()
|
||||
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
: std::trunc(x)
|
||||
#else
|
||||
: x == 0 ? x
|
||||
: std::numeric_limits<std::uintmax_t>::max() < x || std::numeric_limits<std::uintmax_t>::max() < -x
|
||||
? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::runtime_error("trunc: large float rounding."), x)
|
||||
: x < 0 ? -static_cast<FloatType>(static_cast<std::uintmax_t>(-x))
|
||||
: static_cast<FloatType>(static_cast<std::uintmax_t>(x))
|
||||
#endif
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename IntType,
|
||||
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||
|
|
|
@ -369,7 +369,7 @@ namespace sprout {
|
|||
template<int D, typename Engine, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
||||
SPROUT_CONSTEXPR sprout::random::random_result<Engine, binomial_distribution>
|
||||
generate_4(Engine const& eng, RealType v, RealType u, RealType us) const {
|
||||
return generate_5<D + 1>(eng, v, u, us, static_cast<IntType>(sprout::floor((2 * btrd_.a / us + btrd_.b) * u + btrd_.c)));
|
||||
return generate_5<D + 1>(eng, v, u, us, static_cast<IntType>(sprout::math::floor((2 * btrd_.a / us + btrd_.b) * u + btrd_.c)));
|
||||
}
|
||||
template<int D, typename Engine, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
||||
SPROUT_CONSTEXPR sprout::random::random_result<Engine, binomial_distribution>
|
||||
|
@ -409,7 +409,7 @@ namespace sprout {
|
|||
SPROUT_CONSTEXPR sprout::random::random_result<Engine, binomial_distribution>
|
||||
generate_1_1(Engine const& eng, RealType u) const {
|
||||
return sprout::random::random_result<Engine, binomial_distribution>(
|
||||
static_cast<IntType>(sprout::floor((2 * btrd_.a / (RealType(0.5) - sprout::abs(u)) + btrd_.b) * u + btrd_.c)),
|
||||
static_cast<IntType>(sprout::math::floor((2 * btrd_.a / (RealType(0.5) - sprout::abs(u)) + btrd_.b) * u + btrd_.c)),
|
||||
eng, *this
|
||||
);
|
||||
}
|
||||
|
@ -530,7 +530,7 @@ namespace sprout {
|
|||
template<typename Engine>
|
||||
SPROUT_CONSTEXPR sprout::random::random_result<Engine, binomial_distribution>
|
||||
generate_4(Engine const& eng, RealType v, RealType u, RealType us) const {
|
||||
return generate_5(eng, v, u, us, static_cast<IntType>(sprout::floor((2 * btrd_.a / us + btrd_.b) * u + btrd_.c)));
|
||||
return generate_5(eng, v, u, us, static_cast<IntType>(sprout::math::floor((2 * btrd_.a / us + btrd_.b) * u + btrd_.c)));
|
||||
}
|
||||
template<typename Engine>
|
||||
SPROUT_CONSTEXPR sprout::random::random_result<Engine, binomial_distribution>
|
||||
|
@ -557,7 +557,7 @@ namespace sprout {
|
|||
SPROUT_CONSTEXPR sprout::random::random_result<Engine, binomial_distribution>
|
||||
generate_1_1(Engine const& eng, RealType u) const {
|
||||
return sprout::random::random_result<Engine, binomial_distribution>(
|
||||
static_cast<IntType>(sprout::floor((2 * btrd_.a / (RealType(0.5) - sprout::abs(u)) + btrd_.b) * u + btrd_.c)),
|
||||
static_cast<IntType>(sprout::math::floor((2 * btrd_.a / (RealType(0.5) - sprout::abs(u)) + btrd_.b) * u + btrd_.c)),
|
||||
eng,
|
||||
*this
|
||||
);
|
||||
|
|
|
@ -83,7 +83,7 @@ namespace sprout {
|
|||
template<typename Engine, typename Random>
|
||||
SPROUT_CONSTEXPR sprout::random::random_result<Engine, geometric_distribution> generate_1(Random const& rnd) const {
|
||||
return sprout::random::random_result<Engine, geometric_distribution>(
|
||||
static_cast<IntType>(sprout::floor(sprout::math::log(RealType(1) - rnd.result()) / log_1mp_)),
|
||||
static_cast<IntType>(sprout::math::floor(sprout::math::log(RealType(1) - rnd.result()) / log_1mp_)),
|
||||
rnd.engine(),
|
||||
*this
|
||||
);
|
||||
|
|
|
@ -64,7 +64,7 @@ namespace sprout {
|
|||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>
|
||||
float_to_string(FloatType val, bool negative, int digits) {
|
||||
return sprout::detail::float_to_string_impl<Elem>(
|
||||
val, negative, digits, static_cast<int>((val - sprout::floor(val)) * sprout::detail::int_pow<int>(sprout::detail::decimal_places_length)),
|
||||
val, negative, digits, static_cast<int>((val - sprout::math::floor(val)) * sprout::detail::int_pow<int>(sprout::detail::decimal_places_length)),
|
||||
sprout::make_index_tuple<sprout::printed_float_digits<FloatType>::value - 1>::make()
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue