Struct num::BigInt
[−]
[src]
pub struct BigInt { // some fields omitted }
A big signed integer type.
Methods
impl BigInt
fn new(sign: Sign, digits: Vec<u32>) -> BigInt
Creates and initializes a BigInt.
The digits are in little-endian base 232.
fn from_biguint(sign: Sign, data: BigUint) -> BigInt
Creates and initializes a BigInt
.
The digits are in little-endian base 232.
fn from_slice(sign: Sign, slice: &[u32]) -> BigInt
Creates and initializes a BigInt
.
fn from_bytes_be(sign: Sign, bytes: &[u8]) -> BigInt
Creates and initializes a BigInt
.
The bytes are in big-endian byte order.
Examples
extern crate num; fn main() { use num_bigint::{BigInt, Sign}; assert_eq!(BigInt::from_bytes_be(Sign::Plus, b"A"), BigInt::parse_bytes(b"65", 10).unwrap()); assert_eq!(BigInt::from_bytes_be(Sign::Plus, b"AA"), BigInt::parse_bytes(b"16705", 10).unwrap()); assert_eq!(BigInt::from_bytes_be(Sign::Plus, b"AB"), BigInt::parse_bytes(b"16706", 10).unwrap()); assert_eq!(BigInt::from_bytes_be(Sign::Plus, b"Hello world!"), BigInt::parse_bytes(b"22405534230753963835153736737", 10).unwrap()); }use num_bigint::{BigInt, Sign}; assert_eq!(BigInt::from_bytes_be(Sign::Plus, b"A"), BigInt::parse_bytes(b"65", 10).unwrap()); assert_eq!(BigInt::from_bytes_be(Sign::Plus, b"AA"), BigInt::parse_bytes(b"16705", 10).unwrap()); assert_eq!(BigInt::from_bytes_be(Sign::Plus, b"AB"), BigInt::parse_bytes(b"16706", 10).unwrap()); assert_eq!(BigInt::from_bytes_be(Sign::Plus, b"Hello world!"), BigInt::parse_bytes(b"22405534230753963835153736737", 10).unwrap());
fn from_bytes_le(sign: Sign, bytes: &[u8]) -> BigInt
Creates and initializes a BigInt
.
The bytes are in little-endian byte order.
fn to_bytes_le(&self) -> (Sign, Vec<u8>)
Returns the sign and the byte representation of the BigInt
in little-endian byte order.
Examples
extern crate num; fn main() { use num_bigint::{ToBigInt, Sign}; let i = -1125.to_bigint().unwrap(); assert_eq!(i.to_bytes_le(), (Sign::Minus, vec![101, 4])); }use num_bigint::{ToBigInt, Sign}; let i = -1125.to_bigint().unwrap(); assert_eq!(i.to_bytes_le(), (Sign::Minus, vec![101, 4]));
fn to_bytes_be(&self) -> (Sign, Vec<u8>)
Returns the sign and the byte representation of the BigInt
in big-endian byte order.
Examples
extern crate num; fn main() { use num_bigint::{ToBigInt, Sign}; let i = -1125.to_bigint().unwrap(); assert_eq!(i.to_bytes_be(), (Sign::Minus, vec![4, 101])); }use num_bigint::{ToBigInt, Sign}; let i = -1125.to_bigint().unwrap(); assert_eq!(i.to_bytes_be(), (Sign::Minus, vec![4, 101]));
fn to_str_radix(&self, radix: u32) -> String
Returns the integer formatted as a string in the given radix.
radix
must be in the range [2, 36]
.
Examples
extern crate num; fn main() { use num_bigint::BigInt; let i = BigInt::parse_bytes(b"ff", 16).unwrap(); assert_eq!(i.to_str_radix(16), "ff"); }use num_bigint::BigInt; let i = BigInt::parse_bytes(b"ff", 16).unwrap(); assert_eq!(i.to_str_radix(16), "ff");
fn sign(&self) -> Sign
Returns the sign of the BigInt
as a Sign
.
Examples
extern crate num; fn main() { use num_bigint::{ToBigInt, Sign}; assert_eq!(ToBigInt::to_bigint(&1234).unwrap().sign(), Sign::Plus); assert_eq!(ToBigInt::to_bigint(&-4321).unwrap().sign(), Sign::Minus); assert_eq!(ToBigInt::to_bigint(&0).unwrap().sign(), Sign::NoSign); }use num_bigint::{ToBigInt, Sign}; assert_eq!(ToBigInt::to_bigint(&1234).unwrap().sign(), Sign::Plus); assert_eq!(ToBigInt::to_bigint(&-4321).unwrap().sign(), Sign::Minus); assert_eq!(ToBigInt::to_bigint(&0).unwrap().sign(), Sign::NoSign);
fn parse_bytes(buf: &[u8], radix: u32) -> Option<BigInt>
Creates and initializes a BigInt
.
Examples
extern crate num; fn main() { use num_bigint::{BigInt, ToBigInt}; assert_eq!(BigInt::parse_bytes(b"1234", 10), ToBigInt::to_bigint(&1234)); assert_eq!(BigInt::parse_bytes(b"ABCD", 16), ToBigInt::to_bigint(&0xABCD)); assert_eq!(BigInt::parse_bytes(b"G", 16), None); }use num_bigint::{BigInt, ToBigInt}; assert_eq!(BigInt::parse_bytes(b"1234", 10), ToBigInt::to_bigint(&1234)); assert_eq!(BigInt::parse_bytes(b"ABCD", 16), ToBigInt::to_bigint(&0xABCD)); assert_eq!(BigInt::parse_bytes(b"G", 16), None);
fn bits(&self) -> usize
Determines the fewest bits necessary to express the BigInt
,
not including the sign.
fn to_biguint(&self) -> Option<BigUint>
Converts this BigInt
into a BigUint
, if it's not negative.
fn checked_add(&self, v: &BigInt) -> Option<BigInt>
fn checked_sub(&self, v: &BigInt) -> Option<BigInt>
fn checked_mul(&self, v: &BigInt) -> Option<BigInt>
fn checked_div(&self, v: &BigInt) -> Option<BigInt>
Trait Implementations
impl Hash for BigInt
impl Debug for BigInt
impl Clone for BigInt
impl Decodable for BigInt
impl Encodable for BigInt
impl PartialEq<BigInt> for BigInt
impl Eq for BigInt
impl PartialOrd<BigInt> for BigInt
fn partial_cmp(&self, other: &BigInt) -> Option<Ordering>
impl Ord for BigInt
impl Default for BigInt
impl Display for BigInt
impl Binary for BigInt
impl Octal for BigInt
impl LowerHex for BigInt
impl UpperHex for BigInt
impl FromStr for BigInt
type Err = ParseBigIntError
fn from_str(s: &str) -> Result<BigInt, ParseBigIntError>
impl Num for BigInt
type FromStrRadixErr = ParseBigIntError
fn from_str_radix(s: &str, radix: u32) -> Result<BigInt, ParseBigIntError>
Creates and initializes a BigInt.
impl Shl<usize> for BigInt
impl<'a> Shl<usize> for &'a BigInt
impl Shr<usize> for BigInt
impl<'a> Shr<usize> for &'a BigInt
impl Zero for BigInt
fn zero() -> BigInt
Returns the additive identity element of Self
, 0
. Read more
fn is_zero(&self) -> bool
Returns true
if self
is equal to the additive identity.
impl One for BigInt
impl Signed for BigInt
fn abs(&self) -> BigInt
Computes the absolute value. Read more
fn abs_sub(&self, other: &BigInt) -> BigInt
The positive difference of two numbers. Read more
fn signum(&self) -> BigInt
Returns the sign of the number. Read more
fn is_positive(&self) -> bool
Returns true if the number is positive and false if the number is zero or negative.
fn is_negative(&self) -> bool
Returns true if the number is negative and false if the number is zero or positive.
impl<'a, 'b> Add<&'b BigInt> for &'a BigInt
impl<'a> Add<BigInt> for &'a BigInt
impl<'a> Add<&'a BigInt> for BigInt
impl Add<BigInt> for BigInt
impl<'a, 'b> Sub<&'b BigInt> for &'a BigInt
impl<'a> Sub<BigInt> for &'a BigInt
impl<'a> Sub<&'a BigInt> for BigInt
impl Sub<BigInt> for BigInt
impl Mul<BigInt> for BigInt
impl<'a> Mul<&'a BigInt> for BigInt
impl<'a> Mul<BigInt> for &'a BigInt
impl<'a, 'b> Mul<&'b BigInt> for &'a BigInt
impl Div<BigInt> for BigInt
impl<'a> Div<&'a BigInt> for BigInt
impl<'a> Div<BigInt> for &'a BigInt
impl<'a, 'b> Div<&'b BigInt> for &'a BigInt
impl Rem<BigInt> for BigInt
impl<'a> Rem<&'a BigInt> for BigInt
impl<'a> Rem<BigInt> for &'a BigInt
impl<'a, 'b> Rem<&'b BigInt> for &'a BigInt
impl Neg for BigInt
impl<'a> Neg for &'a BigInt
impl CheckedAdd for BigInt
fn checked_add(&self, v: &BigInt) -> Option<BigInt>
Adds two numbers, checking for overflow. If overflow happens, None
is returned. Read more
impl CheckedSub for BigInt
fn checked_sub(&self, v: &BigInt) -> Option<BigInt>
Subtracts two numbers, checking for underflow. If underflow happens, None
is returned. Read more
impl CheckedMul for BigInt
fn checked_mul(&self, v: &BigInt) -> Option<BigInt>
Multiplies two numbers, checking for underflow or overflow. If underflow or overflow happens, None
is returned. Read more
impl CheckedDiv for BigInt
fn checked_div(&self, v: &BigInt) -> Option<BigInt>
Divides two numbers, checking for underflow, overflow and division by zero. If any of that happens, None
is returned. Read more
impl Integer for BigInt
fn div_rem(&self, other: &BigInt) -> (BigInt, BigInt)
Simultaneous truncated integer division and modulus. Returns (quotient, remainder)
. Read more
fn div_floor(&self, other: &BigInt) -> BigInt
Floored integer division. Read more
fn mod_floor(&self, other: &BigInt) -> BigInt
Floored integer modulo, satisfying: Read more
fn div_mod_floor(&self, other: &BigInt) -> (BigInt, BigInt)
Simultaneous floored integer division and modulus. Returns (quotient, remainder)
. Read more
fn gcd(&self, other: &BigInt) -> BigInt
Calculates the Greatest Common Divisor (GCD) of the number and other
.
The result is always positive.
fn lcm(&self, other: &BigInt) -> BigInt
Calculates the Lowest Common Multiple (LCM) of the number and other
.
fn divides(&self, other: &BigInt) -> bool
Deprecated, use is_multiple_of
instead.
fn is_multiple_of(&self, other: &BigInt) -> bool
Returns true
if the number is a multiple of other
.
fn is_even(&self) -> bool
Returns true
if the number is divisible by 2
.
fn is_odd(&self) -> bool
Returns true
if the number is not divisible by 2
.
impl ToPrimitive for BigInt
fn to_i64(&self) -> Option<i64>
Converts the value of self
to an i64
.
fn to_u64(&self) -> Option<u64>
Converts the value of self
to an u64
.
fn to_f32(&self) -> Option<f32>
Converts the value of self
to an f32
.
fn to_f64(&self) -> Option<f64>
Converts the value of self
to an f64
.
fn to_isize(&self) -> Option<isize>
Converts the value of self
to an isize
.
fn to_i8(&self) -> Option<i8>
Converts the value of self
to an i8
.
fn to_i16(&self) -> Option<i16>
Converts the value of self
to an i16
.
fn to_i32(&self) -> Option<i32>
Converts the value of self
to an i32
.
fn to_usize(&self) -> Option<usize>
Converts the value of self
to a usize
.
fn to_u8(&self) -> Option<u8>
Converts the value of self
to an u8
.
fn to_u16(&self) -> Option<u16>
Converts the value of self
to an u16
.
fn to_u32(&self) -> Option<u32>
Converts the value of self
to an u32
.
impl FromPrimitive for BigInt
fn from_i64(n: i64) -> Option<BigInt>
Convert an i64
to return an optional value of this type. If the type cannot be represented by this value, the None
is returned. Read more
fn from_u64(n: u64) -> Option<BigInt>
Convert an u64
to return an optional value of this type. If the type cannot be represented by this value, the None
is returned. Read more
fn from_f64(n: f64) -> Option<BigInt>
Convert a f64
to return an optional value of this type. If the type cannot be represented by this value, the None
is returned. Read more
fn from_isize(n: isize) -> Option<Self>
Convert an isize
to return an optional value of this type. If the value cannot be represented by this value, the None
is returned. Read more
fn from_i8(n: i8) -> Option<Self>
Convert an i8
to return an optional value of this type. If the type cannot be represented by this value, the None
is returned. Read more
fn from_i16(n: i16) -> Option<Self>
Convert an i16
to return an optional value of this type. If the type cannot be represented by this value, the None
is returned. Read more
fn from_i32(n: i32) -> Option<Self>
Convert an i32
to return an optional value of this type. If the type cannot be represented by this value, the None
is returned. Read more
fn from_usize(n: usize) -> Option<Self>
Convert a usize
to return an optional value of this type. If the type cannot be represented by this value, the None
is returned. Read more
fn from_u8(n: u8) -> Option<Self>
Convert an u8
to return an optional value of this type. If the type cannot be represented by this value, the None
is returned. Read more
fn from_u16(n: u16) -> Option<Self>
Convert an u16
to return an optional value of this type. If the type cannot be represented by this value, the None
is returned. Read more
fn from_u32(n: u32) -> Option<Self>
Convert an u32
to return an optional value of this type. If the type cannot be represented by this value, the None
is returned. Read more
fn from_f32(n: f32) -> Option<Self>
Convert a f32
to return an optional value of this type. If the type cannot be represented by this value, the None
is returned. Read more