From 6baf0822343fd9599f7a8c5a0f59c1a56c9f84cb Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Fri, 4 May 2012 18:02:42 +0900 Subject: [PATCH] add sprout::log10, log2, log1p --- sprout/math/constants.hpp | 28 +++++++++++++++++++++++ sprout/math/exponential.hpp | 3 +++ sprout/math/log.hpp | 3 ++- sprout/math/log10.hpp | 45 +++++++++++++++++++++++++++++++++++++ sprout/math/log1p.hpp | 44 ++++++++++++++++++++++++++++++++++++ sprout/math/log2.hpp | 45 +++++++++++++++++++++++++++++++++++++ 6 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 sprout/math/log10.hpp create mode 100644 sprout/math/log1p.hpp create mode 100644 sprout/math/log2.hpp diff --git a/sprout/math/constants.hpp b/sprout/math/constants.hpp index da7de011..64a9298b 100644 --- a/sprout/math/constants.hpp +++ b/sprout/math/constants.hpp @@ -26,6 +26,34 @@ namespace sprout { inline SPROUT_CONSTEXPR T root_two() { return 1.414213562373095048801688724209698078569671875376948073L; } + // + // half_root_two + // + template + inline SPROUT_CONSTEXPR T half_root_two() { + return 0.70710678118654752440084436210484903928483593756084L; + } + // + // e + // + template + inline SPROUT_CONSTEXPR T e() { + return 2.7182818284590452353602874713526624977572470936999595749669676L; + } + // + // ln_ten + // + template + inline SPROUT_CONSTEXPR T ln_ten() { + return 2.302585092994045684017991454684364207601101488628772976L; + } + // + // ln_two + // + template + inline SPROUT_CONSTEXPR T ln_two() { + return 0.693147180559945309417232121458176568075500134360255254L; + } } // namespace math } // namespace sprout diff --git a/sprout/math/exponential.hpp b/sprout/math/exponential.hpp index b441aee3..9cd437de 100644 --- a/sprout/math/exponential.hpp +++ b/sprout/math/exponential.hpp @@ -3,5 +3,8 @@ #include #include +#include +#include +#include #endif // #ifndef SPROUT_MATH_EXPONENTIAL_HPP diff --git a/sprout/math/log.hpp b/sprout/math/log.hpp index a59fb3d9..18225cd0 100644 --- a/sprout/math/log.hpp +++ b/sprout/math/log.hpp @@ -53,7 +53,8 @@ namespace sprout { inline SPROUT_CONSTEXPR FloatType log(FloatType x) { typedef double type; - return !(x > 0) ? -std::numeric_limits::infinity() + return x == 0 ? std::numeric_limits::quiet_NaN() + : !(x > 0) ? -std::numeric_limits::infinity() : static_cast(sprout::math::detail::log_impl(x)) ; } diff --git a/sprout/math/log10.hpp b/sprout/math/log10.hpp new file mode 100644 index 00000000..ebacff58 --- /dev/null +++ b/sprout/math/log10.hpp @@ -0,0 +1,45 @@ +#ifndef SPROUT_MATH_LOG10_HPP +#define SPROUT_MATH_LOG10_HPP + +#include +#include +#include +#include +#include +#if SPROUT_USE_BUILTIN_CMATH_FUNCTION +# include +#endif + +namespace sprout { + namespace math { + namespace detail { + template< + typename FloatType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR FloatType + log10(FloatType x) { + return sprout::math::detail::log(x) / sprout::math::ln_ten(); + } + + template< + typename IntType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR double + log10(IntType x) { + return sprout::math::detail::log10(static_cast(x)); + } + } // namespace detail + +# if SPROUT_USE_BUILTIN_CMATH_FUNCTION + using std::log10; +# else + using sprout::math::detail::log10; +# endif + } // namespace math + + using sprout::math::log10; +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_LOG10_HPP diff --git a/sprout/math/log1p.hpp b/sprout/math/log1p.hpp new file mode 100644 index 00000000..8e1f4a0b --- /dev/null +++ b/sprout/math/log1p.hpp @@ -0,0 +1,44 @@ +#ifndef SPROUT_MATH_LOG1P_HPP +#define SPROUT_MATH_LOG1P_HPP + +#include +#include +#include +#include +#if SPROUT_USE_BUILTIN_CMATH_FUNCTION +# include +#endif + +namespace sprout { + namespace math { + namespace detail { + template< + typename FloatType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR FloatType + log1p(FloatType x) { + return sprout::math::detail::log(1 + x); + } + + template< + typename IntType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR double + log1p(IntType x) { + return sprout::math::detail::log1p(static_cast(x)); + } + } // namespace detail + +# if SPROUT_USE_BUILTIN_CMATH_FUNCTION + using std::log1p; +# else + using sprout::math::detail::log1p; +# endif + } // namespace math + + using sprout::math::log1p; +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_LOG1P_HPP diff --git a/sprout/math/log2.hpp b/sprout/math/log2.hpp new file mode 100644 index 00000000..6e8e385f --- /dev/null +++ b/sprout/math/log2.hpp @@ -0,0 +1,45 @@ +#ifndef SPROUT_MATH_LOG2HPP +#define SPROUT_MATH_LOG2HPP + +#include +#include +#include +#include +#include +#if SPROUT_USE_BUILTIN_CMATH_FUNCTION +# include +#endif + +namespace sprout { + namespace math { + namespace detail { + template< + typename FloatType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR FloatType + log2(FloatType x) { + return sprout::math::detail::log(x) / sprout::math::ln_two(); + } + + template< + typename IntType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR double + log2(IntType x) { + return sprout::math::detail::log2(static_cast(x)); + } + } // namespace detail + +# if SPROUT_USE_BUILTIN_CMATH_FUNCTION + using std::log2; +# else + using sprout::math::detail::log2; +# endif + } // namespace math + + using sprout::math::log2; +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_LOG2HPP