arbi
arbi implements an Arbitrary Precision Integer type: Arbi.
This crate has:
-
No dependencies.
Empty dependencies section in
Cargo.toml. -
no_stdsupport.This crate is
no_stdand utilizes thealloccrate for dynamic memory allocation.
Usage
Construct an Arbi integer
The following are all equivalent and return an Arbi object representing the
integer 0 (no memory allocation occurs):
use Arbi;
let a = new;
let b = default;
let c = zero;
assert_eq!;
assert_eq!;
assert_eq!;
Construct an Arbi integer from any primitive integer type value,
any f64 (except for infinities and NaNs), or any string containing a
base-base representation of an integer, where base must be in [2, 36].
use ;
// From any primitive integer type
let a = from;
assert_eq!;
// From any f64 (except for infinities and NaNs)
let b = from;
assert_eq!;
// From a string
let c = from_str_radix.unwrap;
let d = from_str_base.unwrap;
assert_eq!;
assert_eq!;
use FromStr;
// Currently, FromStr assumes base 10.
let e = from_str.unwrap;
assert_eq!;
let f = "123456789"..unwrap;
assert_eq!;
Use Arbi::with_capacity_bits() or Arbi::with_capacity() to construct an
integer representing zero with at least a specified capacity:
use ;
let mut a = with_capacity_bits;
let initial_capacity = a.capacity_bits;
assert_eq!;
assert!;
a.assign;
assert_eq!; // no memory allocation needed
assert_eq!;
Assign to an Arbi integer
Assign to an Arbi any primitive integer type value, a floating-point
value, a string, or another Arbi integer.
The main benefit of assigning is that if there is already enough capacity,
memory allocation need not occur. In contrast, from methods typically involve
memory allocation.
use ;
let mut a = with_capacity;
let mut capacity = a.capacity;
// From integer
a.assign;
assert_eq!;
assert_eq!; // no memory allocation occurred
// From float
a.assign;
assert_eq!;
assert!; // memory allocation occured because we needed
// more capacity to represent the value
capacity = a.capacity;
// From string (no need for the Assign trait)
if let Err = a.assign_str_radix
assert_eq!;
assert_eq!; // no memory allocation occurred
// From another Arbi integer
let b = from;
a.assign;
assert_eq!; // no memory allocation occurred
Comparisons
Compare an Arbi to another Arbi, a primitive integer type value, or a
floating-point value.
All equality and comparison operators are supported ==, !=, <, <=, >,
>=.
Comparisons with floating-point values are designed to be consistent with IEEE
754. SeePartialOrd<f64> for Arbi for a description of the semantics of
comparing an Arbi to a floating-point value.
use ;
let mut a;
// Float
a = from;
a <<= 2000_usize;
assert_ne!;
assert!;
assert!;
// Integer
a.assign;
assert_eq!;
assert!;
assert!;
// Arbi
let b = from;
assert!;
assert!;
To and From a String
In what follows, base must be an integer in [2, 36]. Moreover, the
*_radix() and *_base() string-related functions are equivalent, except that
*_radix() functions panic on an invalid base and *_base() functions cannot
panic due to an invalid base.
Convert any Arbi integer to a String containing the base-base
representation of the integer.
Arbi::to_string_radix()Arbi::to_string_base()Arbi::to_string()(usesArbi::to_string_base(), assuming base10).
Convert any string containing the base-base representation of an integer to
an Arbi.
Arbi::from_str_radix()Arbi::from_str_base()Arbi::from_str()(same asfrom_str_base()but assumes base10and needscore::str::FromStrin scope).
Assign any string containing the base-base representation of an integer to
an Arbi.
use ;
use FromStr; // needed for from_str() alone
let mut a = from;
// To string
assert_eq!; // assumes base-10
assert_eq!;
assert_eq!;
// From string
assert_eq!; // assumes base-10
assert_eq!;
assert_eq!;
// Assign string
a.assign_str_radix;
assert_eq!;
a.assign_str_base;
assert_eq!;
Arithmetic
All standard arithmetic operations are implemented:
- Binary operators:
+,-,*,/,%,+=,-=,*=,/=,%= - Unary operators:
-.
Bitwise Operations
All standard bitwise operations are implemented. These operators perform bitwise operations on integers using two's complement representation (with sign extension).
- Shift operators:
<<,>>,<<=,>>=. - Bitwise complement:
!. - Bitwise AND, OR, and XOR:
&,|,^,&=,|=,^=.
Test or set a bit at a specified index (zero-based) on the absolute value of an
Arbi integer:
Obtain the number of bits needed to represent the absolute value of an Arbi
integer using Arbi::bit_length():
use Arbi;
let mut a = zero;
assert_eq!;
a.incr; // 1
assert_eq!;
a.incr; // 10
assert_eq!;
a.incr; // 11
assert_eq!;
To Built-In Integer
In what follows, let * denote any primitive integer type name:
i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize.
Methods
-
to_*(): convert thisArbiinteger to a primitive integer type value. This is “wrapping”.See
Arbi::to_i32(). -
to_*_checked(): try to convert thisArbiinteger to a primitive integer type value. If thisArbiinteger does not fit in the target primitive integer type, returnsNone. -
fits_*(): test if thisArbiinteger fits in a primitive integer type. TheFitstrait can also be used to do the same thing.See
Arbi::fits_i32()and also theFitstrait.
To Floating-Point Value
Convert an Arbi integer to a floating-point value using the Arbi::to_f64()
method:
use ;
let a = from;
assert_eq!;
let b = from;
assert_ne!;
assert_eq!;
Increment/Decrement
Increment or decrement an Arbi integer in-place by one using the
Arbi::incr() and Arbi::decr() methods (+= and -= can also be used).
use Arbi;
let mut a = zero;
a.incr;
assert_eq!;
a.decr;
assert_eq!;
a.decr;
assert_eq!;
Exponentiation
Raise an Arbi integer to the power of a usize, u128, or another
Arbi integer using the Pow trait.
use ;
let mut a = from;
a = a.pow;
assert_eq!;
Display
The core::fmt::Display implementation uses the base-10 representation of the
Arbi integer and by extension, so does Arbi::to_string().
use Arbi;
let a = from;
assert_eq!;
assert_eq!;