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);
i = i << 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 fits.

Converts to a u32, wrapping if the value is too large.

Converts to an i32 if the value fits.

Converts to an i32, wrapping if the value is too large.

Converts to an f64, rounding towards zero.

Converts to an f32, rounding towards zero.

Computes the quotient and remainder of self divided by `divisor.

Panics

Panics if divisor is zero.

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;

assert!(Integer::from(0).significant_bits() == 0);
assert!(Integer::from(1).significant_bits() == 1);
assert!(Integer::from(-1).significant_bits() == 1);
assert!(Integer::from(4).significant_bits() == 3);
assert!(Integer::from(-4).significant_bits() == 3);
assert!(Integer::from(7).significant_bits() == 3);
assert!(Integer::from(-7).significant_bits() == 3);

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

Examples

use rugint::Integer;
assert!(Integer::from(0).count_ones() == Some(0));
assert!(Integer::from(15).count_ones() == Some(4));
assert!(Integer::from(-1).count_ones() == None);

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

Examples

use rugint::Integer;
let i = Integer::from(-1);
assert!(Integer::from(0).ham_dist(&i) == None);
assert!(Integer::from(-1).ham_dist(&i) == Some(0));
assert!(Integer::from(-13).ham_dist(&i) == Some(2));

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

use rugint::Integer;
assert!(Integer::from(-2).find_zero(0) == Some(0));
assert!(Integer::from(-2).find_zero(1) == None);
assert!(Integer::from(15).find_zero(0) == Some(4));
assert!(Integer::from(15).find_zero(20) == Some(20));

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

use rugint::Integer;
assert!(Integer::from(1).find_one(0) == Some(0));
assert!(Integer::from(1).find_one(1) == None);
assert!(Integer::from(-16).find_one(0) == Some(4));
assert!(Integer::from(-16).find_one(20) == Some(20));

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 with a specified maximum 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.assign_random_bits(0, &mut rng);
    assert!(i == 0);
    i.assign_random_bits(80, &mut rng);
    assert!(i.significant_bits() <= 80);
}

Generates a non-negative random number below the given boundary 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 the boundary value is less than or equal to zero.

Returns a string representation of self for the specified radix.

Examples

use rugint::{Assign, Integer};
let mut i = Integer::new();
assert!(i.to_string_radix(10) == "0");
i.assign(-10);
assert!(i.to_string_radix(16) == "-a");
i.assign(0x1234cdef);
assert!(i.to_string_radix(4) == "102031030313233");
i.assign_str_radix("1234567890aAbBcCdDeEfF", 16).unwrap();
assert!(i.to_string_radix(16) == "1234567890aabbccddeeff");

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 in decimal.

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.

Checks if an Integer can be parsed.

If this method does not return an error, neither will any other function that parses an Integer. If this method returns an error, the other functions will return the same error.

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> SubFromAssign<&'a Integer> for Integer
[src]

Peforms the subtraction.

impl SubFromAssign<Integer> for Integer
[src]

Peforms the subtraction.

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> DivFromAssign<&'a Integer> for Integer
[src]

Peforms the division.

impl DivFromAssign<Integer> for Integer
[src]

Peforms the division.

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<'a> RemFromAssign<&'a Integer> for Integer
[src]

Peforms the remainder operation.

impl RemFromAssign<Integer> for Integer
[src]

Peforms the remainder operation.

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

The resulting type after applying the & operator

The method for the & operator

impl BitAnd<Integer> 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<Integer> 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<Integer> 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 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 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 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 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 DivFromAssign<u32> for Integer
[src]

Peforms the division.

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 RemFromAssign<u32> for Integer
[src]

Peforms the remainder operation.

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 BitAnd<u32> for Integer
[src]

The resulting type after applying the & operator

The method for the & operator

impl BitAndAssign<u32> for Integer
[src]

The method for the &= operator

impl BitOr<u32> for Integer
[src]

The resulting type after applying the | operator

The method for the | operator

impl BitOrAssign<u32> for Integer
[src]

The method for the |= operator

impl BitXor<u32> for Integer
[src]

The resulting type after applying the ^ operator

The method for the ^ operator

impl BitXorAssign<u32> for Integer
[src]

The method for the ^= operator

impl Add<i32> for Integer
[src]

The resulting type after applying the + operator

The method for the + operator

impl AddAssign<i32> for Integer
[src]

The method for the += operator

impl Sub<i32> for Integer
[src]

The resulting type after applying the - operator

The method for the - operator

impl SubAssign<i32> for Integer
[src]

The method for the -= operator

impl SubFromAssign<i32> for Integer
[src]

Peforms the subtraction.

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<i32> for Integer
[src]

The resulting type after applying the / operator

The method for the / operator

impl DivAssign<i32> for Integer
[src]

The method for the /= operator

impl DivFromAssign<i32> for Integer
[src]

Peforms the division.

impl Rem<i32> for Integer
[src]

The resulting type after applying the % operator

The method for the % operator

impl RemAssign<i32> for Integer
[src]

The method for the %= operator

impl RemFromAssign<i32> for Integer
[src]

Peforms the remainder operation.

impl Shl<i32> for Integer
[src]

The resulting type after applying the << operator

The method for the << operator

impl ShlAssign<i32> for Integer
[src]

The method for the <<= operator

impl Shr<i32> for Integer
[src]

The resulting type after applying the >> operator

The method for the >> operator

impl ShrAssign<i32> for Integer
[src]

The method for the >>= operator

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 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.