Struct ramp::int::Int
[−]
[src]
pub struct Int { // some fields omitted }
An arbitrary-precision signed integer.
This type grows to the size it needs to in order to store the result of any operation.
Creation
An Int
can be constructed in a number of ways:
Int::zero
andInt::one
construct a zero- and one-valuedInt
respectively.Int::from
will convert from any primitive integer type to anInt
of the same valuelet four = Int::from(4);
Int::from_str
(orstr::parse
) will attempt to convert from a string to anInt
let i = Int::from_str("123456789").unwrap();
Output
Int
supports all the formatting traits, allowing it to be used just like a regular integer
when used in format!
and similar macros. Int
also supports conversion to primitive integer
types, truncating if the Int
cannot fit into the target type. Conversion to primtive integers
is done with the From
trait:
let big_i = Int::from(123456789); let i = i32::from(&big_i); assert_eq!(123456789, i);
Usage
Int
has a number of operator overloads to make working with them as painless as possible.
The most basic usage is simply a + b
or similar. Assuming a
and b
are of type Int
, this
operation will consume both operands, reusing the storage from one of them. If you do not wish
your operands to be moved, one or both of them can be references: &a + &b
works as well, but
requires an entire new Int
to be allocated for the return value.
There are also a overloads for a small number of primitive integer types, namely i32
and
usize
. While automatic type widening isn't done in Rust in general, many operations are much
more efficient when working with a single integer. This means you can do a + 1
knowing that it
will be performed as efficiently as possible. Comparison with these integer types is also
possible, allowing checks for small constant values to be done easily:
let big_i = Int::from(123456789); assert!(big_i == 123456789);
Semantics
Addition, subtraction and multiplication follow the expected rules for integers. Division of two
integers, N / D
is defined as producing two values: a quotient, Q
, and a remainder, R
,
such that the following equation holds: N = Q*D + R
. The division operator itself returns Q
while the remainder/modulo operator returns R
.
The "bit-shift" operations are defined as being multiplication and division by a power-of-two for shift-left and shift-right respectively. The sign of the number is unaffected.
The remaining bitwise operands act as if the numbers are stored in two's complement format and as if the two inputs have the same number of bits.
Methods
impl Int
[src]
fn zero() -> Int
fn one() -> Int
fn from_single_limb(limb: Limb) -> Int
Creates a new Int from the given Limb.
fn sign(&self) -> i32
Returns the sign of the Int as either -1, 0 or 1 for self being negative, zero or positive, respectively.
fn abs(self) -> Int
Consumes self and returns the absolute value
fn to_single_limb(&self) -> Limb
Returns the least-significant limb of self.
fn abs_cmp(&self, other: &Int) -> Ordering
Compare the absolute value of self to the absolute value of other, returning an Ordering with the result.
fn shrink_to_fit(&mut self)
Try to shrink the allocated data for this Int.
fn to_str_radix(&self, base: u8, upper: bool) -> String
Returns a string containing the value of self in base base
. For bases greater than
ten, if upper
is true, upper-case letters are used, otherwise lower-case ones are used.
Panics if base
is less than two or greater than 36.
fn write_radix<W: Write>(&self, w: &mut W, base: u8, upper: bool) -> Result<()>
fn from_str_radix(src: &str, base: u8) -> Result<Int, ParseIntError>
Creates a new Int from the given string in base base
.
fn divmod(&self, other: &Int) -> (Int, Int)
Divide self by other, returning the quotient, Q, and remainder, R as (Q, R).
With N = self, D = other, Q and R satisfy: N = QD + R
.
This will panic if other
is zero.
fn pow(&self, exp: usize) -> Int
Raises self to the power of exp
fn square(&self) -> Int
Returns the square of self
.
fn dsquare(self) -> Int
fn sqrt_rem(self) -> Option<(Int, Int)>
Compute the sqrt of this number, returning its floor, S, and the remainder, R, as Some((S, R)), or None if this number is negative.
The numbers S, R are both positive and satisfy self = S * S + R
.
fn negate(&mut self)
Negates self
in-place
fn is_even(&self) -> bool
Returns whether or not this number is even.
Returns 0 if self == 0
fn trailing_zeros(&self) -> u32
Returns the number of trailing zero bits in this number
Returns 0 if self == 0
fn count_ones(&self) -> usize
Returns the number of ones (the population count) in this number
If this number is negative, it has infinitely many ones (in two's complement), so this returns usize::MAX.
fn bit_length(&self) -> u32
Returns the number of bits required to represent (the absolute
value of) this number, that is, floor(log2(abs(self))) + 1
.
Returns 1 if self == 0
.
fn bit(&self, bit: u32) -> bool
Returns the value of the bit
th bit in this number, as if it
were represented in two's complement.
fn set_bit(&mut self, bit: u32, bit_val: bool)
Set the bit
th bit of this number to bit_val
, treating
negative numbers as if they're stored in two's complement.
fn gcd(&self, other: &Int) -> Int
Calculates the Greatest Common Divisor (GCD) of the number and other
.
The result is always positive.
fn lcm(&self, other: &Int) -> Int
Calculates the Lowest Common Multiple (LCM) of the number and other
.