mirror of
https://github.com/bolero-MURAKAMI/Sprout
synced 2025-08-03 12:49:50 +00:00
workaround for no c++11 numeric_limits
This commit is contained in:
parent
ce16e24637
commit
7a1974742a
110 changed files with 727 additions and 436 deletions
|
@ -1,9 +1,9 @@
|
|||
#ifndef SPROUT_RANDOM_DETAIL_CONST_MOD_HPP
|
||||
#define SPROUT_RANDOM_DETAIL_CONST_MOD_HPP
|
||||
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/limits.hpp>
|
||||
#include <sprout/assert.hpp>
|
||||
#ifdef SPROUT_WORKAROUND_NOT_TERMINATE_RECURSIVE_CONSTEXPR_FUNCTION_TEMPLATE
|
||||
# include <sprout/workaround/recursive_function_template.hpp>
|
||||
|
@ -41,7 +41,7 @@ namespace sprout {
|
|||
return mult_schrage_1(a, value, m / a, m % a);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType mult_general(IntType a, IntType b) {
|
||||
return std::uintmax_t(modulus) <= std::numeric_limits<std::uintmax_t>::max() / modulus
|
||||
return std::uintmax_t(modulus) <= sprout::numeric_limits<std::uintmax_t>::max() / modulus
|
||||
? static_cast<IntType>(std::uintmax_t(a) * b % modulus)
|
||||
//: static_cast<IntType>(sprout::random::detail::mulmod(a, b, modulus)) // ???
|
||||
: (SPROUT_ASSERT_MSG(0, "Sorry, not implemented."), IntType())
|
||||
|
@ -51,7 +51,7 @@ namespace sprout {
|
|||
return a < b ? m - (b - a) : a - b;
|
||||
}
|
||||
static SPROUT_CONSTEXPR unsigned_type unsigned_m() {
|
||||
return m == 0 ? unsigned_type((std::numeric_limits<IntType>::max)()) + 1 : unsigned_type(m);
|
||||
return m == 0 ? unsigned_type((sprout::numeric_limits<IntType>::max)()) + 1 : unsigned_type(m);
|
||||
}
|
||||
#ifdef SPROUT_WORKAROUND_NOT_TERMINATE_RECURSIVE_CONSTEXPR_FUNCTION_TEMPLATE
|
||||
template<int D, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
||||
|
@ -106,10 +106,10 @@ namespace sprout {
|
|||
}
|
||||
template<int D, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian0_1(IntType c, IntType l1, IntType l2, IntType n) {
|
||||
return SPROUT_ASSERT_MSG(std::numeric_limits<IntType>::max() % n != n - 1, "c must be relatively prime to m."),
|
||||
return SPROUT_ASSERT_MSG(sprout::numeric_limits<IntType>::max() % n != n - 1, "c must be relatively prime to m."),
|
||||
invert_euclidian0_2<D + 1>(
|
||||
c, l1 + (std::numeric_limits<IntType>::max() / n) * l2, l2, n,
|
||||
std::numeric_limits<IntType>::max() - (std::numeric_limits<IntType>::max() / n) * n + 1
|
||||
c, l1 + (sprout::numeric_limits<IntType>::max() / n) * l2, l2, n,
|
||||
sprout::numeric_limits<IntType>::max() - (sprout::numeric_limits<IntType>::max() / n) * n + 1
|
||||
)
|
||||
;
|
||||
}
|
||||
|
@ -149,10 +149,10 @@ namespace sprout {
|
|||
return p == 0 ? l2 : invert_euclidian0_3(c, l1, l2 + (n / p) * l1, n - (n / p) * p, p);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian0_1(IntType c, IntType l1, IntType l2, IntType n) {
|
||||
return SPROUT_ASSERT_MSG(std::numeric_limits<IntType>::max() % n != n - 1, "c must be relatively prime to m."),
|
||||
return SPROUT_ASSERT_MSG(sprout::numeric_limits<IntType>::max() % n != n - 1, "c must be relatively prime to m."),
|
||||
invert_euclidian0_2(
|
||||
c, l1 + (std::numeric_limits<IntType>::max() / n) * l2, l2, n,
|
||||
std::numeric_limits<IntType>::max() - (std::numeric_limits<IntType>::max() / n) * n + 1
|
||||
c, l1 + (sprout::numeric_limits<IntType>::max() / n) * l2, l2, n,
|
||||
sprout::numeric_limits<IntType>::max() - (sprout::numeric_limits<IntType>::max() / n) * n + 1
|
||||
)
|
||||
;
|
||||
}
|
||||
|
@ -180,15 +180,15 @@ namespace sprout {
|
|||
return ((unsigned_m() - 1) & unsigned_m()) == 0 ? unsigned_type(a) * unsigned_type(x) & (unsigned_m() - 1)
|
||||
: a == 0 ? 0
|
||||
: a == 1 ? x
|
||||
: m <= std::numeric_limits<IntType>::max() / a ? mult_small(a, x)
|
||||
: std::numeric_limits<IntType>::is_signed && (m % a < m / a) ? mult_schrage(a, x)
|
||||
: m <= sprout::numeric_limits<IntType>::max() / a ? mult_small(a, x)
|
||||
: sprout::numeric_limits<IntType>::is_signed && (m % a < m / a) ? mult_schrage(a, x)
|
||||
: mult_general(a, x)
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType mult_add(IntType a, IntType x, IntType c) {
|
||||
return ((unsigned_m() - 1) & unsigned_m()) == 0 ? (unsigned_type(a) * unsigned_type(x) + unsigned_type(c)) & (unsigned_m() - 1)
|
||||
: a == 0 ? c
|
||||
: m <= (std::numeric_limits<IntType>::max() - c) / a ? (a * x + c) % (m + supress_warnings)
|
||||
: m <= (sprout::numeric_limits<IntType>::max() - c) / a ? (a * x + c) % (m + supress_warnings)
|
||||
: add(mult(a, x), c)
|
||||
;
|
||||
}
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
#define SPROUT_RANDOM_DETAIL_GENERATOR_BITS_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <limits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/limits.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
|
@ -12,7 +12,7 @@ namespace sprout {
|
|||
struct generator_bits {
|
||||
public:
|
||||
static SPROUT_CONSTEXPR std::size_t value() {
|
||||
return std::numeric_limits<typename URNG::result_type>::digits;
|
||||
return sprout::numeric_limits<typename URNG::result_type>::digits;
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
#ifndef SPROUT_RANDOM_DETAIL_SIGNED_UNSIGNED_TOOLS_HPP
|
||||
#define SPROUT_RANDOM_DETAIL_SIGNED_UNSIGNED_TOOLS_HPP
|
||||
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/limits.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
namespace detail {
|
||||
template<typename T, bool B = std::numeric_limits<T>::is_signed>
|
||||
template<typename T, bool B = sprout::numeric_limits<T>::is_signed>
|
||||
struct subtract {};
|
||||
template<typename T>
|
||||
struct subtract<T, false> {
|
||||
|
@ -32,7 +32,7 @@ namespace sprout {
|
|||
}
|
||||
};
|
||||
|
||||
template<typename T1, typename T2, bool B = std::numeric_limits<T2>::is_signed>
|
||||
template<typename T1, typename T2, bool B = sprout::numeric_limits<T2>::is_signed>
|
||||
struct add {};
|
||||
template<typename T1, typename T2>
|
||||
struct add<T1, T2, false> {
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/limits.hpp>
|
||||
#include <sprout/random/random_result.hpp>
|
||||
#include <sprout/random/detail/generator_bits.hpp>
|
||||
#include <sprout/detail/integer.hpp>
|
||||
|
@ -19,9 +19,9 @@ namespace sprout {
|
|||
typedef URNG base_type;
|
||||
typedef typename base_type::result_type base_result;
|
||||
typedef typename sprout::detail::uint_t<
|
||||
(std::numeric_limits<std::uintmax_t>::digits < std::numeric_limits<base_result>::digits)
|
||||
? std::numeric_limits<std::uintmax_t>::digits
|
||||
: std::numeric_limits<base_result>::digits
|
||||
(sprout::numeric_limits<std::uintmax_t>::digits < sprout::numeric_limits<base_result>::digits)
|
||||
? sprout::numeric_limits<std::uintmax_t>::digits
|
||||
: sprout::numeric_limits<base_result>::digits
|
||||
>::fast result_type;
|
||||
private:
|
||||
base_type rng_;
|
||||
|
@ -39,7 +39,7 @@ namespace sprout {
|
|||
return (
|
||||
result_type(2) << (
|
||||
NS_SSCRISK_CEL_OR_SPROUT::min(
|
||||
std::numeric_limits<result_type>::digits,
|
||||
sprout::numeric_limits<result_type>::digits,
|
||||
sprout::random::detail::generator_bits<base_type>::value()
|
||||
) - 1
|
||||
)
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
#define SPROUT_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
|
||||
|
||||
#include <ios>
|
||||
#include <limits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/limits.hpp>
|
||||
#include <sprout/math/log.hpp>
|
||||
#include <sprout/math/floor.hpp>
|
||||
#include <sprout/random/random_result.hpp>
|
||||
|
@ -112,7 +112,7 @@ namespace sprout {
|
|||
return 0;
|
||||
}
|
||||
SPROUT_CONSTEXPR result_type max() const SPROUT_NOEXCEPT {
|
||||
return std::numeric_limits<result_type>::max();
|
||||
return sprout::numeric_limits<result_type>::max();
|
||||
}
|
||||
SPROUT_CONSTEXPR param_type param() const SPROUT_NOEXCEPT {
|
||||
return param_type(p_);
|
||||
|
|
|
@ -2,10 +2,10 @@
|
|||
#define SPROUT_RANDOM_INVERSIVE_CONGRUENTIAL_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <ios>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/limits.hpp>
|
||||
#include <sprout/random/detail/const_mod.hpp>
|
||||
#include <sprout/random/random_result.hpp>
|
||||
#include <sprout/math/comparison.hpp>
|
||||
|
|
|
@ -2,10 +2,10 @@
|
|||
#define SPROUT_RANDOM_LINEAR_CONGRUENTIAL_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <ios>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/limits.hpp>
|
||||
#include <sprout/random/detail/const_mod.hpp>
|
||||
#include <sprout/random/random_result.hpp>
|
||||
#include <sprout/math/comparison.hpp>
|
||||
|
@ -18,7 +18,7 @@ namespace sprout {
|
|||
//
|
||||
template<typename UIntType, UIntType a, UIntType c, UIntType m>
|
||||
class linear_congruential_engine {
|
||||
static_assert(std::numeric_limits<UIntType>::is_integer, "std::numeric_limits<UIntType>::is_integer");
|
||||
static_assert(sprout::numeric_limits<UIntType>::is_integer, "sprout::numeric_limits<UIntType>::is_integer");
|
||||
static_assert(m == 0 || a < m, "m == 0 || a < m");
|
||||
static_assert(m == 0 || c < m, "m == 0 || c < m");
|
||||
public:
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
#ifndef SPROUT_RANDOM_NORMAL_DISTRIBUTION_HPP
|
||||
#define SPROUT_RANDOM_NORMAL_DISTRIBUTION_HPP
|
||||
|
||||
#include <limits>
|
||||
#include <ios>
|
||||
#include <istream>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/limits.hpp>
|
||||
#include <sprout/math/constants.hpp>
|
||||
#include <sprout/math/sin.hpp>
|
||||
#include <sprout/math/cos.hpp>
|
||||
|
@ -185,10 +185,10 @@ namespace sprout {
|
|||
return sigma_;
|
||||
}
|
||||
SPROUT_CONSTEXPR result_type min() const SPROUT_NOEXCEPT {
|
||||
return -std::numeric_limits<RealType>::infinity();
|
||||
return -sprout::numeric_limits<RealType>::infinity();
|
||||
}
|
||||
SPROUT_CONSTEXPR result_type max() const SPROUT_NOEXCEPT {
|
||||
return std::numeric_limits<RealType>::infinity();
|
||||
return sprout::numeric_limits<RealType>::infinity();
|
||||
}
|
||||
SPROUT_CONSTEXPR param_type param() const SPROUT_NOEXCEPT {
|
||||
return param_type(mean_, sigma_);
|
||||
|
|
|
@ -3,11 +3,11 @@
|
|||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <ios>
|
||||
#include <istream>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/limits.hpp>
|
||||
#include <sprout/array/array.hpp>
|
||||
#include <sprout/operation/fixed/set.hpp>
|
||||
#include <sprout/math/compare.hpp>
|
||||
|
@ -38,8 +38,8 @@ namespace sprout {
|
|||
: private sprout::random::detail::shuffle_order_engine_member<UniformRandomNumberGenerator, k>
|
||||
{
|
||||
static_assert(
|
||||
std::numeric_limits<typename UniformRandomNumberGenerator::result_type>::is_integer,
|
||||
"std::numeric_limits<typename UniformRandomNumberGenerator::result_type>::is_integer"
|
||||
sprout::numeric_limits<typename UniformRandomNumberGenerator::result_type>::is_integer,
|
||||
"sprout::numeric_limits<typename UniformRandomNumberGenerator::result_type>::is_integer"
|
||||
);
|
||||
static_assert(k > 0, "k > 0");
|
||||
private:
|
||||
|
@ -113,8 +113,8 @@ namespace sprout {
|
|||
return generate_1(
|
||||
rng_(),
|
||||
k == 1 ? BaseUnsigned(0)
|
||||
: sprout::math::less(brange, std::numeric_limits<BaseUnsigned>::max() / k) ? BaseUnsigned(k * off / (brange + 1))
|
||||
: sprout::math::less(brange, std::numeric_limits<std::uintmax_t>::max() / k)
|
||||
: sprout::math::less(brange, sprout::numeric_limits<BaseUnsigned>::max() / k) ? BaseUnsigned(k * off / (brange + 1))
|
||||
: sprout::math::less(brange, sprout::numeric_limits<std::uintmax_t>::max() / k)
|
||||
? static_cast<BaseUnsigned>(static_cast<std::uintmax_t>(off) * k / (static_cast<std::uintmax_t>(brange) + 1))
|
||||
//: static_cast<BaseUnsigned>(sprout::random::detail::muldiv(off, k, static_cast<std::uintmax_t>(brange) + 1)) // ???
|
||||
: (SPROUT_ASSERT_MSG(0, "Sorry, not implemented."), sprout::random::random_result<shuffle_order_engine>())
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
|
||||
#include <iosfwd>
|
||||
#include <istream>
|
||||
#include <limits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/limits.hpp>
|
||||
#include <sprout/random/random_result.hpp>
|
||||
#ifdef SPROUT_WORKAROUND_NOT_TERMINATE_RECURSIVE_CONSTEXPR_FUNCTION_TEMPLATE
|
||||
# include <sprout/workaround/recursive_function_template.hpp>
|
||||
|
@ -76,7 +76,7 @@ namespace sprout {
|
|||
result_type(rnd.result() - eng.min()) * (
|
||||
result_type(1) / (
|
||||
result_type(eng.max() - eng.min()) + result_type(
|
||||
std::numeric_limits<base_result>::is_integer ? 1 : 0
|
||||
sprout::numeric_limits<base_result>::is_integer ? 1 : 0
|
||||
)
|
||||
)
|
||||
)
|
||||
|
@ -106,7 +106,7 @@ namespace sprout {
|
|||
result_type(rnd.result() - eng.min()) * (
|
||||
result_type(1) / (
|
||||
result_type(eng.max() - eng.min()) + result_type(
|
||||
std::numeric_limits<base_result>::is_integer ? 1 : 0
|
||||
sprout::numeric_limits<base_result>::is_integer ? 1 : 0
|
||||
)
|
||||
)
|
||||
)
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
#ifndef SPROUT_RANDOM_UNIFORM_INT_DISTRIBUTION_HPP
|
||||
#define SPROUT_RANDOM_UNIFORM_INT_DISTRIBUTION_HPP
|
||||
|
||||
#include <limits>
|
||||
#include <ios>
|
||||
#include <istream>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/limits.hpp>
|
||||
#include <sprout/random/detail/signed_unsigned_tools.hpp>
|
||||
#include <sprout/random/detail/uniform_int_float.hpp>
|
||||
#include <sprout/random/random_result.hpp>
|
||||
|
@ -100,7 +100,7 @@ namespace sprout {
|
|||
return sprout::random::detail::generate_uniform_int_true_3_1<D + 1>(
|
||||
eng(), min_value, range,
|
||||
bmin, brange,
|
||||
brange == std::numeric_limits<BaseUnsigned>::max()
|
||||
brange == sprout::numeric_limits<BaseUnsigned>::max()
|
||||
? brange / (static_cast<BaseUnsigned>(range) + 1) + (
|
||||
brange % (static_cast<BaseUnsigned>(range) + 1) == static_cast<BaseUnsigned>(range) ? 1 : 0
|
||||
)
|
||||
|
@ -160,7 +160,7 @@ namespace sprout {
|
|||
BaseResult bmin, BaseUnsigned brange, RangeType result, RangeType mult, RangeType result_increment
|
||||
)
|
||||
{
|
||||
return std::numeric_limits<RangeType>::max() / mult < result_increment
|
||||
return sprout::numeric_limits<RangeType>::max() / mult < result_increment
|
||||
? sprout::random::detail::generate_uniform_int_true_2<D + 1>(eng, min_value, range, bmin, brange)
|
||||
: sprout::random::detail::generate_uniform_int_true_2_4<D + 1>(
|
||||
eng, min_value, range,
|
||||
|
@ -290,7 +290,7 @@ namespace sprout {
|
|||
return sprout::random::detail::generate_uniform_int_true_2_1<D + 1>(
|
||||
eng, min_value, range,
|
||||
bmin, brange,
|
||||
range == std::numeric_limits<RangeType>::max()
|
||||
range == sprout::numeric_limits<RangeType>::max()
|
||||
? range / (RangeType(brange) + 1) + (
|
||||
range % (RangeType(brange) + 1) == RangeType(brange) ? 1 : 0
|
||||
)
|
||||
|
@ -465,7 +465,7 @@ namespace sprout {
|
|||
return sprout::random::detail::generate_uniform_int_true_3_1(
|
||||
eng(), min_value, range,
|
||||
bmin, brange,
|
||||
brange == std::numeric_limits<BaseUnsigned>::max()
|
||||
brange == sprout::numeric_limits<BaseUnsigned>::max()
|
||||
? brange / (static_cast<BaseUnsigned>(range) + 1) + (
|
||||
brange % (static_cast<BaseUnsigned>(range) + 1) == static_cast<BaseUnsigned>(range) ? 1 : 0
|
||||
)
|
||||
|
@ -501,7 +501,7 @@ namespace sprout {
|
|||
BaseResult bmin, BaseUnsigned brange, RangeType result, RangeType mult, RangeType result_increment
|
||||
)
|
||||
{
|
||||
return std::numeric_limits<RangeType>::max() / mult < result_increment
|
||||
return sprout::numeric_limits<RangeType>::max() / mult < result_increment
|
||||
? sprout::random::detail::generate_uniform_int_true_2(eng, min_value, range, bmin, brange)
|
||||
: sprout::random::detail::generate_uniform_int_true_2_4(
|
||||
eng, min_value, range,
|
||||
|
@ -587,7 +587,7 @@ namespace sprout {
|
|||
return sprout::random::detail::generate_uniform_int_true_2_1(
|
||||
eng, min_value, range,
|
||||
bmin, brange,
|
||||
range == std::numeric_limits<RangeType>::max()
|
||||
range == sprout::numeric_limits<RangeType>::max()
|
||||
? range / (RangeType(brange) + 1) + (
|
||||
range % (RangeType(brange) + 1) == RangeType(brange) ? 1 : 0
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue