Crate num_bigint[−][src]
A Big integer (signed version: BigInt
, unsigned version: BigUint
).
A BigUint
is represented as a vector of BigDigit
s.
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; 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 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);
See the “Features” section for instructions for enabling random number generation.
Features
The std
crate feature is mandatory and enabled by default. If you depend on
num-bigint
with default-features = false
, you must manually enable the
std
feature yourself. In the future, we hope to support #![no_std]
with
the alloc
crate when std
is not enabled.
Implementations for i128
and u128
are only available with Rust 1.26 and
later. The build script automatically detects this, but you can make it
mandatory by enabling the i128
crate feature.
Random Generation
num-bigint
supports the generation of random big integers when the rand
feature is enabled. To enable it include rand as
rand = "0.5"
num-bigint = { version = "0.2", features = ["rand"] }
Note that you must use the version of rand
that num-bigint
is compatible
with: 0.5
.
Compatibility
The num-bigint
crate is tested for rustc 1.15 and greater.
Structs
BigInt | A big signed integer type. |
BigUint | A big unsigned integer type. |
ParseBigIntError |
Enums
Sign | A Sign is a |
Traits
ToBigInt | A generic trait for converting a value to a |
ToBigUint | A generic trait for converting a value to a |