Struct rugint::Integer [] [src]

pub struct Integer { /* fields omitted */ }

An arbitrary-precision integer.

Standard arithmetic operations, bitwise operations and comparisons are supported. In standard arithmetic operations such as addition, you can mix Integer and primitive integer types; the result will be an Integer.

Internally the integer is not stored using two's-complement representation, however, for bitwise operations and shifts, the functionality is the same as if the representation was using two's complement.

Examples

use rugint::Integer;

let mut i = Integer::from(1) << 1000;
// i is now 1000000... (1000 zeros)
assert!(i.significant_bits() == 1001);
assert!(i.find_one(0) == Some(1000));
i -= 1;
// i is now 111111... (1000 ones)
assert!(i.count_ones() == Some(1000));

let a = Integer::from(0xf00d);
let all_ones_xor_a = Integer::from(-1) ^ &a;
// a is unchanged as we borrowed it
let complement_a = !a;
// now a has been moved, so this would cause an error:
// assert!(a > 0);
assert!(all_ones_xor_a == complement_a);
assert!(complement_a == -0xf00e);
assert!(format!("{:x}", complement_a) == "-f00e");

Methods

impl Integer
[src]

Constructs a new arbitrary-precision integer with value 0.

Converts to a u32. If the value is too large for the target type, only the least-significant bits are returned.

Converts to an i32. If the value is too large for the target type, only the least-significant bits are returned.

Converts to an f64 rounding towards zero.

Converts to an f32 rounding towards zero.

