From 4b3d4e82d7ba92a854a132b3d03735482c99ab90 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Thu, 7 Feb 2013 03:32:05 +0900 Subject: [PATCH] fix math::exp implementation --- sprout/math/exp.hpp | 20 +++++++++++--------- sprout/math/log.hpp | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/sprout/math/exp.hpp b/sprout/math/exp.hpp index 148f41e3..7d8cf21f 100644 --- a/sprout/math/exp.hpp +++ b/sprout/math/exp.hpp @@ -14,13 +14,18 @@ namespace sprout { namespace detail { template 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 ? sprout::detail::pow_n(x, n) / sprout::math::factorial(n) - : sprout::math::detail::exp_impl(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, n + (last - n) / 2) + + sprout::math::detail::exp_impl_1(x, n + (last - n) / 2, last) ; } + template + inline SPROUT_CONSTEXPR T + exp_impl(T x) { + return 1 + sprout::math::detail::exp_impl_1(x, 1, sprout::math::factorial_limit() + 1); + } template< typename FloatType, @@ -29,12 +34,9 @@ namespace sprout { inline SPROUT_CONSTEXPR FloatType exp(FloatType x) { typedef double type; - return static_cast( - type(1) + sprout::math::detail::exp_impl( - static_cast(x), - 1, sprout::math::factorial_limit() + 1 - ) - ); + return !(x > -1) ? 1 / sprout::math::detail::exp_impl(-static_cast(x)) + : sprout::math::detail::exp_impl(static_cast(x)) + ; } template< diff --git a/sprout/math/log.hpp b/sprout/math/log.hpp index ee2969b7..b265e7e2 100644 --- a/sprout/math/log.hpp +++ b/sprout/math/log.hpp @@ -42,7 +42,7 @@ namespace sprout { typedef double type; return x == 0 ? std::numeric_limits::quiet_NaN() : !(x > 0) ? -std::numeric_limits::infinity() - : x < 1 ? static_cast(-sprout::math::detail::log_impl(type(1) / static_cast(x))) + : x < 1 ? static_cast(-sprout::math::detail::log_impl(1 / static_cast(x))) : static_cast(sprout::math::detail::log_impl(static_cast(x))) ; }