mirror of
https://github.com/bolero-MURAKAMI/Sprout
synced 2025-08-03 12:49:50 +00:00
add SPROUT_ASSERT
This commit is contained in:
parent
a5e14e71e1
commit
07f052fb6e
32 changed files with 386 additions and 284 deletions
|
@ -2,9 +2,9 @@
|
|||
#define SPROUT_RANDOM_BERNOULLI_DISTRIBUTION_HPP
|
||||
|
||||
#include <iosfwd>
|
||||
#include <stdexcept>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/random/random_result.hpp>
|
||||
#include <sprout/assert.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
|
@ -16,16 +16,6 @@ namespace sprout {
|
|||
public:
|
||||
typedef int input_type;
|
||||
typedef bool result_type;
|
||||
private:
|
||||
static SPROUT_CONSTEXPR bool arg_check_nothrow(RealType p_arg) {
|
||||
return p_arg >= 0 && p_arg <= 1;
|
||||
}
|
||||
static SPROUT_CONSTEXPR RealType arg_check(RealType p_arg) {
|
||||
return arg_check_nothrow(p_arg)
|
||||
? p_arg
|
||||
: throw std::invalid_argument("bernoulli_distribution<>: invalid argument (p_arg >= 0 && p_arg <= 1)")
|
||||
;
|
||||
}
|
||||
public:
|
||||
//
|
||||
// param_type
|
||||
|
@ -33,10 +23,6 @@ namespace sprout {
|
|||
class param_type {
|
||||
public:
|
||||
typedef bernoulli_distribution distribution_type;
|
||||
private:
|
||||
static SPROUT_CONSTEXPR bool arg_check_nothrow(RealType p_arg) {
|
||||
return distribution_type::arg_check_nothrow(p_arg);
|
||||
}
|
||||
private:
|
||||
RealType p_;
|
||||
public:
|
||||
|
@ -44,7 +30,7 @@ namespace sprout {
|
|||
: p_(RealType(0.5))
|
||||
{}
|
||||
explicit SPROUT_CONSTEXPR param_type(RealType p_arg)
|
||||
: p_(arg_check(p_arg))
|
||||
: p_((SPROUT_ASSERT(p_arg >= RealType(0)), SPROUT_ASSERT(p_arg <= RealType(1)), p_arg))
|
||||
{}
|
||||
SPROUT_CONSTEXPR RealType p() const SPROUT_NOEXCEPT {
|
||||
return p_;
|
||||
|
@ -57,7 +43,7 @@ namespace sprout {
|
|||
{
|
||||
RealType p;
|
||||
if (lhs >> p) {
|
||||
if (arg_check_nothrow(p)) {
|
||||
if (p >= RealType(0) && p <= RealType(1)) {
|
||||
rhs.p_ = p;
|
||||
} else {
|
||||
lhs.setstate(std::ios_base::failbit);
|
||||
|
@ -99,7 +85,7 @@ namespace sprout {
|
|||
: p_(RealType(0.5))
|
||||
{}
|
||||
explicit SPROUT_CONSTEXPR bernoulli_distribution(RealType p_arg)
|
||||
: p_(arg_check(p_arg))
|
||||
: p_((SPROUT_ASSERT(p_arg >= RealType(0)), SPROUT_ASSERT(p_arg <= RealType(1)), p_arg))
|
||||
{}
|
||||
explicit SPROUT_CONSTEXPR bernoulli_distribution(param_type const& parm)
|
||||
: p_(parm.p())
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
#include <iosfwd>
|
||||
#include <istream>
|
||||
#include <stdexcept>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/array/array.hpp>
|
||||
#include <sprout/cstdlib/abs.hpp>
|
||||
|
@ -14,6 +13,7 @@
|
|||
#include <sprout/math/floor.hpp>
|
||||
#include <sprout/random/random_result.hpp>
|
||||
#include <sprout/random/uniform_01.hpp>
|
||||
#include <sprout/assert.hpp>
|
||||
#ifdef SPROUT_WORKAROUND_NOT_TERMINATE_RECURSIVE_CONSTEXPR_FUNCTION_TEMPLATE
|
||||
# include <sprout/workaround/recursive_function_template.hpp>
|
||||
#endif
|
||||
|
@ -69,16 +69,6 @@ namespace sprout {
|
|||
RealType v_r;
|
||||
RealType u_rv_r;
|
||||
};
|
||||
private:
|
||||
static SPROUT_CONSTEXPR bool arg_check_nothrow(IntType t_arg, RealType p_arg) {
|
||||
return t_arg >= IntType(0) && RealType(0) <= p_arg && p_arg <= RealType(1);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType arg_check(IntType t_arg, RealType p_arg) {
|
||||
return arg_check_nothrow(t_arg, p_arg)
|
||||
? t_arg
|
||||
: throw std::invalid_argument("binomial_distribution<>: invalid argument (t_arg >= 0 && 0 <= p_arg && p_arg <= 1)")
|
||||
;
|
||||
}
|
||||
public:
|
||||
//
|
||||
// param_type
|
||||
|
@ -86,10 +76,6 @@ namespace sprout {
|
|||
class param_type {
|
||||
public:
|
||||
typedef binomial_distribution distribution_type;
|
||||
private:
|
||||
static SPROUT_CONSTEXPR bool arg_check_nothrow(IntType t_arg, RealType p_arg) {
|
||||
return distribution_type::arg_check_nothrow(t_arg, p_arg);
|
||||
}
|
||||
private:
|
||||
IntType t_;
|
||||
RealType p_;
|
||||
|
@ -99,8 +85,8 @@ namespace sprout {
|
|||
, p_(0.5)
|
||||
{}
|
||||
explicit SPROUT_CONSTEXPR param_type(IntType t_arg, RealType p_arg = RealType(0.5))
|
||||
: t_(arg_check(t_arg, p_arg))
|
||||
, p_(p_arg)
|
||||
: t_((SPROUT_ASSERT(t_arg >= IntType(0)), t_arg))
|
||||
, p_((SPROUT_ASSERT(RealType(0) <= p_arg && p_arg <= RealType(1)), p_arg))
|
||||
{}
|
||||
SPROUT_CONSTEXPR IntType t() const SPROUT_NOEXCEPT {
|
||||
return t_;
|
||||
|
@ -117,7 +103,7 @@ namespace sprout {
|
|||
IntType t;
|
||||
RealType p;
|
||||
if (lhs >> t >> std::ws >> p) {
|
||||
if (arg_check_nothrow(t, p)) {
|
||||
if (t >= IntType(0) && RealType(0) <= p && p <= RealType(1)) {
|
||||
rhs.t_ = t;
|
||||
rhs.p_ = p;
|
||||
} else {
|
||||
|
@ -622,8 +608,8 @@ namespace sprout {
|
|||
, q_n_(init_use_inversion(1, RealType(0.5)) ? init_q_n(1, RealType(0.5)) : RealType())
|
||||
{}
|
||||
explicit SPROUT_CONSTEXPR binomial_distribution(IntType t_arg, RealType p_arg = RealType(0.5))
|
||||
: t_(arg_check(t_arg, p_arg))
|
||||
, p_(p_arg)
|
||||
: t_((SPROUT_ASSERT(t_arg >= IntType(0)), t_arg))
|
||||
, p_((SPROUT_ASSERT(RealType(0) <= p_arg && p_arg <= RealType(1)), p_arg))
|
||||
, m_(init_m(t_arg, p_arg))
|
||||
, btrd_(!init_use_inversion(t_arg, p_arg) ? init_btrd(t_arg, p_arg) : btrd_type())
|
||||
, q_n_(init_use_inversion(t_arg, p_arg) ? init_q_n(t_arg, p_arg) : RealType())
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
#define SPROUT_RANDOM_DETAIL_CONST_MOD_HPP
|
||||
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/assert.hpp>
|
||||
#ifdef SPROUT_WORKAROUND_NOT_TERMINATE_RECURSIVE_CONSTEXPR_FUNCTION_TEMPLATE
|
||||
# include <sprout/workaround/recursive_function_template.hpp>
|
||||
#endif
|
||||
|
@ -17,10 +17,10 @@ namespace sprout {
|
|||
private:
|
||||
typedef typename std::make_unsigned<IntType>::type unsigned_type;
|
||||
private:
|
||||
SPROUT_STATIC_CONSTEXPR IntType suppress_warnings = m == 0;
|
||||
SPROUT_STATIC_CONSTEXPR IntType modulus = m + suppress_warnings;
|
||||
SPROUT_STATIC_CONSTEXPR IntType supress_warnings = m == 0;
|
||||
SPROUT_STATIC_CONSTEXPR IntType modulus = m + supress_warnings;
|
||||
private:
|
||||
static_assert(suppress_warnings == 0, "suppress_warnings == 0");
|
||||
static_assert(supress_warnings == 0, "supress_warnings == 0");
|
||||
static_assert(modulus == m, "modulus == m");
|
||||
private:
|
||||
static SPROUT_CONSTEXPR IntType pow_1(IntType a, std::uintmax_t exponent, IntType result = 1) {
|
||||
|
@ -30,12 +30,11 @@ namespace sprout {
|
|||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType mult_small(IntType a, IntType x) {
|
||||
return a * x % (m + suppress_warnings);
|
||||
return a * x % (m + supress_warnings);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType mult_schrage_1(IntType a, IntType value, IntType q, IntType r) {
|
||||
return r < q
|
||||
? sub(a * (value % q), r * (value / q))
|
||||
: throw std::domain_error("const_mod<>: domain error (r < q)")
|
||||
return SPROUT_ASSERT(r < q),
|
||||
sub(a * (value % q), r * (value / q))
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType mult_schrage(IntType a, IntType value) {
|
||||
|
@ -45,7 +44,7 @@ namespace sprout {
|
|||
return std::uintmax_t(modulus) <= std::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)) // ???
|
||||
: throw std::domain_error("const_mod<>: Sorry, not implemented.")
|
||||
: (SPROUT_ASSERT_MSG(0, "Sorry, not implemented."), IntType())
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType sub(IntType a, IntType b) {
|
||||
|
@ -81,9 +80,8 @@ namespace sprout {
|
|||
}
|
||||
template<int D = 16, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian(IntType c) {
|
||||
return c > 0
|
||||
? c == 1 ? 1 : invert_euclidian_1<D + 1>(c, 0, 1, c, m)
|
||||
: throw std::domain_error("const_mod<>: domain error (c > 0)")
|
||||
return SPROUT_ASSERT(c > 0),
|
||||
c == 1 ? 1 : invert_euclidian_1<D + 1>(c, 0, 1, c, m)
|
||||
;
|
||||
}
|
||||
template<int D = 16, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
||||
|
@ -108,12 +106,11 @@ 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, IntType p) {
|
||||
return std::numeric_limits<IntType>::max() % n != n - 1
|
||||
? invert_euclidian0_2<D + 1>(
|
||||
return SPROUT_ASSERT_MSG(std::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
|
||||
)
|
||||
: throw std::domain_error("const_mod<>: domain error (numeric_limits<IntType>::max() % n != n - 1)")
|
||||
;
|
||||
}
|
||||
template<int D, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
||||
|
@ -122,9 +119,8 @@ namespace sprout {
|
|||
}
|
||||
template<int D = 16, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian0(IntType c) {
|
||||
return c > 0
|
||||
? c == 1 ? 1 : invert_euclidian0_1<D + 1>(c, 0, 1, c, m)
|
||||
: throw std::domain_error("const_mod<>: domain error (c > 0)")
|
||||
return SPROUT_ASSERT(c > 0),
|
||||
c == 1 ? 1 : invert_euclidian0_1<D + 1>(c, 0, 1, c, m)
|
||||
;
|
||||
}
|
||||
template<int D = 16, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
||||
|
@ -142,9 +138,8 @@ namespace sprout {
|
|||
return invert_euclidian_2(c, l1 + (p / n) * l2, l2, n, p - (p / n) * n);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian(IntType c) {
|
||||
return c > 0
|
||||
? c == 1 ? 1 : invert_euclidian_1(c, 0, 1, c, m)
|
||||
: throw std::domain_error("const_mod<>: domain error (c > 0)")
|
||||
return SPROUT_ASSERT(c > 0),
|
||||
c == 1 ? 1 : invert_euclidian_1(c, 0, 1, c, m)
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian0_3(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
||||
|
@ -154,18 +149,16 @@ 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, IntType p) {
|
||||
return std::numeric_limits<IntType>::max() % n != n - 1
|
||||
? invert_euclidian0_2(
|
||||
return SPROUT_ASSERT_MSG(std::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
|
||||
)
|
||||
: throw std::domain_error("const_mod<>: domain error (numeric_limits<IntType>::max() % n != n - 1)")
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian0(IntType c) {
|
||||
return c > 0
|
||||
? c == 1 ? 1 : invert_euclidian0_1(c, 0, 1, c, m)
|
||||
: throw std::domain_error("const_mod<>: domain error (c > 0)")
|
||||
return SPROUT_ASSERT(c > 0),
|
||||
c == 1 ? 1 : invert_euclidian0_1(c, 0, 1, c, m)
|
||||
;
|
||||
}
|
||||
#endif
|
||||
|
@ -173,7 +166,7 @@ namespace sprout {
|
|||
static SPROUT_CONSTEXPR IntType apply(IntType x) {
|
||||
return ((unsigned_m() - 1) & unsigned_m()) == 0
|
||||
? (unsigned_type(x)) & (unsigned_m() - 1)
|
||||
: x % (m + suppress_warnings)
|
||||
: x % (m + supress_warnings)
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType add(IntType x, IntType c) {
|
||||
|
@ -195,7 +188,7 @@ namespace sprout {
|
|||
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 + suppress_warnings)
|
||||
: m <= (std::numeric_limits<IntType>::max() - c) / a ? (a * x + c) % (m + supress_warnings)
|
||||
: add(mult(a, x), c)
|
||||
;
|
||||
}
|
||||
|
|
|
@ -3,12 +3,12 @@
|
|||
|
||||
#include <ios>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/log.hpp>
|
||||
#include <sprout/math/floor.hpp>
|
||||
#include <sprout/random/random_result.hpp>
|
||||
#include <sprout/random/uniform_01.hpp>
|
||||
#include <sprout/assert.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
|
@ -20,16 +20,6 @@ namespace sprout {
|
|||
public:
|
||||
typedef RealType input_type;
|
||||
typedef IntType result_type;
|
||||
private:
|
||||
static SPROUT_CONSTEXPR bool arg_check_nothrow(RealType p_arg) {
|
||||
return RealType(0) < p_arg && p_arg < RealType(1);
|
||||
}
|
||||
static SPROUT_CONSTEXPR RealType arg_check(RealType p_arg) {
|
||||
return arg_check_nothrow(p_arg)
|
||||
? p_arg
|
||||
: throw std::invalid_argument("geometric_distribution<>: invalid argument (0 < p_arg && p_arg < 1)")
|
||||
;
|
||||
}
|
||||
public:
|
||||
//
|
||||
// param_type
|
||||
|
@ -37,10 +27,6 @@ namespace sprout {
|
|||
class param_type {
|
||||
public:
|
||||
typedef geometric_distribution distribution_type;
|
||||
private:
|
||||
static SPROUT_CONSTEXPR bool arg_check_nothrow(RealType p_arg) {
|
||||
return distribution_type::arg_check_nothrow(p_arg);
|
||||
}
|
||||
private:
|
||||
RealType p_;
|
||||
public:
|
||||
|
@ -48,7 +34,7 @@ namespace sprout {
|
|||
: p_(RealType(0.5))
|
||||
{}
|
||||
explicit SPROUT_CONSTEXPR param_type(RealType p_arg)
|
||||
: p_(arg_check(p_arg))
|
||||
: p_((SPROUT_ASSERT(RealType(0) < p_arg), SPROUT_ASSERT(p_arg < RealType(1)), p_arg))
|
||||
{}
|
||||
SPROUT_CONSTEXPR RealType p() const SPROUT_NOEXCEPT {
|
||||
return p_;
|
||||
|
@ -61,7 +47,7 @@ namespace sprout {
|
|||
{
|
||||
RealType p;
|
||||
if (lhs >> p) {
|
||||
if (arg_check_nothrow(p)) {
|
||||
if (RealType(0) < p && p < RealType(1)) {
|
||||
rhs.p_ = p;
|
||||
} else {
|
||||
lhs.setstate(std::ios_base::failbit);
|
||||
|
@ -112,7 +98,7 @@ namespace sprout {
|
|||
, log_1mp_(init_log_1mp(RealType(0.5)))
|
||||
{}
|
||||
explicit SPROUT_CONSTEXPR geometric_distribution(RealType p_arg)
|
||||
: p_(arg_check(p_arg))
|
||||
: p_((SPROUT_ASSERT(RealType(0) < p_arg), SPROUT_ASSERT(p_arg < RealType(1)), p_arg))
|
||||
, log_1mp_(init_log_1mp(p_arg))
|
||||
{}
|
||||
explicit SPROUT_CONSTEXPR geometric_distribution(param_type const& parm)
|
||||
|
|
|
@ -4,11 +4,12 @@
|
|||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <ios>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/random/detail/const_mod.hpp>
|
||||
#include <sprout/random/random_result.hpp>
|
||||
#include <sprout/math/comparison.hpp>
|
||||
#include <sprout/assert.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
|
@ -33,27 +34,11 @@ namespace sprout {
|
|||
static SPROUT_CONSTEXPR result_type static_max() SPROUT_NOEXCEPT {
|
||||
return modulus - 1;
|
||||
}
|
||||
private:
|
||||
template<typename T>
|
||||
static SPROUT_CONSTEXPR typename std::enable_if<!(b == 0) && std::is_unsigned<T>::value, bool>::type
|
||||
arg_check_nothrow(T const& x0) {
|
||||
return x0 <= static_max();
|
||||
}
|
||||
template<typename T>
|
||||
static SPROUT_CONSTEXPR typename std::enable_if<!(!(b == 0) && std::is_unsigned<T>::value), bool>::type
|
||||
arg_check_nothrow(T const& x0) {
|
||||
return x0 >= static_min() && x0 <= static_max();
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType arg_check(IntType const& x0) {
|
||||
return arg_check_nothrow(x0)
|
||||
? x0
|
||||
: throw std::invalid_argument(
|
||||
"inversive_congruential_engine<>: invalid argument (x0 >= static_min() && x0 <= static_max())"
|
||||
)
|
||||
;
|
||||
static SPROUT_CONSTEXPR IntType init_seed_3(IntType const& x0) {
|
||||
return SPROUT_ASSERT(sprout::math::greater_equal(x0, static_min())), SPROUT_ASSERT(x0 <= static_max()), x0;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType init_seed_2(IntType const& x0) {
|
||||
return arg_check(increment == 0 && x0 == 0 ? 1 : x0);
|
||||
return init_seed_3(increment == 0 && x0 == 0 ? 1 : x0);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType init_seed_1(IntType const& x0) {
|
||||
return init_seed_2(x0 <= 0 && x0 != 0 ? x0 + modulus : x0);
|
||||
|
@ -105,7 +90,7 @@ namespace sprout {
|
|||
{
|
||||
IntType x;
|
||||
if (lhs >> x) {
|
||||
if(arg_check_nothrow(x)) {
|
||||
if (sprout::math::greater_equal(x, static_min()) && x <= static_max()) {
|
||||
rhs.x_ = x;
|
||||
} else {
|
||||
lhs.setstate(std::ios_base::failbit);
|
||||
|
|
|
@ -4,11 +4,12 @@
|
|||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <ios>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/random/detail/const_mod.hpp>
|
||||
#include <sprout/random/random_result.hpp>
|
||||
#include <sprout/math/comparison.hpp>
|
||||
#include <sprout/assert.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
|
@ -37,23 +38,11 @@ namespace sprout {
|
|||
return m - 1;
|
||||
}
|
||||
private:
|
||||
template<typename T>
|
||||
static SPROUT_CONSTEXPR typename std::enable_if<!(c == 0) && std::is_unsigned<T>::value, bool>::type
|
||||
arg_check_nothrow(T const& x0) {
|
||||
return x0 <= static_max();
|
||||
}
|
||||
template<typename T>
|
||||
static SPROUT_CONSTEXPR typename std::enable_if<!(!(c == 0) && std::is_unsigned<T>::value), bool>::type
|
||||
arg_check_nothrow(T const& x0) {
|
||||
return x0 >= static_min() && x0 <= static_max();
|
||||
}
|
||||
static SPROUT_CONSTEXPR UIntType arg_check(UIntType const& x0) {
|
||||
return arg_check_nothrow(x0) ? x0
|
||||
: throw std::invalid_argument("linear_congruential_engine<>: invalid argument (x0 >= static_min() && x0 <= static_max())")
|
||||
;
|
||||
static SPROUT_CONSTEXPR UIntType init_seed_3(UIntType const& x0) {
|
||||
return SPROUT_ASSERT(sprout::math::greater_equal(x0, static_min())), SPROUT_ASSERT(x0 <= static_max()), x0;
|
||||
}
|
||||
static SPROUT_CONSTEXPR UIntType init_seed_2(UIntType const& x0) {
|
||||
return arg_check(increment == 0 && x0 == 0 ? 1 : x0);
|
||||
return init_seed_3(increment == 0 && x0 == 0 ? 1 : x0);
|
||||
}
|
||||
static SPROUT_CONSTEXPR UIntType init_seed_1(UIntType const& x0) {
|
||||
return init_seed_2(x0 <= 0 && x0 != 0 ? x0 + modulus : x0);
|
||||
|
@ -103,7 +92,7 @@ namespace sprout {
|
|||
{
|
||||
UIntType x;
|
||||
if (lhs >> x) {
|
||||
if(arg_check_nothrow(x)) {
|
||||
if (sprout::math::greater_equal(x, static_min()) && x <= static_max()) {
|
||||
rhs.x_ = x;
|
||||
} else {
|
||||
lhs.setstate(std::ios_base::failbit);
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
#include <limits>
|
||||
#include <ios>
|
||||
#include <istream>
|
||||
#include <stdexcept>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/constants.hpp>
|
||||
#include <sprout/math/sin.hpp>
|
||||
|
@ -13,6 +12,7 @@
|
|||
#include <sprout/math/sqrt.hpp>
|
||||
#include <sprout/random/uniform_01.hpp>
|
||||
#include <sprout/random/random_result.hpp>
|
||||
#include <sprout/assert.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
|
@ -26,16 +26,6 @@ namespace sprout {
|
|||
typedef RealType result_type;
|
||||
private:
|
||||
struct private_constructor_tag {};
|
||||
private:
|
||||
static SPROUT_CONSTEXPR bool arg_check_nothrow(RealType mean_arg, RealType sigma_arg) {
|
||||
return sigma_arg >= RealType(0);
|
||||
}
|
||||
static SPROUT_CONSTEXPR RealType arg_check(RealType mean_arg, RealType sigma_arg) {
|
||||
return arg_check_nothrow(mean_arg, sigma_arg)
|
||||
? mean_arg
|
||||
: throw std::invalid_argument("normal_distribution<>: invalid argument (sigma_arg >= 0)")
|
||||
;
|
||||
}
|
||||
public:
|
||||
//
|
||||
// param_type
|
||||
|
@ -43,10 +33,6 @@ namespace sprout {
|
|||
class param_type {
|
||||
public:
|
||||
typedef normal_distribution distribution_type;
|
||||
private:
|
||||
static SPROUT_CONSTEXPR bool arg_check_nothrow(RealType mean_arg, RealType sigma_arg) {
|
||||
return distribution_type::arg_check_nothrow(mean_arg, sigma_arg);
|
||||
}
|
||||
private:
|
||||
RealType mean_;
|
||||
RealType sigma_;
|
||||
|
@ -56,8 +42,8 @@ namespace sprout {
|
|||
, sigma_(RealType(1.0))
|
||||
{}
|
||||
explicit SPROUT_CONSTEXPR param_type(RealType mean_arg, RealType sigma_arg = RealType(1.0))
|
||||
: mean_(arg_check(mean_arg, sigma_arg))
|
||||
, sigma_(sigma_arg)
|
||||
: mean_(mean_arg)
|
||||
, sigma_((SPROUT_ASSERT(sigma_arg >= RealType(0)), sigma_arg))
|
||||
{}
|
||||
SPROUT_CONSTEXPR RealType mean() const SPROUT_NOEXCEPT {
|
||||
return mean_;
|
||||
|
@ -77,7 +63,7 @@ namespace sprout {
|
|||
RealType mean;
|
||||
RealType sigma;
|
||||
if (lhs >> mean >> std::ws >> sigma) {
|
||||
if (arg_check_nothrow(mean, sigma)) {
|
||||
if (sigma >= RealType(0)) {
|
||||
rhs.mean_ = mean;
|
||||
rhs.sigma_ = sigma;
|
||||
} else {
|
||||
|
@ -174,8 +160,8 @@ namespace sprout {
|
|||
, valid_(false)
|
||||
{}
|
||||
explicit SPROUT_CONSTEXPR normal_distribution(RealType mean_arg, RealType sigma_arg = RealType(1.0))
|
||||
: mean_(arg_check(mean_arg, sigma_arg))
|
||||
, sigma_(sigma_arg)
|
||||
: mean_(mean_arg)
|
||||
, sigma_((SPROUT_ASSERT(sigma_arg >= RealType(0)), sigma_arg))
|
||||
, r1_(0)
|
||||
, r2_(0)
|
||||
, cached_rho_(0)
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
#include <limits>
|
||||
#include <ios>
|
||||
#include <istream>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/array/array.hpp>
|
||||
|
@ -15,6 +14,7 @@
|
|||
#include <sprout/random/random_result.hpp>
|
||||
#include <sprout/random/linear_congruential.hpp>
|
||||
#include <sprout/random/detail/signed_unsigned_tools.hpp>
|
||||
#include <sprout/assert.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
|
@ -117,7 +117,7 @@ namespace sprout {
|
|||
: sprout::math::less(brange, std::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)) // ???
|
||||
: throw std::domain_error("shuffle_order_engine<>: Sorry, not implemented.")
|
||||
: (SPROUT_ASSERT_MSG(0, "Sorry, not implemented."), sprout::random::random_result<shuffle_order_engine>())
|
||||
);
|
||||
}
|
||||
public:
|
||||
|
|
|
@ -4,12 +4,12 @@
|
|||
#include <limits>
|
||||
#include <ios>
|
||||
#include <istream>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/random/detail/signed_unsigned_tools.hpp>
|
||||
#include <sprout/random/detail/uniform_int_float.hpp>
|
||||
#include <sprout/random/random_result.hpp>
|
||||
#include <sprout/assert.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
|
@ -288,15 +288,6 @@ namespace sprout {
|
|||
public:
|
||||
typedef IntType input_type;
|
||||
typedef IntType result_type;
|
||||
private:
|
||||
static SPROUT_CONSTEXPR bool arg_check_nothrow(IntType min_arg, IntType max_arg) {
|
||||
return min_arg <= max_arg;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType arg_check(IntType min_arg, IntType max_arg) {
|
||||
return arg_check_nothrow(min_arg, max_arg) ? min_arg
|
||||
: throw std::invalid_argument("uniform_int_distribution<>: invalid argument (min_arg <= max_arg)")
|
||||
;
|
||||
}
|
||||
public:
|
||||
//
|
||||
// param_type
|
||||
|
@ -304,10 +295,6 @@ namespace sprout {
|
|||
class param_type {
|
||||
public:
|
||||
typedef uniform_int_distribution distribution_type;
|
||||
private:
|
||||
static SPROUT_CONSTEXPR bool arg_check_nothrow(IntType min_arg, IntType max_arg) {
|
||||
return distribution_type::arg_check_nothrow(min_arg, max_arg);
|
||||
}
|
||||
private:
|
||||
IntType min_;
|
||||
IntType max_;
|
||||
|
@ -317,7 +304,7 @@ namespace sprout {
|
|||
, max_(9)
|
||||
{}
|
||||
explicit SPROUT_CONSTEXPR param_type(IntType min_arg, IntType max_arg = 9)
|
||||
: min_(arg_check(min_arg, max_arg))
|
||||
: min_((SPROUT_ASSERT(min_arg <= max_arg), min_arg))
|
||||
, max_(max_arg)
|
||||
{}
|
||||
SPROUT_CONSTEXPR IntType a() const SPROUT_NOEXCEPT {
|
||||
|
@ -335,7 +322,7 @@ namespace sprout {
|
|||
IntType min;
|
||||
IntType max;
|
||||
if (lhs >> min >> std::ws >> max) {
|
||||
if (arg_check_nothrow(min, max)) {
|
||||
if (min <= max) {
|
||||
rhs.min_ = min;
|
||||
rhs.max_ = max;
|
||||
} else {
|
||||
|
@ -377,7 +364,7 @@ namespace sprout {
|
|||
, max_(9)
|
||||
{}
|
||||
explicit SPROUT_CONSTEXPR uniform_int_distribution(IntType min_arg, IntType max_arg = 9)
|
||||
: min_(arg_check(min_arg, max_arg))
|
||||
: min_((SPROUT_ASSERT(min_arg <= max_arg), min_arg))
|
||||
, max_(max_arg)
|
||||
{}
|
||||
explicit SPROUT_CONSTEXPR uniform_int_distribution(param_type const& parm)
|
||||
|
|
|
@ -3,11 +3,11 @@
|
|||
|
||||
#include <ios>
|
||||
#include <istream>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/random/detail/signed_unsigned_tools.hpp>
|
||||
#include <sprout/random/random_result.hpp>
|
||||
#include <sprout/assert.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
|
@ -47,16 +47,13 @@ namespace sprout {
|
|||
T divisor
|
||||
)
|
||||
{
|
||||
return divisor > 0
|
||||
? numerator >= 0 && numerator <= divisor
|
||||
? sprout::random::detail::generate_uniform_real_false_3(
|
||||
eng,
|
||||
min_value,
|
||||
max_value,
|
||||
numerator / divisor * (max_value - min_value) + min_value
|
||||
)
|
||||
: throw std::domain_error("generate_uniform_real(): domain error (numerator >= 0 && numerator <= divisor)")
|
||||
: throw std::domain_error("generate_uniform_real(): domain error (divisor > 0)")
|
||||
return SPROUT_ASSERT(divisor > 0), SPROUT_ASSERT(numerator >= 0 && numerator <= divisor),
|
||||
sprout::random::detail::generate_uniform_real_false_3(
|
||||
eng,
|
||||
min_value,
|
||||
max_value,
|
||||
numerator / divisor * (max_value - min_value) + min_value
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename Engine, typename T>
|
||||
|
@ -116,16 +113,13 @@ namespace sprout {
|
|||
T divisor
|
||||
)
|
||||
{
|
||||
return divisor > 0
|
||||
? numerator >= 0 && numerator <= divisor
|
||||
? sprout::random::detail::generate_uniform_real_true_3(
|
||||
eng,
|
||||
min_value,
|
||||
max_value,
|
||||
numerator / divisor * (max_value - min_value) + min_value
|
||||
)
|
||||
: throw std::domain_error("generate_uniform_real(): domain error (numerator >= 0 && numerator <= divisor)")
|
||||
: throw std::domain_error("generate_uniform_real(): domain error (divisor > 0)")
|
||||
return SPROUT_ASSERT(divisor > 0), SPROUT_ASSERT(numerator >= 0 && numerator <= divisor),
|
||||
sprout::random::detail::generate_uniform_real_true_3(
|
||||
eng,
|
||||
min_value,
|
||||
max_value,
|
||||
numerator / divisor * (max_value - min_value) + min_value
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename Engine, typename T>
|
||||
|
@ -182,16 +176,6 @@ namespace sprout {
|
|||
public:
|
||||
typedef RealType input_type;
|
||||
typedef RealType result_type;
|
||||
private:
|
||||
static SPROUT_CONSTEXPR bool arg_check_nothrow(RealType min_arg, RealType max_arg) {
|
||||
return min_arg <= max_arg;
|
||||
}
|
||||
static SPROUT_CONSTEXPR RealType arg_check(RealType min_arg, RealType max_arg) {
|
||||
return arg_check_nothrow(min_arg, max_arg)
|
||||
? min_arg
|
||||
: throw std::invalid_argument("uniform_real_distribution<>: invalid argument (min_arg <= max_arg)")
|
||||
;
|
||||
}
|
||||
public:
|
||||
//
|
||||
// param_type
|
||||
|
@ -199,10 +183,6 @@ namespace sprout {
|
|||
class param_type {
|
||||
public:
|
||||
typedef uniform_real_distribution distribution_type;
|
||||
private:
|
||||
static SPROUT_CONSTEXPR bool arg_check_nothrow(RealType min_arg, RealType max_arg) {
|
||||
return distribution_type::arg_check_nothrow(min_arg, max_arg);
|
||||
}
|
||||
private:
|
||||
RealType min_;
|
||||
RealType max_;
|
||||
|
@ -212,7 +192,7 @@ namespace sprout {
|
|||
, max_(RealType(1.0))
|
||||
{}
|
||||
explicit SPROUT_CONSTEXPR param_type(RealType min_arg, RealType max_arg = RealType(1.0))
|
||||
: min_(arg_check(min_arg, max_arg))
|
||||
: min_((SPROUT_ASSERT(min_arg <= max_arg), min_arg))
|
||||
, max_(max_arg)
|
||||
{}
|
||||
SPROUT_CONSTEXPR RealType a() const SPROUT_NOEXCEPT {
|
||||
|
@ -230,7 +210,7 @@ namespace sprout {
|
|||
RealType min;
|
||||
RealType max;
|
||||
if (lhs >> min >> std::ws >> max) {
|
||||
if (arg_check_nothrow(min, max)) {
|
||||
if (min <= max) {
|
||||
rhs.min_ = min;
|
||||
rhs.max_ = max;
|
||||
} else {
|
||||
|
@ -272,7 +252,7 @@ namespace sprout {
|
|||
, max_(RealType(1.0))
|
||||
{}
|
||||
explicit SPROUT_CONSTEXPR uniform_real_distribution(RealType min_arg, RealType max_arg = RealType(1.0))
|
||||
: min_(arg_check(min_arg, max_arg))
|
||||
: min_((SPROUT_ASSERT(min_arg <= max_arg), min_arg))
|
||||
, max_(max_arg)
|
||||
{}
|
||||
explicit SPROUT_CONSTEXPR uniform_real_distribution(param_type const& parm)
|
||||
|
|
|
@ -3,12 +3,12 @@
|
|||
|
||||
#include <iosfwd>
|
||||
#include <istream>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/random/detail/signed_unsigned_tools.hpp>
|
||||
#include <sprout/random/random_result.hpp>
|
||||
#include <sprout/random/uniform_01.hpp>
|
||||
#include <sprout/assert.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
|
@ -20,16 +20,6 @@ namespace sprout {
|
|||
public:
|
||||
typedef IntType input_type;
|
||||
typedef IntType result_type;
|
||||
private:
|
||||
static SPROUT_CONSTEXPR bool arg_check_nothrow(IntType min_arg, IntType max_arg) {
|
||||
return min_arg <= max_arg;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType arg_check(IntType min_arg, IntType max_arg) {
|
||||
return arg_check_nothrow(min_arg, max_arg)
|
||||
? min_arg
|
||||
: throw std::invalid_argument("uniform_smallint<>: invalid argument (min_arg <= max_arg)")
|
||||
;
|
||||
}
|
||||
public:
|
||||
//
|
||||
// param_type
|
||||
|
@ -37,10 +27,6 @@ namespace sprout {
|
|||
class param_type {
|
||||
public:
|
||||
typedef uniform_smallint distribution_type;
|
||||
private:
|
||||
static SPROUT_CONSTEXPR bool arg_check_nothrow(IntType min_arg, IntType max_arg) {
|
||||
return distribution_type::arg_check_nothrow(min_arg, max_arg);
|
||||
}
|
||||
private:
|
||||
IntType min_;
|
||||
IntType max_;
|
||||
|
@ -50,7 +36,7 @@ namespace sprout {
|
|||
, max_(9)
|
||||
{}
|
||||
explicit SPROUT_CONSTEXPR param_type(IntType min_arg, IntType max_arg = 9)
|
||||
: min_(arg_check(min_arg, max_arg))
|
||||
: min_((SPROUT_ASSERT(min_arg <= max_arg), min_arg))
|
||||
, max_(max_arg)
|
||||
{}
|
||||
SPROUT_CONSTEXPR IntType a() const SPROUT_NOEXCEPT {
|
||||
|
@ -68,7 +54,7 @@ namespace sprout {
|
|||
IntType min;
|
||||
IntType max;
|
||||
if (lhs >> min >> std::ws >> max) {
|
||||
if (arg_check_nothrow(min, max)) {
|
||||
if (min <= max) {
|
||||
rhs.min_ = min;
|
||||
rhs.max_ = max;
|
||||
} else {
|
||||
|
@ -207,7 +193,7 @@ namespace sprout {
|
|||
, max_(9)
|
||||
{}
|
||||
explicit SPROUT_CONSTEXPR uniform_smallint(IntType min_arg, IntType max_arg = 9)
|
||||
: min_(arg_check(min_arg, max_arg))
|
||||
: min_((SPROUT_ASSERT(min_arg <= max_arg), min_arg))
|
||||
, max_(max_arg)
|
||||
{}
|
||||
explicit SPROUT_CONSTEXPR uniform_smallint(param_type const& parm)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue