add sprout::sinh, cosh, tanh, sqrt

This commit is contained in:
bolero-MURAKAMI 2012-05-04 14:07:36 +09:00
parent 234757af07
commit d7c441ad69
8 changed files with 269 additions and 1 deletions

View file

@ -2,6 +2,6 @@
#define SPROUT_CMATH_HPP #define SPROUT_CMATH_HPP
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/math/trigonometric.hpp> #include <sprout/math/functions.hpp>
#endif // #ifndef SPROUT_CMATH_HPP #endif // #ifndef SPROUT_CMATH_HPP

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

@ -0,0 +1,64 @@
#ifndef SPROUT_MATH_COSH_HPP
#define SPROUT_MATH_COSH_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
cosh_impl(T x, T tmp, std::size_t n, T x2n) {
return 2 * n > sprout::math::factorial_limit<T>() ? tmp
: sprout::math::detail::cosh_impl(
x,
tmp + 1 / sprout::math::factorial<T>(2 * n) * x2n,
n + 1,
x2n * x * x
)
;
}
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
cosh(FloatType x) {
typedef double type;
return static_cast<FloatType>(sprout::math::detail::cosh_impl(
static_cast<type>(x),
type(1),
1,
static_cast<type>(x) * static_cast<type>(x)
));
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
cosh(IntType x) {
return sprout::math::detail::cosh(static_cast<double>(x));
}
} // namespace detail
# if SPROUT_USE_BUILTIN_CMATH_FUNCTION
using std::cosh;
# else
using sprout::math::detail::cosh;
# endif
} // namespace math
using sprout::math::cosh;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_COSH_HPP

View file

@ -0,0 +1,9 @@
#ifndef SPROUT_MATH_FUNCTIONS_HPP
#define SPROUT_MATH_FUNCTIONS_HPP
#include <sprout/config.hpp>
#include <sprout/math/power.hpp>
#include <sprout/math/trigonometric.hpp>
#include <sprout/math/hyperbolic.hpp>
#endif // #ifndef SPROUT_MATH_FUNCTIONS_HPP

View file

@ -0,0 +1,8 @@
#ifndef SPROUT_MATH_HYPERBOLIC_HPP
#define SPROUT_MATH_HYPERBOLIC_HPP
#include <sprout/math/sinh.hpp>
#include <sprout/math/cosh.hpp>
#include <sprout/math/tanh.hpp>
#endif // #ifndef SPROUT_MATH_HYPERBOLIC_HPP

6
sprout/math/power.hpp Normal file
View file

@ -0,0 +1,6 @@
#ifndef SPROUT_MATH_POWER_HPP
#define SPROUT_MATH_POWER_HPP
#include <sprout/math/sqrt.hpp>
#endif // #ifndef SPROUT_MATH_POWER_HPP

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

@ -0,0 +1,64 @@
#ifndef SPROUT_MATH_SINH_HPP
#define SPROUT_MATH_SINH_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
sinh_impl(T x, T tmp, std::size_t n, T x2n1) {
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,
n + 1,
x2n1 * x * x
)
;
}
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
sinh(FloatType x) {
typedef double type;
return static_cast<FloatType>(sprout::math::detail::sinh_impl(
static_cast<type>(x),
static_cast<type>(x),
1,
static_cast<type>(x) * static_cast<type>(x) * static_cast<type>(x)
));
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
sinh(IntType x) {
return sprout::math::detail::sinh(static_cast<double>(x));
}
} // namespace detail
# if SPROUT_USE_BUILTIN_CMATH_FUNCTION
using std::sinh;
# else
using sprout::math::detail::sinh;
# endif
} // namespace math
using sprout::math::sinh;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_SINH_HPP

72
sprout/math/sqrt.hpp Normal file
View file

@ -0,0 +1,72 @@
#ifndef SPROUT_MATH_SQRT_HPP
#define SPROUT_MATH_SQRT_HPP
#include <limits>
#include <type_traits>
#include <sprout/config.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
sqrt_impl_1(T x, T s, T s2) {
return !(s < s2) ? s2
: sprout::math::detail::sqrt_impl_1(
x,
(x / s + s) / 2,
s
)
;
}
template<typename T>
inline SPROUT_CONSTEXPR T
sqrt_impl(T x, T s) {
return sprout::math::detail::sqrt_impl_1(
x,
(x / s + s) / 2,
s
)
;
}
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
sqrt(FloatType x) {
typedef double type;
return x < 0 ? std::numeric_limits<FloatType>::quiet_NaN()
: x == 0 ? type(0)
: static_cast<FloatType>(sprout::math::detail::sqrt_impl(
static_cast<type>(x),
x > 1 ? static_cast<type>(x) : type(1)
));
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
sqrt(IntType x) {
return sprout::math::detail::sqrt(static_cast<double>(x));
}
} // namespace detail
# if SPROUT_USE_BUILTIN_CMATH_FUNCTION
using std::sqrt;
# else
using sprout::math::detail::sqrt;
# endif
} // namespace math
using sprout::math::sqrt;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_SQRT_HPP

45
sprout/math/tanh.hpp Normal file
View file

@ -0,0 +1,45 @@
#ifndef SPROUT_MATH_TANH_HPP
#define SPROUT_MATH_TANH_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/math/sinh.hpp>
#include <sprout/math/cosh.hpp>
#include <sprout/utility/enabler_if.hpp>
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
# include <cmath>
#endif
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
tanh(FloatType x) {
return sprout::math::detail::sinh(x) / sprout::math::detail::cosh(x);
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
tanh(IntType x) {
return sprout::math::detail::tanh(static_cast<double>(x));
}
} // namespace detail
# if SPROUT_USE_BUILTIN_CMATH_FUNCTION
using std::tanh;
# else
using sprout::math::detail::tanh;
# endif
} // namespace math
using sprout::math::tanh;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_TANH_HPP