Module num::bigintExperimental
[-]
[+]
[src]
A Big integer (signed version: BigInt
, unsigned version: BigUint
).
A BigUint
is represented as an array 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; fn main() { use num::{BigUint, Zero, One}; use std::mem::replace; // Calculate large fibonacci numbers. fn fib(n: uint) -> BigUint { let mut f0: BigUint = Zero::zero(); let mut f1: BigUint = One::one(); for _ in range(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)); }use num::{BigUint, Zero, One}; use std::mem::replace; // Calculate large fibonacci numbers. fn fib(n: uint) -> BigUint { let mut f0: BigUint = Zero::zero(); let mut f1: BigUint = One::one(); for _ in range(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 num; fn main() { use num::bigint::{ToBigInt, RandBigInt}; use std::rand; let mut rng = rand::task_rng(); let a = rng.gen_bigint(1000u); let low = -10000i.to_bigint().unwrap(); let high = 10000i.to_bigint().unwrap(); let b = rng.gen_bigint_range(&low, &high); // Probably an even larger number. println!("{}", a * b); }use num::bigint::{ToBigInt, RandBigInt}; use std::rand; let mut rng = rand::task_rng(); let a = rng.gen_bigint(1000u); let low = -10000i.to_bigint().unwrap(); let high = 10000i.to_bigint().unwrap(); let b = rng.gen_bigint_range(&low, &high); // Probably an even larger number. println!("{}", a * b);
Modules
BigDigit |
Structs
BigInt | A big signed integer type. |
BigUint | A big unsigned integer type. |
Enums
Sign | A Sign is a |
Constants
ZERO_BIG_DIGIT |
Traits
RandBigInt | |
ToBigInt | A generic trait for converting a value to a |
ToBigUint | A generic trait for converting a value to a |
Type Definitions
BigDigit | A |
DoubleBigDigit | A |