fix math::exp implementation

This commit is contained in:
bolero-MURAKAMI 2013-02-07 03:32:05 +09:00
parent 66da9f4fea
commit 4b3d4e82d7
2 changed files with 12 additions and 10 deletions

View file

@ -14,13 +14,18 @@ namespace sprout {
namespace detail { namespace detail {
template<typename T> template<typename T>
inline SPROUT_CONSTEXPR T inline SPROUT_CONSTEXPR T
exp_impl(T x, std::size_t n, std::size_t last) { exp_impl_1(T x, std::size_t n, std::size_t last) {
return last - n == 1 return last - n == 1
? sprout::detail::pow_n(x, n) / sprout::math::factorial<T>(n) ? sprout::detail::pow_n(x, n) / sprout::math::factorial<T>(n)
: sprout::math::detail::exp_impl(x, n, n + (last - n) / 2) : sprout::math::detail::exp_impl_1(x, n, n + (last - n) / 2)
+ sprout::math::detail::exp_impl(x, n + (last - n) / 2, last) + sprout::math::detail::exp_impl_1(x, n + (last - n) / 2, last)
; ;
} }
template<typename T>
inline SPROUT_CONSTEXPR T
exp_impl(T x) {
return 1 + sprout::math::detail::exp_impl_1(x, 1, sprout::math::factorial_limit<T>() + 1);
}
template< template<
typename FloatType, typename FloatType,
@ -29,12 +34,9 @@ namespace sprout {
inline SPROUT_CONSTEXPR FloatType inline SPROUT_CONSTEXPR FloatType
exp(FloatType x) { exp(FloatType x) {
typedef double type; typedef double type;
return static_cast<FloatType>( return !(x > -1) ? 1 / sprout::math::detail::exp_impl(-static_cast<type>(x))
type(1) + sprout::math::detail::exp_impl( : sprout::math::detail::exp_impl(static_cast<type>(x))
static_cast<type>(x), ;
1, sprout::math::factorial_limit<type>() + 1
)
);
} }
template< template<

View file

@ -42,7 +42,7 @@ namespace sprout {
typedef double type; typedef double type;
return x == 0 ? std::numeric_limits<FloatType>::quiet_NaN() return x == 0 ? std::numeric_limits<FloatType>::quiet_NaN()
: !(x > 0) ? -std::numeric_limits<FloatType>::infinity() : !(x > 0) ? -std::numeric_limits<FloatType>::infinity()
: x < 1 ? static_cast<FloatType>(-sprout::math::detail::log_impl(type(1) / static_cast<type>(x))) : x < 1 ? static_cast<FloatType>(-sprout::math::detail::log_impl(1 / static_cast<type>(x)))
: static_cast<FloatType>(sprout::math::detail::log_impl(static_cast<type>(x))) : static_cast<FloatType>(sprout::math::detail::log_impl(static_cast<type>(x)))
; ;
} }