Crate num_bigint_dig

Source
Expand description

A Big integer (signed version: BigInt, unsigned version: BigUint).

A BigUint is represented as a vector of BigDigits. A BigInt is a combination of BigUint and Sign.

Common numerical operations are overloaded, so we can treat them the same way we treat other numbers.

§Example

extern crate num_bigint_dig as num_bigint;
extern crate num_traits;

use num_bigint::BigUint;
use num_traits::{Zero, One};
use std::mem::replace;

// Calculate large fibonacci numbers.
fn fib(n: usize) -> BigUint {
    let mut f0: BigUint = Zero::zero();
    let mut f1: BigUint = One::one();
    for _ in 0..n {
        let f2 = f0 + &f1;
        // This is a low cost way of swapping f0 with f1 and f1 with f2.
        f0 = replace(&mut f1, f2);
    }
    f0
}

// This is a very large number.
//println!("fib(1000) = {}", fib(1000));

It’s easy to generate large random numbers:


extern crate rand;
extern crate num_bigint_dig as bigint;

use bigint::{ToBigInt, RandBigInt};

let mut rng = rand::thread_rng();
let a = rng.gen_bigint(1000);

let low = -10000.to_bigint().unwrap();
let high = 10000.to_bigint().unwrap();
let b = rng.gen_bigint_range(&low, &high);

// Probably an even larger number.
//println!("{}", a * b);

§Compatibility

The num-bigint-dig crate is tested for rustc 1.56 and greater.

§no_std compatibility

This crate is compatible with no_std environments.

Note however that it still requires the alloc crate, so the user should ensure that they set a global_allocator.

To use in no_std environment, add the crate as such in your Cargo.toml file:

[dependencies]
num-bigint-dig = { version = "0.8", default-features=false }

Every features should be compatible with no_std environment, so feel free to add features like prime, i128, etc…

Re-exports§

pub use crate::traits::*;

Modules§

algorithms
prime
Implements probabilistic prime checkers.
traits

Structs§

BigInt
A big signed integer type.
BigUint
A big unsigned integer type.
ParseBigIntError
RandomBits
A random distribution for BigUint and BigInt values of a particular bit size.
UniformBigInt
The back-end implementing rand’s UniformSampler for BigInt.
UniformBigUint
The back-end implementing rand’s UniformSampler for BigUint.

Enums§

Sign
A Sign is a BigInt’s composing element.

Constants§

VEC_SIZE

Traits§

IntoBigInt
A generic trait for converting a value to a BigInt, consuming the value.
IntoBigUint
A generic trait for converting a value to a BigUint, and consuming the value.
RandBigInt
RandPrime
A generic trait for generating random primes.
ToBigInt
A generic trait for converting a value to a BigInt.
ToBigUint
A generic trait for converting a value to a BigUint.

Functions§

negate_sign
Negates the sign of BigInt.