1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00

factorial 修正

This commit is contained in:
bolero-MURAKAMI 2011-10-06 12:54:15 +09:00
parent 5ab8fbfe7a
commit 9297b36d0b

View file

@ -19,15 +19,6 @@ namespace sprout {
SPROUT_STATIC_CONSTEXPR std::size_t limit = 5;
public:
typedef sprout::array<type, limit + 1> table_type;
public:
SPROUT_STATIC_CONSTEXPR table_type table = table_type{{
1,
1,
2,
6,
24,
120
}};
};
template<>
struct factorials<std::uint8_t> {
@ -37,15 +28,6 @@ namespace sprout {
SPROUT_STATIC_CONSTEXPR std::size_t limit = 5;
public:
typedef sprout::array<type, limit + 1> table_type;
public:
SPROUT_STATIC_CONSTEXPR table_type table = table_type{{
1,
1,
2,
6,
24,
120
}};
};
template<>
struct factorials<std::int16_t> {
@ -55,17 +37,6 @@ namespace sprout {
SPROUT_STATIC_CONSTEXPR std::size_t limit = 7;
public:
typedef sprout::array<type, limit + 1> table_type;
public:
SPROUT_STATIC_CONSTEXPR table_type table = table_type{{
1,
1,
2,
6,
24,
120,
720,
5040
}};
};
template<>
struct factorials<std::uint16_t> {
@ -75,8 +46,97 @@ namespace sprout {
SPROUT_STATIC_CONSTEXPR std::size_t limit = 8;
public:
typedef sprout::array<type, limit + 1> table_type;
};
template<>
struct factorials<std::int32_t> {
public:
SPROUT_STATIC_CONSTEXPR table_type table = table_type{{
typedef std::int32_t type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 11;
public:
typedef sprout::array<type, limit + 1> table_type;
};
template<>
struct factorials<std::uint32_t> {
public:
typedef std::uint32_t type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 11;
public:
typedef sprout::array<type, limit + 1> table_type;
};
template<>
struct factorials<std::int64_t> {
public:
typedef std::int64_t type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 20;
public:
typedef sprout::array<type, limit + 1> table_type;
};
template<>
struct factorials<std::uint64_t> {
public:
typedef std::uint64_t type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 20;
public:
typedef sprout::array<type, limit + 1> table_type;
};
template<>
struct factorials<float> {
public:
typedef float type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 34;
public:
typedef sprout::array<type, limit + 1> table_type;
};
template<>
struct factorials<double> {
public:
typedef double type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 170;
public:
typedef sprout::array<type, limit + 1> table_type;
};
template<>
struct factorials<long double> {
public:
typedef long double type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 170;
public:
typedef sprout::array<type, limit + 1> table_type;
};
SPROUT_STATIC_CONSTEXPR factorials<std::int8_t>::table_type factorial_table_i8 = factorials<std::int8_t>::table_type{{
1,
1,
2,
6,
24,
120
}};
SPROUT_STATIC_CONSTEXPR factorials<std::uint8_t>::table_type factorial_table_u8 = factorials<std::uint8_t>::table_type{{
1,
1,
2,
6,
24,
120
}};
SPROUT_STATIC_CONSTEXPR factorials<std::int16_t>::table_type factorial_table_i16 = factorials<std::int16_t>::table_type{{
1,
1,
2,
6,
24,
120,
720,
5040
}};
SPROUT_STATIC_CONSTEXPR factorials<std::uint16_t>::table_type factorial_table_u16 = factorials<std::uint16_t>::table_type{{
1,
1,
2,
@ -87,17 +147,7 @@ namespace sprout {
5040,
40320
}};
};
template<>
struct factorials<std::int32_t> {
public:
typedef std::int32_t type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 11;
public:
typedef sprout::array<type, limit + 1> table_type;
public:
SPROUT_STATIC_CONSTEXPR table_type table = table_type{{
SPROUT_STATIC_CONSTEXPR factorials<std::int32_t>::table_type factorial_table_i32 = factorials<std::int32_t>::table_type{{
1,
1,
2,
@ -111,17 +161,7 @@ namespace sprout {
3628800,
39916800
}};
};
template<>
struct factorials<std::uint32_t> {
public:
typedef std::uint32_t type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 11;
public:
typedef sprout::array<type, limit + 1> table_type;
public:
SPROUT_STATIC_CONSTEXPR table_type table = table_type{{
SPROUT_STATIC_CONSTEXPR factorials<std::uint32_t>::table_type factorial_table_u32 = factorials<std::uint32_t>::table_type{{
1,
1,
2,
@ -135,17 +175,7 @@ namespace sprout {
3628800,
39916800
}};
};
template<>
struct factorials<std::int64_t> {
public:
typedef std::int64_t type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 20;
public:
typedef sprout::array<type, limit + 1> table_type;
public:
SPROUT_STATIC_CONSTEXPR table_type table = table_type{{
SPROUT_STATIC_CONSTEXPR factorials<std::int64_t>::table_type factorial_table_i64 = factorials<std::int64_t>::table_type{{
INT64_C(1),
INT64_C(1),
INT64_C(2),
@ -168,17 +198,7 @@ namespace sprout {
INT64_C(121645100408832000),
INT64_C(2432902008176640000)
}};
};
template<>
struct factorials<std::uint64_t> {
public:
typedef std::uint64_t type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 20;
public:
typedef sprout::array<type, limit + 1> table_type;
public:
SPROUT_STATIC_CONSTEXPR table_type table = table_type{{
SPROUT_STATIC_CONSTEXPR factorials<std::uint64_t>::table_type factorial_table_u64 = factorials<std::uint64_t>::table_type{{
UINT64_C(1),
UINT64_C(1),
UINT64_C(2),
@ -201,17 +221,7 @@ namespace sprout {
UINT64_C(121645100408832000),
UINT64_C(2432902008176640000)
}};
};
template<>
struct factorials<float> {
public:
typedef float type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 34;
public:
typedef sprout::array<type, limit + 1> table_type;
public:
SPROUT_STATIC_CONSTEXPR table_type table = table_type{{
SPROUT_STATIC_CONSTEXPR factorials<float>::table_type factorial_table_f = factorials<float>::table_type{{
1.0F,
1.0F,
2.0F,
@ -248,17 +258,7 @@ namespace sprout {
0.868331761881188649551819440128e37F,
0.29523279903960414084761860964352e39F
}};
};
template<>
struct factorials<double> {
public:
typedef double type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 170;
public:
typedef sprout::array<type, limit + 1> table_type;
public:
SPROUT_STATIC_CONSTEXPR table_type table = table_type{{
SPROUT_STATIC_CONSTEXPR factorials<double>::table_type factorial_table_d = factorials<double>::table_type{{
1.0,
1.0,
2.0,
@ -431,17 +431,7 @@ namespace sprout {
0.4269068009004705274939251888899566538069e305,
0.7257415615307998967396728211129263114717e307
}};
};
template<>
struct factorials<long double> {
public:
typedef long double type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 170;
public:
typedef sprout::array<type, limit + 1> table_type;
public:
SPROUT_STATIC_CONSTEXPR table_type table = table_type{{
SPROUT_STATIC_CONSTEXPR factorials<long double>::table_type factorial_table_ld = factorials<long double>::table_type{{
1.0L,
1.0L,
2.0L,
@ -614,13 +604,60 @@ namespace sprout {
0.4269068009004705274939251888899566538069e305L,
0.7257415615307998967396728211129263114717e307L
}};
};
template<typename T>
SPROUT_CONSTEXPR T factorial_impl(std::size_t x);
template<>
SPROUT_CONSTEXPR std::int8_t factorial_impl<std::int8_t>(std::size_t x) {
return sprout::math::detail::factorial_table_i8[x];
}
template<>
SPROUT_CONSTEXPR std::uint8_t factorial_impl<std::uint8_t>(std::size_t x) {
return sprout::math::detail::factorial_table_u8[x];
}
template<>
SPROUT_CONSTEXPR std::int16_t factorial_impl<std::int16_t>(std::size_t x) {
return sprout::math::detail::factorial_table_i16[x];
}
template<>
SPROUT_CONSTEXPR std::uint16_t factorial_impl<std::uint16_t>(std::size_t x) {
return sprout::math::detail::factorial_table_u16[x];
}
template<>
SPROUT_CONSTEXPR std::int32_t factorial_impl<std::int32_t>(std::size_t x) {
return sprout::math::detail::factorial_table_i32[x];
}
template<>
SPROUT_CONSTEXPR std::uint32_t factorial_impl<std::uint32_t>(std::size_t x) {
return sprout::math::detail::factorial_table_u32[x];
}
template<>
SPROUT_CONSTEXPR std::int64_t factorial_impl<std::int64_t>(std::size_t x) {
return sprout::math::detail::factorial_table_i64[x];
}
template<>
SPROUT_CONSTEXPR std::uint64_t factorial_impl<std::uint64_t>(std::size_t x) {
return sprout::math::detail::factorial_table_u64[x];
}
template<>
SPROUT_CONSTEXPR float factorial_impl<float>(std::size_t x) {
return sprout::math::detail::factorial_table_f[x];
}
template<>
SPROUT_CONSTEXPR double factorial_impl<double>(std::size_t x) {
return sprout::math::detail::factorial_table_d[x];
}
template<>
SPROUT_CONSTEXPR long double factorial_impl<long double>(std::size_t x) {
return sprout::math::detail::factorial_table_ld[x];
}
} // namespace detail
//
// factorial
//
template<typename T>
SPROUT_CONSTEXPR T factorial(std::size_t x) {
return x <= sprout::math::detail::factorials<T>::limit
? sprout::math::detail::factorials<T>::table[x]
? sprout::math::detail::factorial_impl<T>(x)
: throw "assert(x <= sprout::math::detail::factorials<T>::limit)"
;
}
@ -631,3 +668,4 @@ namespace sprout {
#endif // #ifndef SPROUT_MATH_FACTORIAL_HPP