Computes the quotient and remainder of self divided by divisor. The remainder is stored indivisor`.

Computes the absolute value of self.

Divides self by other. This is much faster than normal division, but produces correct results only when the division is exact.

Panics

Panics if other is zero.

Returns true if self is divisible by other.

Returns true if self is congruent to c modulo d, that is, if there exists a q such that self == c + q * d. Unlike other division functions, d can be zero.

Computes the nth root of self and truncates the result.

Computes the nth root of self and returns the truncated root and the remainder. The remainder is self minus the truncated root raised to the power of n. The remainder is stored in buf.

Computes the square root of self and truncates the result.

Computes the square root of self and returns the truncated root and the remainder. The remainder is self minus the truncated root squared. The remainder is stored in buf.

Returns true if self is a perfect power.

Returns true if self is a perfect square.

Finds the greatest common divisor. The result is always positive except when both inputs are zero.

Finds the least common multiple. The result is always positive except when one or both inputs are zero.

Finds the inverse of self modulo m if an inverse exists.

Panics

Panics if m is zero.

Computes the factorial of n. The value of self is ignored.

Computes the double factorial of n. The value of self is ignored.

Computes the m-multi factorial of n. The value of self is ignored.

Computes the primorial of n. The value of self is ignored.

Computes the binomial coefficient self over k.

Computes the binomial coefficient n over k. The value of self is ignored.

Compares the absolute values of self and other.

Returns the same result as self.cmp(0), but is faster.

Returns the number of bits required to represent the absolute value of self.

Examples

use rugint::Integer;

let i = Integer::from(0);
assert!(i.significant_bits() == 0);
let i = Integer::from(4);
assert!(i.significant_bits() == 3);
let i = Integer::from(7);
assert!(i.significant_bits() == 3);

Returns the number of ones in self if the value >= 0.

Retuns the Hamming distance between self and other if they have the same sign, otherwise None.

Returns the location of the first zero, starting at start. If the bit at location start is zero, returns start.

Returns the location of the first one, starting at start. If the bit at location start is one, returns start.

Sets the bit at location index to 1 if val is true or 0 if val is false.

Returns true if the bit at location index is 1 or false if the bit is 0.

Toggles the bit at location index.

Generates a random number consisting of the required number of bits.

Examples

extern crate rand;
extern crate rugint;
use rugint::Integer;
fn main() {
    let mut rng = rand::thread_rng();
    let mut i = Integer::new();
    i.random_bits(0, &mut rng);
    assert!(i == 0);
    i.random_bits(80, &mut rng);
    assert!(i.significant_bits() <= 80);
}

Generates a non-negative random number below the given value.

Examples

extern crate rand;
extern crate rugint;
use rugint::Integer;
fn main() {
    let mut rng = rand::thread_rng();
    let bound = Integer::from(15);
    let mut random = bound.clone();
    random.random_below(&mut rng);
    println!("0 <= {} < {}", random, bound);
    assert!(random < bound);
}

Panics

Panics if self is less than or equal to zero.

Returns a string representation of self for the specified radix.

Panics

Panics if radix is less than 2 or greater than 36.

Parses an Integer.

See the corresponding assignment.

Panics

Panics if radix is less than 2 or greater than 36.

Parses an Integer from a string.

Examples

use rugint::Integer;
let mut i = Integer::new();
i.assign_str("123").unwrap();
assert!(i == 123);
let ret = i.assign_str("bad");
assert!(ret.is_err());

Parses an Integer from a string with the specified radix.

Examples

use rugint::Integer;
let mut i = Integer::new();
i.assign_str_radix("ff", 16).unwrap();
assert!(i == 255);

Panics

Panics if radix is less than 2 or greater than 36.

Trait Implementations

impl Drop for Integer
[src]

A method called when the value goes out of scope. Read more

impl Default for Integer
[src]

Returns the "default value" for a type. Read more

impl Clone for Integer
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl FromStr for Integer
[src]

The associated error which can be returned from parsing.

Parses an Integer.

See the corresponding assignment.

impl<'a> From<&'a Integer> for Integer
[src]

Constructs an Integer from another Integer.

impl From<u32> for Integer
[src]

Constructs an Integer from a u32.

impl From<i32> for Integer
[src]

Constructs an Integer from an i32.

impl<'a> Assign<&'a Integer> for Integer
[src]

Assigns from another Integer.

impl<'a> Assign<Integer> for Integer
[src]

Assigns from another Integer.

impl Assign<u32> for Integer
[src]

Assigns from a u32.

impl Assign<i32> for Integer
[src]

Assigns from an i32.

impl Assign<f64> for Integer
[src]

Assigns from an f64, rounding towards zero.

impl Assign<f32> for Integer
[src]

Assigns from an f32, rounding towards zero.

impl<'a> Add<&'a Integer> for Integer
[src]

The resulting type after applying the + operator

The method for the + operator

impl Add<Integer> for Integer
[src]

The resulting type after applying the + operator

The method for the + operator

impl<'a> AddAssign<&'a Integer> for Integer
[src]

The method for the += operator

impl AddAssign<Integer> for Integer
[src]

The method for the += operator

impl<'a> Sub<&'a Integer> for Integer
[src]

The resulting type after applying the - operator

The method for the - operator

impl Sub<Integer> for Integer
[src]

The resulting type after applying the - operator

The method for the - operator

impl<'a> SubAssign<&'a Integer> for Integer
[src]

The method for the -= operator

impl SubAssign<Integer> for Integer
[src]

The method for the -= operator

impl<'a> Mul<&'a Integer> for Integer
[src]

The resulting type after applying the * operator

The method for the * operator

impl Mul<Integer> for Integer
[src]

The resulting type after applying the * operator

The method for the * operator

impl<'a> MulAssign<&'a Integer> for Integer
[src]

The method for the *= operator

impl MulAssign<Integer> for Integer
[src]

The method for the *= operator

impl<'a> Div<&'a Integer> for Integer
[src]

The resulting type after applying the / operator

The method for the / operator

impl Div<Integer> for Integer
[src]

The resulting type after applying the / operator

The method for the / operator

impl<'a> DivAssign<&'a Integer> for Integer
[src]

The method for the /= operator

impl DivAssign<Integer> for Integer
[src]

The method for the /= operator

impl<'a> Rem<&'a Integer> for Integer
[src]

The resulting type after applying the % operator

The method for the % operator

impl Rem<Integer> for Integer
[src]

The resulting type after applying the % operator

The method for the % operator

impl<'a> RemAssign<&'a Integer> for Integer
[src]

The method for the %= operator

impl RemAssign<Integer> for Integer
[src]

The method for the %= operator

impl SubFromAssign for Integer
[src]

Peforms the subtraction.

impl<'a> SubFromAssign<&'a Integer> for Integer
[src]

Peforms the subtraction.

impl DivFromAssign for Integer
[src]

Peforms the division.

impl<'a> DivFromAssign<&'a Integer> for Integer
[src]

Peforms the division.

impl Add<u32> for Integer
[src]

The resulting type after applying the + operator

The method for the + operator

impl AddAssign<u32> for Integer
[src]

The method for the += operator

impl Sub<u32> for Integer
[src]

The resulting type after applying the - operator

The method for the - operator

impl SubAssign<u32> for Integer
[src]

The method for the -= operator

impl SubFromAssign<u32> for Integer
[src]

Peforms the subtraction.

impl Mul<u32> for Integer
[src]

The resulting type after applying the * operator

The method for the * operator

impl MulAssign<u32> for Integer
[src]

The method for the *= operator

impl Mul<i32> for Integer
[src]

The resulting type after applying the * operator

The method for the * operator

impl MulAssign<i32> for Integer
[src]

The method for the *= operator

impl Div<u32> for Integer
[src]

The resulting type after applying the / operator

The method for the / operator

impl DivAssign<u32> for Integer
[src]

The method for the /= operator

impl Rem<u32> for Integer
[src]

The resulting type after applying the % operator

The method for the % operator

impl RemAssign<u32> for Integer
[src]

The method for the %= operator

impl Shl<u32> for Integer
[src]

The resulting type after applying the << operator

The method for the << operator

impl ShlAssign<u32> for Integer
[src]

The method for the <<= operator

impl Shr<u32> for Integer
[src]

The resulting type after applying the >> operator

The method for the >> operator

impl ShrAssign<u32> for Integer
[src]

The method for the >>= operator

impl Pow<u32> for Integer
[src]

The resulting type after the power operation.

Performs the power operation.

impl PowAssign<u32> for Integer
[src]

Peforms the power operation.

impl Neg for Integer
[src]

The resulting type after applying the - operator

The method for the unary - operator

impl NegAssign for Integer
[src]

Peforms the negation.

impl Eq for Integer
[src]

impl Ord for Integer
[src]

This method returns an Ordering between self and other. Read more

impl PartialEq for Integer
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl PartialOrd for Integer
[src]

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl PartialOrd<f64> for Integer
[src]

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl PartialEq<f64> for Integer
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl PartialOrd<f32> for Integer
[src]

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl PartialEq<f32> for Integer
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl PartialOrd<u32> for Integer
[src]

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl PartialEq<u32> for Integer
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl PartialOrd<i32> for Integer
[src]

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl PartialEq<i32> for Integer
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<'a> BitAnd<&'a Integer> for Integer
[src]

The resulting type after applying the & operator

The method for the & operator

impl BitAnd for Integer
[src]

The resulting type after applying the & operator

The method for the & operator

impl<'a> BitAndAssign<&'a Integer> for Integer
[src]

The method for the &= operator

impl BitAndAssign<Integer> for Integer
[src]

The method for the &= operator

impl<'a> BitOr<&'a Integer> for Integer
[src]

The resulting type after applying the | operator

The method for the | operator

impl BitOr for Integer
[src]

The resulting type after applying the | operator

The method for the | operator

impl<'a> BitOrAssign<&'a Integer> for Integer
[src]

The method for the |= operator

impl BitOrAssign<Integer> for Integer
[src]

The method for the |= operator

impl<'a> BitXor<&'a Integer> for Integer
[src]

The resulting type after applying the ^ operator

The method for the ^ operator

impl BitXor for Integer
[src]

The resulting type after applying the ^ operator

The method for the ^ operator

impl<'a> BitXorAssign<&'a Integer> for Integer
[src]

The method for the ^= operator

impl BitXorAssign<Integer> for Integer
[src]

The method for the ^= operator

impl Not for Integer
[src]

The resulting type after applying the ! operator

The method for the unary ! operator

impl NotAssign for Integer
[src]

Peforms the complement.

impl Display for Integer
[src]

Formats the value using the given formatter.

impl Debug for Integer
[src]

Formats the value using the given formatter.

impl Binary for Integer
[src]

Formats the value using the given formatter.

impl Octal for Integer
[src]

Formats the value using the given formatter.

impl LowerHex for Integer
[src]

Formats the value using the given formatter.

impl UpperHex for Integer
[src]

Formats the value using the given formatter.