Find
C
ase-sensitive
R
egexp search
Path
mozilla-central
/
third_party
/
rust
/
num-bigint
/
src
Navigation
Enable keyboard shortcuts
Name
Description
Size
bigint
bigint.rs
36441
bigrand.rs
Randomization of big integers
8686
biguint
biguint.rs
34848
lib.rs
Big Integer Types for Rust * A [`BigUint`] is unsigned and represented as a vector of digits. * A [`BigInt`] is signed and 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 ```rust # fn main() { use num_bigint::BigUint; use num_traits::One; // Calculate large fibonacci numbers. fn fib(n: usize) -> BigUint { let mut f0 = BigUint::ZERO; let mut f1 = BigUint::one(); for _ in 0..n { let f2 = f0 + &f1; f0 = f1; f1 = f2; } f0 } // This is a very large number. println!("fib(1000) = {}", fib(1000)); # } ``` It's easy to generate large random numbers: ```rust,ignore use num_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); ``` See the "Features" section for instructions for enabling random number generation. ## Features The `std` crate feature is enabled by default, which enables [`std::error::Error`] implementations and some internal use of floating point approximations. This can be disabled by depending on `num-bigint` with `default-features = false`. Either way, the `alloc` crate is always required for heap allocation of the `BigInt`/`BigUint` digits. ### Random Generation `num-bigint` supports the generation of random big integers when the `rand` feature is enabled. To enable it include rand as ```toml rand = "0.8" num-bigint = { version = "0.4", features = ["rand"] } ``` Note that you must use the version of `rand` that `num-bigint` is compatible with: `0.8`. ### Arbitrary Big Integers `num-bigint` supports `arbitrary` and `quickcheck` features to implement [`arbitrary::Arbitrary`] and [`quickcheck::Arbitrary`], respectively, for both `BigInt` and `BigUint`. These are useful for fuzzing and other forms of randomized testing. ### Serialization The `serde` feature adds implementations of [`Serialize`][serde::Serialize] and [`Deserialize`][serde::Deserialize] for both `BigInt` and `BigUint`. Their serialized data is generated portably, regardless of platform differences like the internal digit size. ## Compatibility The `num-bigint` crate is tested for rustc 1.60 and greater.
7522
macros.rs
13669