9 if (
value.is_negative()) {
13 const auto converted =
value.try_to_uint64();
14 if (!converted || *converted > std::numeric_limits<unsigned int>::max()) {
18 return static_cast<unsigned int>(*converted);
27 const auto factor_bits = largest_factor.bit_length();
28 if (factors != 0 && factor_bits > std::numeric_limits<std::size_t>::max() / factors) {
30 "combinatorial bit bound overflows", operation);
32 return context.require_integer_bits(factor_bits * factors, operation);
36 const char* operation) {
37 auto access =
context.consume_steps(0, operation);
41 auto bits =
context.require_integer_bits(
value.bit_length(), operation);
50 return std::move(result.
value());
63Result<BigInt> BigInt::factorial_checked(
const BigInt& n,
64 ComputationContext&
context)
try {
65 constexpr auto operation =
"BigInt::factorial_checked";
66 auto access =
context.consume_steps(1, operation);
74 if (argument.value() <= 1) {
82 mp_bitcnt_t binary_shift = 0;
83 const auto limbs = lmmp_factorial_size_(argument.value(), &binary_shift);
85 result.realloc_to(limbs);
86 result.size_ = lmmp_factorial_(result.data_, binary_shift, limbs, argument.value());
87 result.sign_ = POSITIVE;
90}
catch (
const std::bad_alloc&) {
92 "factorial allocation failed",
"BigInt::factorial_checked");
93}
catch (
const std::length_error& error) {
99 constexpr auto operation =
"BigInt::npr_checked";
100 auto access =
context.consume_steps(1, operation);
112 if (rank.value() > total.value()) {
115 if (rank.value() == 0) {
123 mp_bitcnt_t binary_shift = 0;
124 const auto limbs = lmmp_nPr_size_(total.value(), rank.value(), &binary_shift);
126 result.realloc_to(limbs);
127 result.size_ = lmmp_nPr_(result.data_, binary_shift, limbs, total.value(), rank.value());
128 result.sign_ = POSITIVE;
131}
catch (
const std::bad_alloc&) {
133 "permutation allocation failed",
"BigInt::npr_checked");
134}
catch (
const std::length_error& error) {
140 constexpr auto operation =
"BigInt::ncr_checked";
141 auto access =
context.consume_steps(1, operation);
153 if (rank.value() > total.value()) {
156 const unsigned int reduced_rank = std::min(rank.value(), total.value() - rank.value());
157 if (reduced_rank == 0) {
165 mp_bitcnt_t binary_shift = 0;
166 const auto limbs = lmmp_nCr_size_(total.value(), reduced_rank, &binary_shift);
168 result.realloc_to(limbs);
169 result.size_ = lmmp_nCr_(result.data_, binary_shift, limbs, total.value(), reduced_rank);
170 result.sign_ = POSITIVE;
173}
catch (
const std::bad_alloc&) {
175 "combination allocation failed",
"BigInt::ncr_checked");
176}
catch (
const std::length_error& error) {
182 return factorial_checked(n,
context);
195BigInt BigInt::factorial(
const BigInt& n) {
199BigInt BigInt::npr(
const BigInt& n,
const BigInt&
r) {
203BigInt BigInt::ncr(
const BigInt& n,
const BigInt&
r) {
任意精度整数 BigInt,底层调用 LMMP 实现高性能大数运算。
static Result failure(CasErrc code, std::string message, std::string operation={})
static Result success(T value)
BigInt require_combinatorial(Result< BigInt > result)
Result< BigInt > finish_combinatorial(BigInt value, ComputationContext &context, const char *operation)
Result< unsigned int > kernel_argument(const BigInt &value, const char *operation)
Result< void > reserve_product(const BigInt &largest_factor, unsigned int factors, ComputationContext &context, const char *operation)
void ensure_lmmc_lifecycle() noexcept
ComputationContext const std::string & operation
__attribute__((visibility("default"))) ComplexSymbolicResult complex_exp_form_checked(std __attribute__((visibility("default"))) ComplexSymbolicResult complex_exp_form_checked(std __attribute__((visibility("default"))) ComplexSymbolicResult complex_trig_form_checked(std __attribute__((visibility("default"))) ComplexSymbolicResult complex_trig_form_checked(std __attribute__((visibility("default"))) ComplexRootsResult solve_complex_nth_root_checked(std __attribute__((visibility("default"))) ComplexRootsResult solve_complex_nth_root_checked(std __attribute__((visibility("default"))) ComplexRootsResult solve_complex_quadratic_checked(std __attribute__((visibility("default"))) ComplexRootsResult solve_complex_quadratic_checked(std std::shared_ptr< SymbolicExpr > r
const std::vector< Polynomial< BigInt > > const BigInt ComputationContext & context
const std::string const ExprPtr & value