fix math::log implementation

support std::hash
This commit is contained in:
bolero-MURAKAMI 2013-02-07 03:11:17 +09:00
parent 9140b68379
commit 66da9f4fea
20 changed files with 288 additions and 5 deletions

View file

@ -2,6 +2,7 @@
#define SPROUT_ARRAY_HASH_HPP
#include <cstddef>
#include <functional>
#include <sprout/config.hpp>
#include <sprout/array/array.hpp>
#include <sprout/functional/hash.hpp>
@ -17,4 +18,14 @@ namespace sprout {
}
} // namespace sprout
namespace std {
//
// hash
//
template<typename T, std::size_t N>
struct hash<sprout::array<T, N> >
: public sprout::hash<sprout::array<T, N> >
{};
} // namespace std
#endif // #ifndef SPROUT_ARRAY_HASH_HPP

View file

@ -18,4 +18,14 @@ namespace sprout {
}
} // namespace sprout
namespace std {
//
// hash
//
template<std::size_t N>
struct hash<sprout::bitset<N> >
: public sprout::hash<sprout::bitset<N> >
{};
} // namespace std
#endif // #ifndef SPROUT_BITSET_HASH_HPP

View file

@ -17,4 +17,14 @@ namespace sprout {
}
} // namespace sprout
namespace std {
//
// hash
//
template<typename T>
struct hash<sprout::complex<T> >
: public sprout::hash<sprout::complex<T> >
{};
} // namespace std
#endif // #ifndef SPROUT_COMPLEX_HASH_HPP

View file

@ -1,7 +1,11 @@
#ifndef SPROUT_DETAIL_POW_HPP
#define SPROUT_DETAIL_POW_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/type_traits/is_int.hpp>
#include <sprout/type_traits/is_uint.hpp>
#include <sprout/type_traits/enabler_if.hpp>
namespace sprout {
namespace detail {
@ -17,21 +21,35 @@ namespace sprout {
return x * x * x;
}
template<typename T>
template<typename T, typename IntType>
inline SPROUT_CONSTEXPR T
pow_n_impl(T const& x, int n) {
pow_n_impl(T const& x, IntType n) {
return n == 1 ? x
: n % 2 ? x * sprout::detail::pow2(sprout::detail::pow_n_impl(x, n / 2))
: sprout::detail::pow2(sprout::detail::pow_n_impl(x, n / 2))
;
}
template<typename T>
template<
typename T, typename UIntType,
typename sprout::enabler_if<sprout::is_uint<UIntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR T
pow_n(T const& x, int n) {
pow_n(T const& x, UIntType n) {
return n == 0 ? T(1)
: sprout::detail::pow_n_impl(x, n)
;
}
template<
typename T, typename IntType,
typename sprout::enabler_if<sprout::is_int<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR T
pow_n(T const& x, IntType n) {
return n == 0 ? T(1)
: n > 0 ? sprout::detail::pow_n_impl(x, n)
: T(1) / sprout::detail::pow_n_impl(x, -n)
;
}
} // namespace detail
} // namespace sprout

View file

@ -25,4 +25,18 @@ namespace sprout {
} // namespace logic
} // namespace sprout
namespace std {
//
// hash
//
template<>
struct hash<sprout::logic::indeterminate_keyword_t>
: public sprout::hash<sprout::logic::indeterminate_keyword_t>
{};
template<>
struct hash<sprout::logic::tribool>
: public sprout::hash<sprout::logic::tribool>
{};
} // namespace std
#endif // #ifndef SPROUT_LOGIC_TRIBOOL_HASH_HPP

View file

@ -10,5 +10,8 @@
#include <sprout/math/log10.hpp>
#include <sprout/math/log2.hpp>
#include <sprout/math/log1p.hpp>
#include <sprout/math/ldexp.hpp>
#include <sprout/math/scalbn.hpp>
#include <sprout/math/scalbln.hpp>
#endif // #ifndef SPROUT_MATH_EXPONENTIAL_HPP

38
sprout/math/ldexp.hpp Normal file
View file

@ -0,0 +1,38 @@
#ifndef SPROUT_MATH_LDEXP_HPP
#define SPROUT_MATH_LDEXP_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/math/detail/config.hpp>
#include <sprout/detail/pow.hpp>
#include <sprout/type_traits/enabler_if.hpp>
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
ldexp(FloatType x, int exp) {
return static_cast<FloatType>(x * sprout::detail::pow_n(FloatType(2), exp));
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
ldexp(IntType x, int exp) {
return sprout::math::detail::ldexp(static_cast<double>(x), exp);
}
} // namespace detail
using NS_SPROUT_MATH_DETAIL::ldexp;
} // namespace math
using sprout::math::ldexp;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_LDEXP_HPP

View file

@ -42,7 +42,8 @@ namespace sprout {
typedef double type;
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))
: x < 1 ? static_cast<FloatType>(-sprout::math::detail::log_impl(type(1) / static_cast<type>(x)))
: static_cast<FloatType>(sprout::math::detail::log_impl(static_cast<type>(x)))
;
}

39
sprout/math/scalbln.hpp Normal file
View file

@ -0,0 +1,39 @@
#ifndef SPROUT_MATH_SCALBLN_HPP
#define SPROUT_MATH_SCALBLN_HPP
#include <cfloat>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/math/detail/config.hpp>
#include <sprout/detail/pow.hpp>
#include <sprout/type_traits/enabler_if.hpp>
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
scalbln(FloatType x, long exp) {
return static_cast<FloatType>(x * sprout::detail::pow_n(FloatType(FLT_RADIX), exp));
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
scalbln(IntType x, long exp) {
return sprout::math::detail::scalbln(static_cast<double>(x), exp);
}
} // namespace detail
using NS_SPROUT_MATH_DETAIL::scalbln;
} // namespace math
using sprout::math::scalbln;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_SCALBLN_HPP

39
sprout/math/scalbn.hpp Normal file
View file

@ -0,0 +1,39 @@
#ifndef SPROUT_MATH_SCALBN_HPP
#define SPROUT_MATH_SCALBN_HPP
#include <cfloat>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/math/detail/config.hpp>
#include <sprout/detail/pow.hpp>
#include <sprout/type_traits/enabler_if.hpp>
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
scalbn(FloatType x, int exp) {
return static_cast<FloatType>(x * sprout::detail::pow_n(FloatType(FLT_RADIX), exp));
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
scalbn(IntType x, int exp) {
return sprout::math::detail::scalbn(static_cast<double>(x), exp);
}
} // namespace detail
using NS_SPROUT_MATH_DETAIL::scalbn;
} // namespace math
using sprout::math::scalbn;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_SCALBN_HPP

View file

@ -19,4 +19,14 @@ namespace sprout {
}
} // namespace sprout
namespace std {
//
// hash
//
template<typename T>
struct hash<sprout::optional<T> >
: public sprout::hash<sprout::optional<T> >
{};
} // namespace std
#endif // #ifndef SPROUT_OPTIONAL_HASH_HPP

View file

@ -17,4 +17,14 @@ namespace sprout {
}
} // namespace sprout
namespace std {
//
// hash
//
template<typename Container>
struct hash<sprout::pit<Container> >
: public sprout::hash<sprout::pit<Container> >
{};
} // namespace std
#endif // #ifndef SPROUT_PIT_HASH_HPP

View file

@ -17,4 +17,14 @@ namespace sprout {
}
} // namespace sprout
namespace std {
//
// hash
//
template<typename T>
struct hash<sprout::rational<T> >
: public sprout::hash<sprout::rational<T> >
{};
} // namespace std
#endif // SPROUT_RATIONAL_HASH_HPP

View file

@ -17,4 +17,14 @@ namespace sprout {
}
} // namespace sprout
namespace std {
//
// hash
//
template<typename T, std::size_t N, typename Traits>
struct hash<sprout::basic_string<T, N, Traits> >
: public sprout::hash<sprout::basic_string<T, N, Traits> >
{};
} // namespace std
#endif // #ifndef SPROUT_STRING_HASH_HPP

View file

@ -16,4 +16,14 @@ namespace sprout {
}
} // namespace sprout
namespace std {
//
// hash
//
template<typename Container>
struct hash<sprout::sub_array<Container> >
: public sprout::hash<sprout::sub_array<Container> >
{};
} // namespace std
#endif // #ifndef SPROUT_SUB_ARRAY_HASH_HPP

View file

@ -31,4 +31,14 @@ namespace sprout {
} // namespace tuples
} // namespace sprout
namespace std {
//
// hash
//
template<typename... Types>
struct hash<sprout::tuples::tuple<Types...> >
: public sprout::hash<sprout::tuples::tuple<Types...> >
{};
} // namespace std
#endif // #ifndef SPROUT_TUPLE_TUPLE_HASH_HPP

View file

@ -17,4 +17,14 @@ namespace sprout {
}
} // namespace sprout
namespace std {
//
// hash
//
template<typename T1, typename T2>
struct hash<sprout::pair<T1, T2> >
: public sprout::hash<sprout::pair<T1, T2> >
{};
} // namespace std
#endif // #ifndef SPROUT_UTILITY_PAIR_HASH_HPP

View file

@ -25,4 +25,14 @@ namespace sprout {
}
} // namespace sprout
namespace std {
//
// hash
//
template<typename T>
struct hash<sprout::value_holder<T> >
: public sprout::hash<sprout::value_holder<T> >
{};
} // namespace std
#endif // #ifndef SPROUT_UTILITY_VALUE_HOLDER_HASH_HPP

View file

@ -16,4 +16,14 @@ namespace sprout {
}
} // namespace sprout
namespace std {
//
// hash
//
template<>
struct hash<sprout::uuids::uuid>
: public sprout::hash<sprout::uuids::uuid>
{};
} // namespace std
#endif // #ifndef SPROUT_UUID_HASH_HPP

View file

@ -33,4 +33,14 @@ namespace sprout {
}
} // namespace sprout
namespace std {
//
// hash
//
template<typename... Types>
struct hash<sprout::variant<Types...> >
: public sprout::hash<sprout::variant<Types...> >
{};
} // namespace std
#endif // #ifndef SPROUT_VARIANT_HASH_HPP