add sprout::exp, log

This commit is contained in:
bolero-MURAKAMI 2012-05-04 16:40:25 +09:00
parent d7c441ad69
commit 29a55bf31d
8 changed files with 163 additions and 3 deletions

View file

@ -19,6 +19,13 @@ namespace sprout {
inline SPROUT_CONSTEXPR T pi_div_two() {
return 1.570796326794896619231321691639751442098584699687552910487472L;
}
//
// root_two
//
template<typename T>
inline SPROUT_CONSTEXPR T root_two() {
return 1.414213562373095048801688724209698078569671875376948073L;
}
} // namespace math
} // namespace sprout

View file

@ -19,7 +19,7 @@ namespace sprout {
return 2 * n > sprout::math::factorial_limit<T>() ? tmp
: sprout::math::detail::cos_impl(
x,
tmp + (n % 2 ? -1 : 1) / sprout::math::factorial<T>(2 * n) * x2n,
tmp + (n % 2 ? -1 : 1) * x2n / sprout::math::factorial<T>(2 * n),
n + 1,
x2n * x * x
)

View file

@ -19,7 +19,7 @@ namespace sprout {
return 2 * n > sprout::math::factorial_limit<T>() ? tmp
: sprout::math::detail::cosh_impl(
x,
tmp + 1 / sprout::math::factorial<T>(2 * n) * x2n,
tmp + x2n / sprout::math::factorial<T>(2 * n),
n + 1,
x2n * x * x
)

64
sprout/math/exp.hpp Normal file
View file

@ -0,0 +1,64 @@
#ifndef SPROUT_MATH_EXP_HPP
#define SPROUT_MATH_EXP_HPP
#include <cstddef>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/math/factorial.hpp>
#include <sprout/utility/enabler_if.hpp>
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
# include <cmath>
#endif
namespace sprout {
namespace math {
namespace detail {
template<typename T>
inline SPROUT_CONSTEXPR T
exp_impl(T x, T tmp, std::size_t n, T xn) {
return n > sprout::math::factorial_limit<T>() ? tmp
: sprout::math::detail::exp_impl(
x,
tmp + xn / sprout::math::factorial<T>(n),
n + 1,
xn * x
)
;
}
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
exp(FloatType x) {
typedef double type;
return static_cast<FloatType>(sprout::math::detail::exp_impl(
static_cast<type>(x),
type(1),
1,
static_cast<type>(x)
));
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
exp(IntType x) {
return sprout::math::detail::exp(static_cast<double>(x));
}
} // namespace detail
# if SPROUT_USE_BUILTIN_CMATH_FUNCTION
using std::exp;
# else
using sprout::math::detail::exp;
# endif
} // namespace math
using sprout::math::exp;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_EXP_HPP

View file

@ -0,0 +1,7 @@
#ifndef SPROUT_MATH_EXPONENTIAL_HPP
#define SPROUT_MATH_EXPONENTIAL_HPP
#include <sprout/math/exp.hpp>
#include <sprout/math/log.hpp>
#endif // #ifndef SPROUT_MATH_EXPONENTIAL_HPP

View file

@ -2,6 +2,7 @@
#define SPROUT_MATH_FUNCTIONS_HPP
#include <sprout/config.hpp>
#include <sprout/math/exponential.hpp>
#include <sprout/math/power.hpp>
#include <sprout/math/trigonometric.hpp>
#include <sprout/math/hyperbolic.hpp>

81
sprout/math/log.hpp Normal file
View file

@ -0,0 +1,81 @@
#ifndef SPROUT_MATH_LOG_HPP
#define SPROUT_MATH_LOG_HPP
#include <cstddef>
#include <limits>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/math/constants.hpp>
#include <sprout/math/factorial.hpp>
#include <sprout/math/sqrt.hpp>
#include <sprout/utility/enabler_if.hpp>
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
# include <cmath>
#endif
namespace sprout {
namespace math {
namespace detail {
template<typename T>
inline SPROUT_CONSTEXPR T
log_impl_2(T x, T tmp, std::size_t n, T xn) {
return n > sprout::math::factorial_limit<T>() ? tmp
: sprout::math::detail::log_impl_2(
x,
tmp + (n % 2 ? 1 : -1) * xn / n,
n + 1,
xn * x
)
;
}
template<typename T>
inline SPROUT_CONSTEXPR T
log_impl_1(T x) {
return sprout::math::detail::log_impl_2(
x,
x,
2,
x * x
);
}
template<typename T>
inline SPROUT_CONSTEXPR T
log_impl(T x) {
return !(x > sprout::math::root_two<T>()) ? sprout::math::detail::log_impl_1(x - 1)
: 2 * sprout::math::detail::log_impl(sprout::math::detail::sqrt(x))
;
}
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
log(FloatType x) {
typedef double type;
return !(x > 0) ? -std::numeric_limits<FloatType>::infinity()
: static_cast<FloatType>(sprout::math::detail::log_impl(x))
;
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
log(IntType x) {
return sprout::math::detail::log(static_cast<double>(x));
}
} // namespace detail
# if SPROUT_USE_BUILTIN_CMATH_FUNCTION
using std::log;
# else
using sprout::math::detail::log;
# endif
} // namespace math
using sprout::math::log;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_LOG_HPP

View file

@ -19,7 +19,7 @@ namespace sprout {
return 2 * n + 1 > sprout::math::factorial_limit<T>() ? tmp
: sprout::math::detail::sinh_impl(
x,
tmp + 1 / sprout::math::factorial<T>(2 * n + 1) * x2n1,
tmp + x2n1 / sprout::math::factorial<T>(2 * n + 1),
n + 1,
x2n1 * x * x
)