add sprout::log10, log2, log1p

This commit is contained in:
bolero-MURAKAMI 2012-05-04 18:02:42 +09:00
parent 29a55bf31d
commit 6baf082234
6 changed files with 167 additions and 1 deletions

View file

@ -26,6 +26,34 @@ namespace sprout {
inline SPROUT_CONSTEXPR T root_two() { inline SPROUT_CONSTEXPR T root_two() {
return 1.414213562373095048801688724209698078569671875376948073L; return 1.414213562373095048801688724209698078569671875376948073L;
} }
//
// half_root_two
//
template<typename T>
inline SPROUT_CONSTEXPR T half_root_two() {
return 0.70710678118654752440084436210484903928483593756084L;
}
//
// e
//
template<typename T>
inline SPROUT_CONSTEXPR T e() {
return 2.7182818284590452353602874713526624977572470936999595749669676L;
}
//
// ln_ten
//
template<typename T>
inline SPROUT_CONSTEXPR T ln_ten() {
return 2.302585092994045684017991454684364207601101488628772976L;
}
//
// ln_two
//
template<typename T>
inline SPROUT_CONSTEXPR T ln_two() {
return 0.693147180559945309417232121458176568075500134360255254L;
}
} // namespace math } // namespace math
} // namespace sprout } // namespace sprout

View file

@ -3,5 +3,8 @@
#include <sprout/math/exp.hpp> #include <sprout/math/exp.hpp>
#include <sprout/math/log.hpp> #include <sprout/math/log.hpp>
#include <sprout/math/log10.hpp>
#include <sprout/math/log2.hpp>
#include <sprout/math/log1p.hpp>
#endif // #ifndef SPROUT_MATH_EXPONENTIAL_HPP #endif // #ifndef SPROUT_MATH_EXPONENTIAL_HPP

View file

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

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

@ -0,0 +1,45 @@
#ifndef SPROUT_MATH_LOG10_HPP
#define SPROUT_MATH_LOG10_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/math/log.hpp>
#include <sprout/math/constants.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
log10(FloatType x) {
return sprout::math::detail::log(x) / sprout::math::ln_ten<FloatType>();
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
log10(IntType x) {
return sprout::math::detail::log10(static_cast<double>(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

44
sprout/math/log1p.hpp Normal file
View file

@ -0,0 +1,44 @@
#ifndef SPROUT_MATH_LOG1P_HPP
#define SPROUT_MATH_LOG1P_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/math/log.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
log1p(FloatType x) {
return sprout::math::detail::log(1 + x);
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
log1p(IntType x) {
return sprout::math::detail::log1p(static_cast<double>(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

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

@ -0,0 +1,45 @@
#ifndef SPROUT_MATH_LOG2HPP
#define SPROUT_MATH_LOG2HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/math/log.hpp>
#include <sprout/math/constants.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
log2(FloatType x) {
return sprout::math::detail::log(x) / sprout::math::ln_two<FloatType>();
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
log2(IntType x) {
return sprout::math::detail::log2(static_cast<double>(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