mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-12-23 21:25:49 +00:00
add math functions: logb, ilogb
This commit is contained in:
parent
4b3d4e82d7
commit
d05b597e80
13 changed files with 218 additions and 11 deletions
|
@ -6,6 +6,7 @@
|
|||
#include <sprout/math/trigonometric.hpp>
|
||||
#include <sprout/math/hyperbolic.hpp>
|
||||
#include <sprout/math/exponential.hpp>
|
||||
#include <sprout/math/floating_point.hpp>
|
||||
#include <sprout/math/power.hpp>
|
||||
#include <sprout/math/operations.hpp>
|
||||
#include <sprout/math/nearest.hpp>
|
||||
|
|
|
@ -34,8 +34,8 @@ namespace sprout {
|
|||
inline SPROUT_CONSTEXPR FloatType
|
||||
exp(FloatType x) {
|
||||
typedef double type;
|
||||
return !(x > -1) ? 1 / sprout::math::detail::exp_impl(-static_cast<type>(x))
|
||||
: sprout::math::detail::exp_impl(static_cast<type>(x))
|
||||
return !(x > -1) ? static_cast<FloatType>(1 / sprout::math::detail::exp_impl(-static_cast<type>(x)))
|
||||
: static_cast<FloatType>(sprout::math::detail::exp_impl(static_cast<type>(x)))
|
||||
;
|
||||
}
|
||||
|
||||
|
|
|
@ -10,8 +10,5 @@
|
|||
#include <sprout/math/log10.hpp>
|
||||
#include <sprout/math/log2.hpp>
|
||||
#include <sprout/math/log1p.hpp>
|
||||
#include <sprout/math/ldexp.hpp>
|
||||
#include <sprout/math/scalbn.hpp>
|
||||
#include <sprout/math/scalbln.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_MATH_EXPONENTIAL_HPP
|
||||
|
|
11
sprout/math/floating_point.hpp
Normal file
11
sprout/math/floating_point.hpp
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef SPROUT_MATH_FLOATING_POINT_HPP
|
||||
#define SPROUT_MATH_FLOATING_POINT_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/ldexp.hpp>
|
||||
#include <sprout/math/scalbn.hpp>
|
||||
#include <sprout/math/scalbln.hpp>
|
||||
#include <sprout/math/ilogb.hpp>
|
||||
#include <sprout/math/logb.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_MATH_FLOATING_POINT_HPP
|
|
@ -6,6 +6,7 @@
|
|||
#include <sprout/math/trigonometric.hpp>
|
||||
#include <sprout/math/hyperbolic.hpp>
|
||||
#include <sprout/math/exponential.hpp>
|
||||
#include <sprout/math/floating_point.hpp>
|
||||
#include <sprout/math/power.hpp>
|
||||
#include <sprout/math/operations.hpp>
|
||||
#include <sprout/math/nearest.hpp>
|
||||
|
|
47
sprout/math/ilogb.hpp
Normal file
47
sprout/math/ilogb.hpp
Normal file
|
@ -0,0 +1,47 @@
|
|||
#ifndef SPROUT_MATH_ILOGB_HPP
|
||||
#define SPROUT_MATH_ILOGB_HPP
|
||||
|
||||
#include <climits>
|
||||
#include <cfloat>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/detail/config.hpp>
|
||||
#include <sprout/math/logb.hpp>
|
||||
#include <sprout/math/isnan.hpp>
|
||||
#include <sprout/math/isinf.hpp>
|
||||
#include <sprout/math/iszero.hpp>
|
||||
#include <sprout/type_traits/enabler_if.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace math {
|
||||
namespace detail {
|
||||
template<
|
||||
typename FloatType,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR int
|
||||
ilogb(FloatType x) {
|
||||
return sprout::math::iszero(x) ? FP_ILOGB0
|
||||
: sprout::math::isinf(x) ? INT_MAX
|
||||
: sprout::math::isnan(x) ? FP_ILOGBNAN
|
||||
: static_cast<int>(sprout::math::logb(x))
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename IntType,
|
||||
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR int
|
||||
ilogb(IntType x) {
|
||||
return sprout::math::detail::ilogb(static_cast<double>(x));
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
using NS_SPROUT_MATH_DETAIL::ilogb;
|
||||
} // namespace math
|
||||
|
||||
using sprout::math::ilogb;
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_MATH_ILOGB_HPP
|
|
@ -16,7 +16,7 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
ldexp(FloatType x, int exp) {
|
||||
return static_cast<FloatType>(x * sprout::detail::pow_n(FloatType(2), exp));
|
||||
return x * sprout::detail::pow_n(FloatType(2), exp);
|
||||
}
|
||||
|
||||
template<
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#ifndef SPROUT_MATH_LOG2HPP
|
||||
#define SPROUT_MATH_LOG2HPP
|
||||
#ifndef SPROUT_MATH_LOG2_HPP
|
||||
#define SPROUT_MATH_LOG2_HPP
|
||||
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
|
@ -36,4 +36,4 @@ namespace sprout {
|
|||
using sprout::math::log2;
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_MATH_LOG2HPP
|
||||
#endif // #ifndef SPROUT_MATH_LOG2_HPP
|
||||
|
|
47
sprout/math/log_a.hpp
Normal file
47
sprout/math/log_a.hpp
Normal file
|
@ -0,0 +1,47 @@
|
|||
#ifndef SPROUT_MATH_LOG_A_HPP
|
||||
#define SPROUT_MATH_LOG_A_HPP
|
||||
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/detail/config.hpp>
|
||||
#include <sprout/math/log.hpp>
|
||||
#include <sprout/math/constants.hpp>
|
||||
#include <sprout/type_traits/float_promote.hpp>
|
||||
#include <sprout/type_traits/enabler_if.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace math {
|
||||
namespace detail {
|
||||
template<
|
||||
typename FloatType,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
log_a(FloatType x, FloatType y) {
|
||||
return x == 2 ? sprout::math::log(y) / sprout::math::ln_two<FloatType>()
|
||||
: x == 10 ? sprout::math::log(y) / sprout::math::ln_ten<FloatType>()
|
||||
: sprout::math::log(y) / sprout::math::log(x)
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename ArithmeticType1,
|
||||
typename ArithmeticType2,
|
||||
typename sprout::enabler_if<
|
||||
std::is_arithmetic<ArithmeticType1>::value && std::is_arithmetic<ArithmeticType2>::value
|
||||
>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR typename sprout::float_promote<ArithmeticType1, ArithmeticType2>::type
|
||||
log_a(ArithmeticType1 x, ArithmeticType2 y) {
|
||||
typedef typename sprout::float_promote<ArithmeticType1, ArithmeticType2>::type type;
|
||||
return sprout::math::detail::log_a(static_cast<type>(x), static_cast<type>(y));
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
using sprout::math::detail::log_a;
|
||||
} // namespace math
|
||||
|
||||
using sprout::math::log_a;
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_MATH_LOG_A_HPP
|
102
sprout/math/logb.hpp
Normal file
102
sprout/math/logb.hpp
Normal file
|
@ -0,0 +1,102 @@
|
|||
#ifndef SPROUT_MATH_LOGB_HPP
|
||||
#define SPROUT_MATH_LOGB_HPP
|
||||
|
||||
#include <climits>
|
||||
#include <cfloat>
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
#include <stdexcept>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/detail/config.hpp>
|
||||
#include <sprout/detail/pow.hpp>
|
||||
#include <sprout/math/log_a.hpp>
|
||||
#include <sprout/type_traits/enabler_if.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace math {
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR T
|
||||
logb_impl_2_neg_lo(T x, T x0, T base, T exp) {
|
||||
return base < 1 ? sprout::math::detail::logb_impl_2_neg_lo(x, x0 * FLT_RADIX, x / (x0 / FLT_RADIX), exp - 1)
|
||||
: exp
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR T
|
||||
logb_impl_2_neg_hi(T x, T x0, T base, T exp) {
|
||||
return !(base < FLT_RADIX) ? sprout::math::detail::logb_impl_2_neg_hi(x, x0 / FLT_RADIX, x / (x0 * FLT_RADIX), exp + 1)
|
||||
: exp
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR T
|
||||
logb_impl_2_pos_lo(T x, T x0, T base, T exp) {
|
||||
return base < 1 ? sprout::math::detail::logb_impl_2_pos_lo(x, x0 * FLT_RADIX, x / (x0 / FLT_RADIX), exp + 1)
|
||||
: exp
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR T
|
||||
logb_impl_2_pos_hi(T x, T x0, T base, T exp) {
|
||||
return !(base < FLT_RADIX) ? sprout::math::detail::logb_impl_2_pos_hi(x, x0 / FLT_RADIX, x / (x0 * FLT_RADIX), exp - 1)
|
||||
: exp
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR T
|
||||
logb_impl_2(T x, T x0, T base, T exp) {
|
||||
return exp < 0
|
||||
? base < 1 ? sprout::math::detail::logb_impl_2_neg_lo(x, x0 * FLT_RADIX, x / (x0 / FLT_RADIX), exp - 1)
|
||||
: !(base < FLT_RADIX) ? sprout::math::detail::logb_impl_2_neg_hi(x, x0 / FLT_RADIX, x / (x0 * FLT_RADIX), exp + 1)
|
||||
: exp
|
||||
: base < 1 ? sprout::math::detail::logb_impl_2_pos_lo(x, x0 * FLT_RADIX, x / (x0 / FLT_RADIX), exp + 1)
|
||||
: !(base < FLT_RADIX) ? sprout::math::detail::logb_impl_2_pos_hi(x, x0 / FLT_RADIX, x / (x0 * FLT_RADIX), exp - 1)
|
||||
: exp
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR T
|
||||
logb_impl_1(T x, T x0, T exp) {
|
||||
return sprout::math::detail::logb_impl_2(x, x0, x / x0, exp);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR T
|
||||
logb_impl(T x, T exp) {
|
||||
return std::numeric_limits<std::intmax_t>::max() < exp || std::numeric_limits<std::intmax_t>::min() > exp
|
||||
? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::domain_error("trunc: large float rounding."), exp)
|
||||
: sprout::math::detail::logb_impl_1(
|
||||
x, sprout::detail::pow_n(T(FLT_RADIX), static_cast<std::intmax_t>(exp)),
|
||||
static_cast<T>(static_cast<std::intmax_t>(exp))
|
||||
)
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename FloatType,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
logb(FloatType x) {
|
||||
return x < 0 ? sprout::math::detail::logb_impl(-x, sprout::math::trunc(sprout::math::log_a(FloatType(FLT_RADIX), -x)))
|
||||
: sprout::math::detail::logb_impl(x, sprout::math::trunc(sprout::math::log_a(FloatType(FLT_RADIX), x)))
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
typename IntType,
|
||||
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR double
|
||||
logb(IntType x) {
|
||||
return sprout::math::detail::logb(static_cast<double>(x));
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
using NS_SPROUT_MATH_DETAIL::logb;
|
||||
} // namespace math
|
||||
|
||||
using sprout::math::logb;
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_MATH_LOGB_HPP
|
|
@ -5,6 +5,7 @@
|
|||
#include <sprout/math/sqrt.hpp>
|
||||
#include <sprout/math/cbrt.hpp>
|
||||
#include <sprout/math/pow.hpp>
|
||||
#include <sprout/math/log_a.hpp>
|
||||
#include <sprout/math/hypot.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_MATH_POWER_HPP
|
||||
|
|
|
@ -17,7 +17,7 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
scalbln(FloatType x, long exp) {
|
||||
return static_cast<FloatType>(x * sprout::detail::pow_n(FloatType(FLT_RADIX), exp));
|
||||
return x * sprout::detail::pow_n(FloatType(FLT_RADIX), exp);
|
||||
}
|
||||
|
||||
template<
|
||||
|
|
|
@ -17,7 +17,7 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
scalbn(FloatType x, int exp) {
|
||||
return static_cast<FloatType>(x * sprout::detail::pow_n(FloatType(FLT_RADIX), exp));
|
||||
return x * sprout::detail::pow_n(FloatType(FLT_RADIX), exp);
|
||||
}
|
||||
|
||||
template<
|
||||
|
|
Loading…
Reference in a new issue