pub struct Number {
pub value: Vec<u8>,
pub negative: bool,
pub byte_order: ByteOrder,
}Expand description
An integer of any size, stored as its sign and the bytes of its absolute value.
value holds the absolute value (the magnitude) of the integer as bytes, in the order
byte_order names: the most significant byte first for ByteOrder::BigEndian, the
least significant first for ByteOrder::LittleEndian. negative holds the sign. This
is the sign-and-magnitude form most big-integer libraries use: -2 is [2] with
negative set, not the two’s-complement byte [0xFE].
The constructors keep the shortest form: they drop the zero bytes above the most
significant non-zero byte and keep one byte for zero, so 1 and -1 are [1], and 0 is
[0], in either order. An empty value is zero as well; the constructors keep an empty
vector empty. Zero is never negative: there is one zero, not a positive and a negative one.
The fields are public, so a Number can also be built directly; it is then stored exactly
as given, zero bytes and the sign of zero included.
§Examples
use a::math::numbers::{ByteOrder, Number};
let big = Number::new_from_i32(-0x0001_0203, ByteOrder::BigEndian);
assert_eq!(big.value, [0x01, 0x02, 0x03]);
assert!(big.is_negative());
let little = Number::new_from_u32(0x0001_0203, ByteOrder::LittleEndian);
assert_eq!(little.value, [0x03, 0x02, 0x01]);
assert!(!little.is_negative());Fields§
§value: Vec<u8>The bytes of the absolute value of the number, in the order of byte_order.
negative: boolWhether the number is below zero; false for zero.
byte_order: ByteOrderThe order of the bytes in value.
Implementations§
Source§impl Number
impl Number
Sourcepub fn bits(&self) -> u64
pub fn bits(&self) -> u64
Returns the number of bits of the magnitude, |self|.ilog2() + 1, or 0 for zero: the
position of the highest one plus one, as BigInt::bits in num-bigint and
mpz_sizeinbase(x, 2) in GMP (which gives 1 for zero).
It takes constant time for a number in the shortest form: the length of the magnitude and its top byte decide.
§Examples
use a::math::numbers::{ByteOrder, Number};
assert_eq!(Number::new_from_i32(-0x1FF, ByteOrder::BigEndian).bits(), 9);
assert_eq!(Number::new_from_u64(1 << 40, ByteOrder::LittleEndian).bits(), 41);
assert_eq!(Number::new_from_u8(0, ByteOrder::BigEndian).bits(), 0);Sourcepub fn count_ones(&self) -> u64
pub fn count_ones(&self) -> u64
Returns the number of ones in the magnitude, as Python’s int.bit_count and num-bigint’s
BigUint::count_ones count them, and GMP’s mpz_popcount for numbers that are not
negative: the sign is not counted. (In two’s complement a negative number has infinitely
many ones, which mpz_popcount gives as its largest value.)
The bytes are counted as they are stored, in either byte order, sixteen at a time with
NEON’s cnt on aarch64.
§Examples
use a::math::numbers::{ByteOrder, Number};
assert_eq!(Number::new_from_i32(0b1011, ByteOrder::BigEndian).count_ones(), 3);
// The magnitude of -0b1011 has as many.
assert_eq!(Number::new_from_i32(-0b1011, ByteOrder::LittleEndian).count_ones(), 3);Sourcepub fn trailing_zeros(&self) -> Option<u64>
pub fn trailing_zeros(&self) -> Option<u64>
Returns the number of zeros below the lowest one of the magnitude, the position of that
one, or None for zero, as num-bigint’s BigInt::trailing_zeros and GMP’s mpz_scan1(x, 0)
give it. The same for x and -x: in two’s complement, -x has its lowest one where
x has.
The bytes are read from the least significant up, eight at a time while they are zero, so for random numbers the first read decides.
§Examples
use a::math::numbers::{ByteOrder, Number};
assert_eq!(Number::new_from_i32(-0x500, ByteOrder::BigEndian).trailing_zeros(), Some(8));
assert_eq!(Number::new_from_u8(0, ByteOrder::LittleEndian).trailing_zeros(), None);Sourcepub fn bit(&self, i: u64) -> bool
pub fn bit(&self, i: u64) -> bool
Returns bit i of the number in infinite two’s complement, as num-bigint’s BigInt::bit
and GMP’s mpz_tstbit: for a negative number, -x is !x + 1, so its bits are those of
!(|x| - 1), and all those above the magnitude are ones.
For a number that is not negative this takes constant time; for a negative one, the bytes
below bit i are read from the nearest down until one is not zero, which for random
numbers is the first.
§Examples
use a::math::numbers::{ByteOrder, Number};
// -4 is …11111100.
let n = Number::new_from_i32(-4, ByteOrder::BigEndian);
assert!(!n.bit(0) && !n.bit(1) && n.bit(2) && n.bit(3) && n.bit(1000));
let n = Number::new_from_i32(4, ByteOrder::LittleEndian);
assert!(n.bit(2) && !n.bit(3) && !n.bit(1000));Sourcepub fn set_bit(&mut self, i: u64, value: bool)
pub fn set_bit(&mut self, i: u64, value: bool)
Sets bit i of the number in infinite two’s complement to value, as num-bigint’s
BigInt::set_bit and GMP’s mpz_setbit and mpz_clrbit do, and stores the result back
as a sign and a magnitude, in the byte order of self, in the shortest form.
For a number that is not negative, the bit of the magnitude is set or cleared. For a
negative one, whose magnitude X has its lowest one at position t: above t the bits are
those of !X, so setting bit i > t clears it in X and clearing it sets it; bit t is
one, and clearing it adds 2^t to X; below t the bits are zeros, and setting bit i
subtracts 2^i from X.
The vector grows as needed, by Vec’s rules, and shrinks to the shortest form; a
big-endian magnitude that grows or shrinks at its top moves its bytes, as its most
significant byte comes first. Otherwise a number in the shortest form costs constant time
(but for the borrows and carries of a negative number, which for random numbers end in the
first byte).
§Panics
When the result is too long to be allocated.
§Examples
use a::math::numbers::{ByteOrder, Number};
let mut n = Number::new_from_i32(5, ByteOrder::BigEndian);
n.set_bit(9, true);
assert_eq!(n.value, [0x02, 0x05]);
// -4 is …11111100: setting bit 0 gives …11111101, -3.
let mut n = Number::new_from_i32(-4, ByteOrder::LittleEndian);
n.set_bit(0, true);
assert_eq!(n.value, [3]);
assert!(n.is_negative());
// Clearing bit 2 gives …11111001, -7.
n.set_bit(2, false);
assert_eq!(n.value, [7]);
assert!(n.is_negative());Source§impl Number
impl Number
Sourcepub fn to_f64(&self) -> f64
pub fn to_f64(&self) -> f64
Returns the number as the nearest f64, ties to even, as the conversions of the primitive
integers with as do; infinity, with the number’s sign, when it is too large.
A magnitude of up to 16 bytes is converted as a u128 is. Of a longer one, the top 64 bits
are converted by the hardware and scaled by a power of two, which is exact: the bits below
them can only matter when the top ones are exactly halfway between two f64s, and are read
only then.
§Examples
use a::math::numbers::{ByteOrder, Number};
assert_eq!(Number::new_from_i64(-12345, ByteOrder::BigEndian).to_f64(), -12345.0);
// 2^53 + 1 is halfway between two f64s: to the even one.
assert_eq!(Number::new_from_u64((1 << 53) + 1, ByteOrder::BigEndian).to_f64(), 9007199254740992.0);
let n = Number::from_str_radix(&"9".repeat(400), 10, ByteOrder::LittleEndian).unwrap();
assert_eq!(n.to_f64(), f64::INFINITY);Sourcepub fn to_f32(&self) -> f32
pub fn to_f32(&self) -> f32
Returns the number as the nearest f32, ties to even, as the conversions of the primitive
integers with as do; infinity, with the number’s sign, when it is too large. It is
rounded once, from the number itself: rounding the to_f64 to an f32
would round twice, and could be one off.
§Examples
use a::math::numbers::{ByteOrder, Number};
assert_eq!(Number::new_from_i64(-12345, ByteOrder::BigEndian).to_f32(), -12345.0);
assert_eq!(Number::new_from_u128(u128::MAX, ByteOrder::BigEndian).to_f32(), f32::INFINITY);Source§impl Number
impl Number
Sourcepub fn div_exact(&self, rhs: &Number) -> Number
pub fn div_exact(&self, rhs: &Number) -> Number
Returns self / rhs for a rhs that divides self exactly, faster than / does: in the
byte order of self, in the shortest form, in one new vector of exactly its length.
The quotient is computed from the low end, by Hensel’s method, whose limbs need no
estimates and whose partial remainders need no limbs above the quotient’s: for a quotient
as long as the divisor, about half the work of a division. When rhs does not divide
self, the result is a number, but not a meaningful one; is_multiple_of tells.
§Panics
When rhs is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
// 0x0123_4567_89AB_CDEF * -7 = -0x07F6_E5D4_C3B2_A189, divided back by 7.
let a = Number::new_from_i64(-0x07F6_E5D4_C3B2_A189, ByteOrder::BigEndian);
let q = a.div_exact(&Number::new_from_i32(7, ByteOrder::LittleEndian));
assert_eq!(q.value, [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF]);
assert!(q.is_negative());Sourcepub fn is_multiple_of(&self, rhs: &Number) -> bool
pub fn is_multiple_of(&self, rhs: &Number) -> bool
Returns whether self is a multiple of rhs: self = rhs q for an integer q, which is
to say that the remainder of self by rhs is zero. As for u32::is_multiple_of, zero is
a multiple of everything, and only zero is a multiple of zero. Signs do not count.
Allocates nothing; a dividend shorter than the divisor, or with fewer zero bits at the low end, is told apart without dividing.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_i64(-1_000_000_000_007 * 13, ByteOrder::BigEndian);
assert!(a.is_multiple_of(&Number::new_from_u8(13, ByteOrder::LittleEndian)));
assert!(!a.is_multiple_of(&Number::new_from_u8(12, ByteOrder::LittleEndian)));
let zero = Number::new_from_u8(0, ByteOrder::BigEndian);
assert!(zero.is_multiple_of(&zero));
assert!(!a.is_multiple_of(&zero));Source§impl Number
impl Number
Sourcepub fn div_rem(&self, rhs: &Number) -> (Number, Number)
pub fn div_rem(&self, rhs: &Number) -> (Number, Number)
Returns the quotient and the remainder of self divided by rhs, (self / rhs, self % rhs), computed together: the quotient truncated toward zero, the remainder with the sign of
self, both in the byte order of self and in the shortest form.
Each is built in one new vector of exactly its length (one byte for zero); both operands are left as they were.
§Panics
When rhs is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
// -1000 = 7 (-142) - 6.
let a = Number::new_from_i32(-1000, ByteOrder::BigEndian);
let (q, r) = a.div_rem(&Number::new_from_i32(7, ByteOrder::BigEndian));
assert_eq!(q.value, [142]);
assert!(q.is_negative());
assert_eq!(r.value, [6]);
assert!(r.is_negative());Sourcepub fn div_euclid(&self, rhs: &Number) -> Number
pub fn div_euclid(&self, rhs: &Number) -> Number
Returns the quotient of Euclid’s division of self by rhs: the q for which self = rhs q + r with 0 <= r < |rhs|, in the byte order of self and in the shortest form, as
i32::div_euclid gives it.
It is self / rhs unless the remainder self % rhs is below zero, and then one further
from zero: -7 / 2 = -3, but the Euclidean quotient is -4. It is built in one new vector
of exactly its length.
§Panics
When rhs is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_i32(-7, ByteOrder::BigEndian);
let q = a.div_euclid(&Number::new_from_i32(2, ByteOrder::BigEndian));
assert_eq!(q.value, [4]);
assert!(q.is_negative());
let q = a.div_euclid(&Number::new_from_i32(-2, ByteOrder::BigEndian));
assert_eq!(q.value, [4]);
assert!(!q.is_negative());Sourcepub fn rem_euclid(&self, rhs: &Number) -> Number
pub fn rem_euclid(&self, rhs: &Number) -> Number
Returns the remainder of Euclid’s division of self by rhs, self modulo |rhs|: the
r for which self = rhs q + r with 0 <= r < |rhs|, in the byte order of self and in
the shortest form, as i32::rem_euclid gives it. It is never negative.
It is self % rhs when that is not below zero, and self % rhs + |rhs| otherwise. It is
built in one new vector of exactly its length.
§Panics
When rhs is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_i32(-7, ByteOrder::BigEndian);
let r = a.rem_euclid(&Number::new_from_i32(2, ByteOrder::BigEndian));
assert_eq!(r.value, [1]);
assert!(!r.is_negative());
let r = a.rem_euclid(&Number::new_from_i32(-2, ByteOrder::BigEndian));
assert_eq!(r.value, [1]);
assert!(!r.is_negative());Source§impl Number
impl Number
Sourcepub fn div_floor(&self, rhs: &Number) -> Number
pub fn div_floor(&self, rhs: &Number) -> Number
Returns the quotient of self divided by rhs rounded toward negative infinity, ⌊self / rhs⌋, in the byte order of self and in the shortest form, as Python’s // and GMP’s
mpz_fdiv_q give it.
It is self / rhs unless the remainder self % rhs is not zero and the operands have
different signs, and then one less: -7 / 2 = -3, but ⌊-7 / 2⌋ = -4. For a positive
rhs it is div_euclid; for a negative one, -((-self).div_euclid(rhs)),
which is how it is computed, with -self a view of self with the other sign, so that it
costs what div_euclid does.
§Panics
When rhs is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_i32(-7, ByteOrder::BigEndian);
assert_eq!(a.div_floor(&Number::from(2)), -4);
assert_eq!(a.div_floor(&Number::from(-2)), 3);
assert_eq!(Number::from(7).div_floor(&Number::from(-2)), -4);Sourcepub fn rem_floor(&self, rhs: &Number) -> Number
pub fn rem_floor(&self, rhs: &Number) -> Number
Returns the remainder of self divided by rhs rounded toward negative infinity, self - rhs·⌊self / rhs⌋, in the byte order of self and in the shortest form, as Python’s %
and GMP’s mpz_fdiv_r give it: it has the sign of rhs, unless it is zero, and a magnitude
below that of rhs.
For a positive rhs it is rem_euclid; for a negative one,
-((-self).rem_euclid(rhs)), computed as div_floor is.
§Panics
When rhs is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_i32(-7, ByteOrder::BigEndian);
assert_eq!(a.rem_floor(&Number::from(2)), 1);
assert_eq!(a.rem_floor(&Number::from(-2)), -1);
assert_eq!(Number::from(7).rem_floor(&Number::from(-2)), -1);Source§impl Number
impl Number
Sourcepub fn factorial(n: u64, byte_order: ByteOrder) -> Number
pub fn factorial(n: u64, byte_order: ByteOrder) -> Number
Returns n!, the product of the integers from 1 to n, in the byte order byte_order, in
the shortest form, in one new vector; 0! is 1.
Up to 34, from a table; beyond, 2^(n - popcount(n)) times the odd part of n!, computed
by Luschny’s method from the prime factors of the swinging factorials, with balanced
product trees on the multiplication, as GMP’s mpz_fac_ui does.
§Panics
Only when the result or its working memory cannot be allocated, as for any vector: with
capacity overflow beyond isize::MAX bytes, and otherwise as the allocator fails.
§Examples
use a::math::numbers::{ByteOrder, Number};
assert_eq!(Number::factorial(5, ByteOrder::BigEndian).value, [120]);
// 25! = 15,511,210,043,330,985,984,000,000 = 0x0C_D4A0_619F_B090_7BC0_0000.
assert_eq!(
Number::factorial(25, ByteOrder::BigEndian).value,
[0x0C, 0xD4, 0xA0, 0x61, 0x9F, 0xB0, 0x90, 0x7B, 0xC0, 0x00, 0x00]
);Sourcepub fn binomial(n: u64, k: u64, byte_order: ByteOrder) -> Number
pub fn binomial(n: u64, k: u64, byte_order: ByteOrder) -> Number
Returns the binomial coefficient C(n, k) = n! / (k! (n - k)!), the number of ways to
choose k things out of n, in the byte order byte_order, in the shortest form, in one
new vector; 0 for k > n.
Coefficients below 2^128 are computed in registers. Beyond, for a k much smaller than
n, as the product of the k numbers from n - k + 1 to n divided exactly by the odd
part of k!, and otherwise as the product of the coefficient’s prime factors, whose
exponents are counted by Kummer’s theorem, as GMP’s mpz_bin_uiui does.
§Panics
Only when the result or its working memory cannot be allocated, as for any vector: with
capacity overflow beyond isize::MAX bytes, and otherwise as the allocator fails.
§Examples
use a::math::numbers::{ByteOrder, Number};
assert_eq!(Number::binomial(5, 2, ByteOrder::BigEndian).value, [10]);
assert_eq!(Number::binomial(2, 5, ByteOrder::BigEndian).value, [0]);
// C(100, 50) = 100,891,344,545,564,193,334,812,497,256
// = 0x01_45FF_5D3B_1070_380D_C808_5568.
assert_eq!(
Number::binomial(100, 50, ByteOrder::LittleEndian).value,
[0x68, 0x55, 0x08, 0xC8, 0x0D, 0x38, 0x70, 0x10, 0x3B, 0x5D, 0xFF, 0x45, 0x01]
);Source§impl Number
impl Number
Sourcepub fn gcd(&self, other: &Number) -> Number
pub fn gcd(&self, other: &Number) -> Number
Returns the greatest common divisor of self and other, in the byte order of self, in
the shortest form: the largest number that divides both, never negative.
As for GMP’s mpz_gcd, gcd(x, 0) = |x|, and gcd(0, 0) = 0. Signs do not count. Both
operands are left as they were, and the result is built in one new vector.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_i32(-84, ByteOrder::BigEndian);
let b = Number::new_from_i32(36, ByteOrder::LittleEndian);
let g = a.gcd(&b);
assert_eq!(g.value, [12]);
assert!(!g.is_negative());
assert!(g.byte_order() == ByteOrder::BigEndian);Sourcepub fn lcm(&self, other: &Number) -> Number
pub fn lcm(&self, other: &Number) -> Number
Returns the least common multiple of self and other, in the byte order of self, in
the shortest form: the smallest number that both divide, never negative, and zero when
either is zero, as GMP’s mpz_lcm gives it.
It is computed as |a| / gcd(a, b) · |b|, the shorter operand divided exactly by the
greatest common divisor, and the quotient multiplied by the longer one.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_i32(-4, ByteOrder::LittleEndian);
let b = Number::new_from_i32(6, ByteOrder::BigEndian);
let l = a.lcm(&b);
assert_eq!(l.value, [12]);
assert!(!l.is_negative());Sourcepub fn gcd_ext(&self, other: &Number) -> (Number, Number, Number)
pub fn gcd_ext(&self, other: &Number) -> (Number, Number, Number)
Returns (g, x, y): the greatest common divisor g of self and other, never negative,
and cofactors with g = self · x + other · y, all three in the byte order of self and in
the shortest form.
The cofactors are those of GMP’s mpz_gcdext: |x| < |other| / 2g and |y| < |self| / 2g,
which define them uniquely, but for a few cases. When |self| = |other|, x = 0 and y = sgn(other); otherwise x = sgn(self) when other = 0 or |other| = 2g, and y = sgn(other) when self = 0 or |self| = 2g. So x = 0 exactly when g = |other|, and
gcd_ext(0, 0) = (0, 0, 0).
§Examples
use a::math::numbers::{ByteOrder, Number};
// gcd(240, -46) = 2 = 240 · (-9) + (-46) · (-47).
let a = Number::new_from_i32(240, ByteOrder::BigEndian);
let b = Number::new_from_i32(-46, ByteOrder::BigEndian);
let (g, x, y) = a.gcd_ext(&b);
assert_eq!(g.value, [2]);
assert_eq!((x.is_negative(), x.value), (true, vec![9]));
assert_eq!((y.is_negative(), y.value), (true, vec![47]));Sourcepub fn mod_inverse(&self, modulus: &Number) -> Option<Number>
pub fn mod_inverse(&self, modulus: &Number) -> Option<Number>
Returns the inverse of self modulo |modulus|: the r with 0 <= r < |modulus| and
self · r = 1 modulo |modulus|, in the byte order of self and in the shortest form, or
None when there is none, which is when self and modulus have a common divisor besides
- As for GMP’s
mpz_invert, every number has the inverse 0 modulo 1.
§Panics
When modulus is zero, with the message of %.
§Examples
use a::math::numbers::{ByteOrder, Number};
// 3 · 4 = 12 = 1 modulo 11, and -3 · 7 = -21 = 1 modulo 11.
let m = Number::new_from_i32(11, ByteOrder::BigEndian);
let r = Number::new_from_i32(3, ByteOrder::BigEndian).mod_inverse(&m).unwrap();
assert_eq!(r.value, [4]);
let r = Number::new_from_i32(-3, ByteOrder::BigEndian).mod_inverse(&m).unwrap();
assert_eq!(r.value, [7]);
// 6 and 9 have the common divisor 3.
let m = Number::new_from_i32(9, ByteOrder::BigEndian);
assert!(Number::new_from_i32(6, ByteOrder::BigEndian).mod_inverse(&m).is_none());Source§impl Number
impl Number
Sourcepub fn new(value: Vec<u8>, byte_order: ByteOrder) -> Self
pub fn new(value: Vec<u8>, byte_order: ByteOrder) -> Self
Creates a non-negative number from its bytes in byte_order, dropping the zero bytes
above the most significant non-zero byte: the leading zeros in big-endian order, the
trailing zeros in little-endian order.
Zero bytes below the most significant non-zero byte are part of the number and stay.
A number whose bytes are all zero keeps one of them, [0]; an empty vector stays
empty.
Takes O(n) time for n bytes and reuses the allocation of value. For a negative
number, see new_signed.
§Examples
use a::math::numbers::{ByteOrder, Number};
let n = Number::new(vec![0, 0, 1, 0, 2], ByteOrder::BigEndian);
assert_eq!(n.value, [1, 0, 2]);
let n = Number::new(vec![2, 0, 1, 0, 0], ByteOrder::LittleEndian);
assert_eq!(n.value, [2, 0, 1]);
let zero = Number::new(vec![0, 0, 0], ByteOrder::BigEndian);
assert_eq!(zero.value, [0]);Sourcepub fn new_signed(value: Vec<u8>, negative: bool, byte_order: ByteOrder) -> Self
pub fn new_signed(value: Vec<u8>, negative: bool, byte_order: ByteOrder) -> Self
Creates a number from the bytes of its absolute value in byte_order and its sign,
dropping the high zero bytes as new does.
The number is negative when negative is set and the bytes are not all zero: zero is
never negative.
§Examples
use a::math::numbers::{ByteOrder, Number};
let n = Number::new_signed(vec![0, 1, 0], true, ByteOrder::BigEndian);
assert_eq!(n.value, [1, 0]);
assert!(n.is_negative());
let zero = Number::new_signed(vec![0, 0], true, ByteOrder::BigEndian);
assert_eq!(zero.value, [0]);
assert!(!zero.is_negative());Sourcepub fn new_from_u8(value: u8, byte_order: ByteOrder) -> Self
pub fn new_from_u8(value: u8, byte_order: ByteOrder) -> Self
Creates a number from a u8, its only byte. Zero is kept as [0].
Sourcepub fn new_from_u16(value: u16, byte_order: ByteOrder) -> Self
pub fn new_from_u16(value: u16, byte_order: ByteOrder) -> Self
Creates a number from the bytes of a u16 in byte_order, without the high zero
bytes (see new).
Sourcepub fn new_from_u32(value: u32, byte_order: ByteOrder) -> Self
pub fn new_from_u32(value: u32, byte_order: ByteOrder) -> Self
Creates a number from the bytes of a u32 in byte_order, without the high zero
bytes (see new).
Sourcepub fn new_from_u64(value: u64, byte_order: ByteOrder) -> Self
pub fn new_from_u64(value: u64, byte_order: ByteOrder) -> Self
Creates a number from the bytes of a u64 in byte_order, without the high zero
bytes (see new).
§Examples
use a::math::numbers::{ByteOrder, Number};
let n = Number::new_from_u64(0x0A0B_0C0D, ByteOrder::BigEndian);
assert_eq!(n.value, [0x0A, 0x0B, 0x0C, 0x0D]);
let n = Number::new_from_u64(0x0A0B_0C0D, ByteOrder::LittleEndian);
assert_eq!(n.value, [0x0D, 0x0C, 0x0B, 0x0A]);
let zero = Number::new_from_u64(0, ByteOrder::LittleEndian);
assert_eq!(zero.value, [0]);Sourcepub fn new_from_u128(value: u128, byte_order: ByteOrder) -> Self
pub fn new_from_u128(value: u128, byte_order: ByteOrder) -> Self
Creates a number from the bytes of a u128 in byte_order, without the high zero
bytes (see new).
Sourcepub fn new_from_usize(value: usize, byte_order: ByteOrder) -> Self
pub fn new_from_usize(value: usize, byte_order: ByteOrder) -> Self
Creates a number from the bytes of a usize in byte_order, without the high zero
bytes (see new).
usize is as wide as a pointer, but as the high zero bytes are dropped, a value has
the same bytes on every target.
Sourcepub fn new_from_i8_vec(value: Vec<i8>, byte_order: ByteOrder) -> Self
pub fn new_from_i8_vec(value: Vec<i8>, byte_order: ByteOrder) -> Self
Creates a non-negative number from bytes given as i8s in byte_order, without the
high zero bytes (see new).
The i8s are only how the bytes are given, not signed digits: each one is the byte
with the same bits (-1 is 0xFF, -128 is 0x80), and the number is never
negative.
§Examples
use a::math::numbers::{ByteOrder, Number};
let n = Number::new_from_i8_vec(vec![0, -1, 1], ByteOrder::BigEndian);
assert_eq!(n.value, [0xFF, 0x01]);
assert!(!n.is_negative());Sourcepub fn new_from_i8(value: i8, byte_order: ByteOrder) -> Self
pub fn new_from_i8(value: i8, byte_order: ByteOrder) -> Self
Creates a number from an i8: its sign, and its absolute value as the only byte.
i8::MIN becomes [0x80] with negative set. Zero is kept as [0].
Sourcepub fn new_from_i16(value: i16, byte_order: ByteOrder) -> Self
pub fn new_from_i16(value: i16, byte_order: ByteOrder) -> Self
Creates a number from an i16: its sign, and the bytes of its absolute value in
byte_order without the high zero bytes (see new).
§Examples
use a::math::numbers::{ByteOrder, Number};
let n = Number::new_from_i16(-2, ByteOrder::BigEndian);
assert_eq!(n.value, [0x02]);
assert!(n.is_negative());
let n = Number::new_from_i16(i16::MIN, ByteOrder::BigEndian);
assert_eq!(n.value, [0x80, 0x00]);
assert!(n.is_negative());Sourcepub fn new_from_i32(value: i32, byte_order: ByteOrder) -> Self
pub fn new_from_i32(value: i32, byte_order: ByteOrder) -> Self
Creates a number from an i32: its sign, and the bytes of its absolute value in
byte_order without the high zero bytes (see new).
Sourcepub fn new_from_i64(value: i64, byte_order: ByteOrder) -> Self
pub fn new_from_i64(value: i64, byte_order: ByteOrder) -> Self
Creates a number from an i64: its sign, and the bytes of its absolute value in
byte_order without the high zero bytes (see new).
Sourcepub fn new_from_i128(value: i128, byte_order: ByteOrder) -> Self
pub fn new_from_i128(value: i128, byte_order: ByteOrder) -> Self
Creates a number from an i128: its sign, and the bytes of its absolute value in
byte_order without the high zero bytes (see new).
Sourcepub fn new_from_isize(value: isize, byte_order: ByteOrder) -> Self
pub fn new_from_isize(value: isize, byte_order: ByteOrder) -> Self
Creates a number from an isize: its sign, and the bytes of its absolute value in
byte_order without the high zero bytes (see new).
As for new_from_usize, a value has the same bytes on every
target.
Sourcepub fn new_from_str(value: &str, byte_order: ByteOrder) -> Self
pub fn new_from_str(value: &str, byte_order: ByteOrder) -> Self
Creates a non-negative number from the UTF-8 bytes of a string, read in byte_order,
without the high zero bytes (see new).
The string is not parsed as digits: its bytes are the number, the way textbook RSA
turns a message into an integer, so a leading '-' is the byte 0x2D, not a sign.
In big-endian order the first byte of the string is the most significant one, and
leading '\0' characters are dropped as zero bytes; in little-endian order the first
byte is the least significant one, and trailing '\0' characters are dropped. An empty
string gives an empty value.
§Examples
use a::math::numbers::{ByteOrder, Number};
// 0x31_32_33 = 3,224,115, not 123.
let n = Number::new_from_str("123", ByteOrder::BigEndian);
assert_eq!(n.value, [0x31, 0x32, 0x33]);Sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
Returns whether the number is below zero.
It reads negative, as the constructors and the operators keep it: never set for zero. A
number built directly from its fields is stored as given, and a zero magnitude with
negative set says it is negative here, though it is zero to the comparisons and the
arithmetic, and to is_zero and signum.
§Examples
use a::math::numbers::{ByteOrder, Number};
assert!(Number::new_from_i32(-7, ByteOrder::BigEndian).is_negative());
assert!(!Number::new_from_i32(7, ByteOrder::BigEndian).is_negative());
assert!(!Number::new_from_i32(0, ByteOrder::BigEndian).is_negative());Sourcepub fn byte_order(&self) -> ByteOrder
pub fn byte_order(&self) -> ByteOrder
Returns the order of the bytes in value.
§Examples
use a::math::numbers::{ByteOrder, Number};
let n = Number::new_from_u8(7, ByteOrder::LittleEndian);
assert!(n.byte_order() == ByteOrder::LittleEndian);Source§impl Number
impl Number
Sourcepub fn pow(&self, exp: u32) -> Number
pub fn pow(&self, exp: u32) -> Number
Returns self raised to the power exp, in the byte order of self, in the shortest form,
in one new vector of exactly its length.
As for i32::pow, x^0 is 1 for every x, zero included. The power is negative when
self is negative and exp odd. The result is exact, however long: a power of n bytes
by exp takes up to n exp bytes.
§Panics
When the power is too long to allocate.
§Examples
use a::math::numbers::{ByteOrder, Number};
// (-3)^5 = -243 = -0xF3.
let p = Number::new_from_i32(-3, ByteOrder::BigEndian).pow(5);
assert_eq!(p.value, [0xF3]);
assert!(p.is_negative());
// 0x100^3 = 0x01_00_00_00, and anything to the power 0 is 1.
let p = Number::new_from_u32(0x100, ByteOrder::LittleEndian).pow(3);
assert_eq!(p.value, [0, 0, 0, 1]);
assert_eq!(Number::new_from_u8(0, ByteOrder::BigEndian).pow(0).value, [1]);Sourcepub fn pow_mod(&self, exp: &Number, modulus: &Number) -> Number
pub fn pow_mod(&self, exp: &Number, modulus: &Number) -> Number
Returns self raised to the power exp modulo modulus: the r with 0 <= r < |modulus|
and r = self^exp (mod |modulus|), Euclid’s remainder, as rem_euclid gives it, in the
byte order of self, in the shortest form, in one new vector of exactly its length.
self may be negative, or longer than the modulus, and the modulus negative: only its
magnitude counts. x^0 is 1 for every x, zero included, so modulo 1, where every
remainder is 0, x^0 mod 1 = 0.
The time it takes depends on the exponent’s bits, not only on its length: it is not a constant-time exponentiation, and is not meant for secret exponents (keys) where whoever could time it could learn them.
§Panics
When modulus is zero, with the message of %, and when exp is negative, with the message
negative exponent: a negative power is a power of the inverse of self modulo modulus,
which has to be computed first, with mod_inverse, and raised to -exp.
§Examples
use a::math::numbers::{ByteOrder, Number};
// 4^13 mod 497 = 445 = 0x01_BD.
let b = Number::new_from_u32(4, ByteOrder::BigEndian);
let e = Number::new_from_u32(13, ByteOrder::LittleEndian);
let m = Number::new_from_u32(497, ByteOrder::BigEndian);
assert_eq!(b.pow_mod(&e, &m).value, [0x01, 0xBD]);
// (-4)^13 mod 497 = 497 - 445 = 52: the remainder is never negative.
let b = Number::new_from_i32(-4, ByteOrder::BigEndian);
let r = b.pow_mod(&e, &m);
assert_eq!(r.value, [52]);
assert!(!r.is_negative());Source§impl Number
impl Number
Sourcepub fn is_probable_prime(&self) -> bool
pub fn is_probable_prime(&self) -> bool
Returns whether the number is a probable prime, by the Baillie–PSW test: false for
everything below 2, negative numbers included, and for every composite number it finds
a factor of or that fails the test; true for every prime.
Small factors are found by trial division; the rest is a strong (Miller–Rabin) test to base 2
and a strong Lucas test with Selfridge’s parameters. The answer is exact below 2^64, where
the test has been checked against every composite that passes Fermat’s test to base 2, which
Feitsma and Galway listed (2009), by Gilchrist: none passes both.
Above, no composite is known that passes it, though such numbers are believed to exist; GMP’s
mpz_probab_prime_p(n, 24) is the same test.
§Examples
use a::math::numbers::{ByteOrder, Number};
assert!(Number::new_from_u64(1_000_000_007, ByteOrder::BigEndian).is_probable_prime());
// 2047 = 23 * 89 passes the strong test to base 2, but not the Lucas test.
assert!(!Number::new_from_u32(2047, ByteOrder::LittleEndian).is_probable_prime());
assert!(!Number::new_from_i32(-7, ByteOrder::BigEndian).is_probable_prime());Sourcepub fn next_prime(&self) -> Number
pub fn next_prime(&self) -> Number
Returns the smallest probable prime above self, in the byte order of self, in one new
vector of exactly its length: 2 for anything below 2, negative numbers included.
“Probable prime” is that of is_probable_prime: exact below
2^64, and with no known exception above. The odd numbers above self are sieved by the
small primes in intervals, and those left are tested in order.
§Examples
use a::math::numbers::{ByteOrder, Number};
let p = Number::new_from_u32(1_000_000_000, ByteOrder::BigEndian).next_prime();
assert_eq!(p.value, 1_000_000_007u32.to_be_bytes());
assert_eq!(Number::new_from_i32(-5, ByteOrder::BigEndian).next_prime().value, [2]);Source§impl Number
impl Number
Sourcepub fn jacobi(&self, n: &Number) -> i32
pub fn jacobi(&self, n: &Number) -> i32
The Jacobi symbol (self / n) for an odd positive n: 0 when the two have a common
factor, otherwise 1 or -1. For a prime n it is the Legendre symbol, 1 for a non-zero
square modulo n and -1 for a non-square; for a composite n, 1 does not make self a
square, though -1 still rules it out.
§Panics
Panics if n is even, zero or negative.
§Examples
use a::math::numbers::{ByteOrder, Number};
let n = Number::new_from_u8(7, ByteOrder::BigEndian);
// 2 = 3^2 modulo 7; 3 is not a square modulo 7; 14 is a multiple of 7.
assert_eq!(Number::new_from_u8(2, ByteOrder::BigEndian).jacobi(&n), 1);
assert_eq!(Number::new_from_u8(3, ByteOrder::BigEndian).jacobi(&n), -1);
assert_eq!(Number::new_from_u8(14, ByteOrder::BigEndian).jacobi(&n), 0);Sourcepub fn sqrt_mod(&self, p: &Number) -> Option<Number>
pub fn sqrt_mod(&self, p: &Number) -> Option<Number>
A square root of self modulo the odd prime p: the smaller of the two, or None when
self is not a square modulo p. Zero is its own root. The result is in the byte order
of self.
The result is checked, so a composite p never gives a wrong root, though it may give
None for a number that has one.
Like pow_mod, which it is built on, it is not constant-time.
§Panics
Panics if p is even, zero or negative.
§Examples
use a::math::numbers::{ByteOrder, Number};
let p = Number::new_from_u8(13, ByteOrder::BigEndian);
// 6^2 = 36 = 10 modulo 13, and 7 = 13 - 6 is the other root.
let r = Number::new_from_u8(10, ByteOrder::BigEndian).sqrt_mod(&p).unwrap();
assert_eq!(r.value, [6]);
assert!(Number::new_from_u8(5, ByteOrder::BigEndian).sqrt_mod(&p).is_none());Source§impl Number
impl Number
Sourcepub fn to_str_radix(&self, radix: u32) -> String
pub fn to_str_radix(&self, radix: u32) -> String
Returns the number as text in radix: - for a negative number, then the digits of the
magnitude, the most significant first, without zeros in front, letters lower-case; 0 for
zero.
§Panics
When radix is not from 2 to 36.
§Examples
use a::math::numbers::{ByteOrder, Number};
let n = Number::new_from_i32(-255, ByteOrder::BigEndian);
assert_eq!(n.to_str_radix(16), "-ff");
assert_eq!(n.to_str_radix(10), "-255");
assert_eq!(n.to_str_radix(2), "-11111111");Sourcepub fn from_str_radix(
src: &str,
radix: u32,
byte_order: ByteOrder,
) -> Result<Number, ParseNumberError>
pub fn from_str_radix( src: &str, radix: u32, byte_order: ByteOrder, ) -> Result<Number, ParseNumberError>
Reads a number from text in radix: an optional sign, + or -, then at least one digit,
from 0 to the radix’s last, letters in either case, with no other characters, as the
primitive integers’ from_str_radix reads; zeros in front count for nothing. The number is
built in byte_order, in the shortest form; -0 is zero, not negative.
§Errors
ParseNumberError of kind NumberErrorKind::Empty for empty text, and
NumberErrorKind::InvalidDigit for a character that is not a digit of the radix, or a
sign alone.
§Panics
When radix is not from 2 to 36.
§Examples
use a::math::numbers::{ByteOrder, Number, NumberErrorKind};
let n = Number::from_str_radix("-FF", 16, ByteOrder::BigEndian).unwrap();
assert_eq!(n.value, [0xFF]);
assert!(n.is_negative());
// 10^21 = 0x36_35C9_ADC5_DEA0_0000.
let n = Number::from_str_radix("1000000000000000000000", 10, ByteOrder::LittleEndian);
assert_eq!(n.unwrap().value, [0, 0, 0xA0, 0xDE, 0xC5, 0xAD, 0xC9, 0x35, 0x36]);
let error = Number::from_str_radix("12a", 10, ByteOrder::BigEndian).err().unwrap();
assert_eq!(*error.kind(), NumberErrorKind::InvalidDigit);Source§impl Number
impl Number
Sourcepub fn isqrt(&self) -> Number
pub fn isqrt(&self) -> Number
Returns the square root of self rounded down, floor(sqrt(self)): the largest integer
whose square is not above self, in the byte order of self, in the shortest form, in one
new vector.
Up to 16 bytes the root is computed in registers; longer magnitudes by Zimmermann’s Karatsuba square root, in the time of a product of their limbs, up to a constant.
§Panics
When self is negative, with the message of i64::isqrt; a negative zero is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
let n = Number::new_from_u64(1_000_000_000_000, ByteOrder::BigEndian);
assert_eq!(n.isqrt().value, Number::new_from_u32(1_000_000, ByteOrder::BigEndian).value);
// 99 is not a square: its root is rounded down.
assert_eq!(Number::new_from_u8(99, ByteOrder::LittleEndian).isqrt().value, [9]);Sourcepub fn isqrt_rem(&self) -> (Number, Number)
pub fn isqrt_rem(&self) -> (Number, Number)
Returns the square root of self rounded down and the remainder: (s, r) with
s = floor(sqrt(self)) and r = self - s^2, which is at most 2s, both in the byte order
of self, in the shortest form, in one new vector each.
§Panics
When self is negative, with the message of i64::isqrt; a negative zero is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
// 1000 = 31^2 + 39.
let (s, r) = Number::new_from_u16(1000, ByteOrder::BigEndian).isqrt_rem();
assert_eq!(s.value, [31]);
assert_eq!(r.value, [39]);Sourcepub fn nth_root(&self, n: u32) -> Number
pub fn nth_root(&self, n: u32) -> Number
Returns the n-th root of self truncated toward zero: for a self that is not negative,
the largest integer whose n-th power is not above self, and for a negative self and an
odd n, the root of its magnitude negated, as GMP’s mpz_root and num-integer’s
Roots::nth_root give it. The root is in the byte order of self, in the shortest form,
in one new vector.
n = 1 gives self and n = 2 its square root, as isqrt does. Up to 16
bytes the root is computed in registers; for longer magnitudes, by Newton’s method at
doubling precision, as GMP’s mpn_rootrem does.
§Panics
When n is 0, with the message root degree n must be at least 1, and when self is
negative and n even, with the message even roots of a negative are imaginary, as
num-integer’s Roots::nth_root panics; a negative zero is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
// 10^18 = (10^6)^3, and the cube root of -999,999 is -99.
let n = Number::new_from_u64(1_000_000_000_000_000_000, ByteOrder::BigEndian);
assert_eq!(n.nth_root(3).value, Number::new_from_u32(1_000_000, ByteOrder::BigEndian).value);
let m = Number::new_from_i32(-999_999, ByteOrder::LittleEndian);
let root = m.nth_root(3);
assert_eq!(root.value, [99]);
assert!(root.is_negative());Source§impl Number
impl Number
Sourcepub fn is_zero(&self) -> bool
pub fn is_zero(&self) -> bool
Returns whether the number is zero: whether every byte of its magnitude is zero, or it has none.
A number in the shortest form costs one read of its top byte.
§Examples
use a::math::numbers::{ByteOrder, Number};
assert!(Number::new_from_u8(0, ByteOrder::BigEndian).is_zero());
assert!(Number::new(Vec::new(), ByteOrder::LittleEndian).is_zero());
assert!(!Number::new_from_i8(-1, ByteOrder::BigEndian).is_zero());Sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
Returns whether the number is above zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
assert!(Number::new_from_i8(1, ByteOrder::BigEndian).is_positive());
assert!(!Number::new_from_i8(0, ByteOrder::BigEndian).is_positive());
assert!(!Number::new_from_i8(-1, ByteOrder::BigEndian).is_positive());Sourcepub fn abs(&self) -> Number
pub fn abs(&self) -> Number
Returns the absolute value of the number, in its byte order and in the shortest form, in one new vector of exactly its length.
§Examples
use a::math::numbers::{ByteOrder, Number};
let n = Number::new_from_i32(-1000, ByteOrder::BigEndian).abs();
assert_eq!(n, 1000);
assert!(!n.is_negative());Sourcepub fn signum(&self) -> Number
pub fn signum(&self) -> Number
Returns the sign of the number as a number, in its byte order: -1 below zero, 0 for zero
and 1 above, as i32::signum does.
§Examples
use a::math::numbers::{ByteOrder, Number};
assert_eq!(Number::new_from_i32(-1000, ByteOrder::BigEndian).signum(), -1);
assert_eq!(Number::new_from_i32(0, ByteOrder::BigEndian).signum(), 0);
assert_eq!(Number::new_from_i32(1000, ByteOrder::LittleEndian).signum(), 1);Trait Implementations§
Source§impl Add for Number
impl Add for Number
Source§fn add(self, rhs: Number) -> Number
fn add(self, rhs: Number) -> Number
Returns self + rhs, in the byte order of self, in the shortest form, as self += rhs
does.
The sum is built in the vector of self when its capacity has room for the sum, or else
in the vector of rhs when that has room, also when rhs is in the other byte order,
either of which allocates nothing; otherwise it gets one new vector of exactly its
length.
§Examples
use a::math::numbers::{ByteOrder, Number};
// 0x01_00 + -0x02_FF = -0x01_FF.
let a = Number::new_from_i32(0x0100, ByteOrder::LittleEndian);
let b = Number::new_from_i32(-0x02FF, ByteOrder::BigEndian);
let sum = a + b;
assert_eq!(sum.value, [0xFF, 0x01]);
assert!(sum.is_negative());Source§impl Add<&Number> for &Number
impl Add<&Number> for &Number
Source§fn add(self, rhs: &Number) -> Number
fn add(self, rhs: &Number) -> Number
Returns self + rhs, in the byte order of self, in the shortest form.
The sum is built in one new vector of exactly its length (one byte for zero), the only allocation; both operands are left as they were. The operands may have any signs, byte orders and lengths, and need not be in the shortest form.
§Examples
use a::math::numbers::{ByteOrder, Number};
// 0x01_00 + -0x01_01 = -1, in the byte order of the left operand.
let a = Number::new_from_i32(0x0100, ByteOrder::BigEndian);
let b = Number::new_from_i32(-0x0101, ByteOrder::LittleEndian);
let sum = &a + &b;
assert_eq!(sum.value, [1]);
assert!(sum.is_negative());
assert!(sum.byte_order() == ByteOrder::BigEndian);
// A number plus its negation is zero, one zero byte and not negative.
let minus_a = Number::new_from_i32(-0x0100, ByteOrder::LittleEndian);
let zero = &a + &minus_a;
assert_eq!(zero.value, [0]);
assert!(!zero.is_negative());Source§impl Add<&Number> for Number
impl Add<&Number> for Number
Source§fn add(self, rhs: &Number) -> Number
fn add(self, rhs: &Number) -> Number
Returns self + rhs, in the byte order of self, in the shortest form, as self += rhs
does.
The sum is built in the vector of self when its capacity has room for the sum, which
allocates nothing, and otherwise in one new vector of exactly its length. rhs is left
as it was.
§Examples
use a::math::numbers::{ByteOrder, Number};
let mut value = Vec::with_capacity(8);
value.extend([0xFF, 0xFF]);
let a = Number::new(value, ByteOrder::BigEndian);
let pointer = a.value.as_ptr();
let one = Number::new_from_u8(1, ByteOrder::BigEndian);
// 0xFF_FF + 1 = 0x01_00_00, in the vector of `a`.
let sum = a + &one;
assert_eq!(sum.value, [1, 0, 0]);
assert_eq!(sum.value.as_ptr(), pointer);Source§impl Add<Number> for &Number
impl Add<Number> for &Number
Source§fn add(self, rhs: Number) -> Number
fn add(self, rhs: Number) -> Number
Returns self + rhs, in the byte order of self, in the shortest form.
The sum is built in the vector of rhs when its capacity has room for the sum, which
allocates nothing, also when rhs is in the other byte order; otherwise it gets one new
vector of exactly its length. self is left as it was.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_u32(0x01_02_03, ByteOrder::BigEndian);
let mut value = Vec::with_capacity(3);
value.extend([0x10, 0x20]);
let b = Number::new(value, ByteOrder::LittleEndian);
let pointer = b.value.as_ptr();
// 0x01_02_03 + 0x20_10 = 0x01_22_13, big-endian, in the vector of `b`.
let sum = &a + b;
assert_eq!(sum.value, [0x01, 0x22, 0x13]);
assert_eq!(sum.value.as_ptr(), pointer);Source§impl AddAssign for Number
impl AddAssign for Number
Source§fn add_assign(&mut self, rhs: Number)
fn add_assign(&mut self, rhs: Number)
Adds rhs to self, keeping the byte order of self, in the shortest form.
The sum is built in the vector of self when its capacity has room for the sum, or else
in the vector of rhs when that has room, also when rhs is in the other byte order,
either of which allocates nothing; otherwise it gets one new vector of exactly its
length.
§Examples
use a::math::numbers::{ByteOrder, Number};
// `n` has no room for 0x01_00, but the vector of the right operand has.
let mut n = Number::new_from_u8(0xFF, ByteOrder::BigEndian);
let mut value = Vec::with_capacity(2);
value.push(1);
let one = Number::new(value, ByteOrder::BigEndian);
let pointer = one.value.as_ptr();
n += one;
assert_eq!(n.value, [1, 0]);
assert_eq!(n.value.as_ptr(), pointer);Source§impl AddAssign<&Number> for Number
impl AddAssign<&Number> for Number
Source§fn add_assign(&mut self, rhs: &Number)
fn add_assign(&mut self, rhs: &Number)
Adds rhs to self, keeping the byte order of self, in the shortest form.
The sum is built in the vector of self when its capacity has room for the sum, which
allocates nothing, and otherwise in one new vector of exactly its length, which
replaces it. rhs is left as it was.
§Examples
use a::math::numbers::{ByteOrder, Number};
let mut n = Number::new_from_u8(0xFF, ByteOrder::LittleEndian);
n += &Number::new_from_u16(0x0101, ByteOrder::BigEndian);
assert_eq!(n.value, [0x00, 0x02]);
n += &Number::new_from_i16(-0x0201, ByteOrder::BigEndian);
assert_eq!(n.value, [1]);
assert!(n.is_negative());Source§impl AddAssign<i8> for Number
impl AddAssign<i8> for Number
Source§fn add_assign(&mut self, rhs: i8)
fn add_assign(&mut self, rhs: i8)
Makes self self + rhs, as with rhs a number, in the vector of self when it has room.
Source§impl AddAssign<i16> for Number
impl AddAssign<i16> for Number
Source§fn add_assign(&mut self, rhs: i16)
fn add_assign(&mut self, rhs: i16)
Makes self self + rhs, as with rhs a number, in the vector of self when it has room.
Source§impl AddAssign<i32> for Number
impl AddAssign<i32> for Number
Source§fn add_assign(&mut self, rhs: i32)
fn add_assign(&mut self, rhs: i32)
Makes self self + rhs, as with rhs a number, in the vector of self when it has room.
Source§impl AddAssign<i64> for Number
impl AddAssign<i64> for Number
Source§fn add_assign(&mut self, rhs: i64)
fn add_assign(&mut self, rhs: i64)
Makes self self + rhs, as with rhs a number, in the vector of self when it has room.
Source§impl AddAssign<i128> for Number
impl AddAssign<i128> for Number
Source§fn add_assign(&mut self, rhs: i128)
fn add_assign(&mut self, rhs: i128)
Makes self self + rhs, as with rhs a number, in the vector of self when it has room.
Source§impl AddAssign<isize> for Number
impl AddAssign<isize> for Number
Source§fn add_assign(&mut self, rhs: isize)
fn add_assign(&mut self, rhs: isize)
Makes self self + rhs, as with rhs a number, in the vector of self when it has room.
Source§impl AddAssign<u8> for Number
impl AddAssign<u8> for Number
Source§fn add_assign(&mut self, rhs: u8)
fn add_assign(&mut self, rhs: u8)
Makes self self + rhs, as with rhs a number, in the vector of self when it has room.
Source§impl AddAssign<u16> for Number
impl AddAssign<u16> for Number
Source§fn add_assign(&mut self, rhs: u16)
fn add_assign(&mut self, rhs: u16)
Makes self self + rhs, as with rhs a number, in the vector of self when it has room.
Source§impl AddAssign<u32> for Number
impl AddAssign<u32> for Number
Source§fn add_assign(&mut self, rhs: u32)
fn add_assign(&mut self, rhs: u32)
Makes self self + rhs, as with rhs a number, in the vector of self when it has room.
Source§impl AddAssign<u64> for Number
impl AddAssign<u64> for Number
Source§fn add_assign(&mut self, rhs: u64)
fn add_assign(&mut self, rhs: u64)
Makes self self + rhs, as with rhs a number, in the vector of self when it has room.
Source§impl AddAssign<u128> for Number
impl AddAssign<u128> for Number
Source§fn add_assign(&mut self, rhs: u128)
fn add_assign(&mut self, rhs: u128)
Makes self self + rhs, as with rhs a number, in the vector of self when it has room.
Source§impl AddAssign<usize> for Number
impl AddAssign<usize> for Number
Source§fn add_assign(&mut self, rhs: usize)
fn add_assign(&mut self, rhs: usize)
Makes self self + rhs, as with rhs a number, in the vector of self when it has room.
Source§impl BitAnd for Number
impl BitAnd for Number
Source§fn bitand(self, rhs: Number) -> Number
fn bitand(self, rhs: Number) -> Number
Returns self & rhs, in the byte order of self, in the shortest
form, as self &= rhs does.
The result is built in the vector of self when its capacity has room for it, or
else in the vector of rhs when that has room, also when rhs is in the other
byte order, either of which allocates nothing; otherwise it gets one new vector of
exactly its length.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_i32(12, ByteOrder::LittleEndian);
let b = Number::new_from_i32(-10, ByteOrder::BigEndian);
let c = a & b;
assert_eq!(c.value, [4]);
assert_eq!(c.is_negative(), false);Source§impl BitAnd<&Number> for &Number
impl BitAnd<&Number> for &Number
Source§fn bitand(self, rhs: &Number) -> Number
fn bitand(self, rhs: &Number) -> Number
Returns self & rhs, the bitwise and of the numbers in
infinite two’s complement, in the byte order of self, in the shortest form.
The result is built in one new vector of exactly its length (one byte for zero); both operands are left as they were. The operands may have any signs, byte orders and lengths, and need not be in the shortest form.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_i32(12, ByteOrder::BigEndian);
let b = Number::new_from_i32(-10, ByteOrder::LittleEndian);
let c = &a & &b;
// As for `i32`: `12 & -10` is 4.
assert_eq!(c.value, [4]);
assert_eq!(c.is_negative(), false);
assert!(c.byte_order() == ByteOrder::BigEndian);Source§impl BitAnd<&Number> for Number
impl BitAnd<&Number> for Number
Source§fn bitand(self, rhs: &Number) -> Number
fn bitand(self, rhs: &Number) -> Number
Returns self & rhs, in the byte order of self, in the shortest
form, as self &= rhs does.
The result is built in the vector of self when its capacity has room for it,
which allocates nothing, and otherwise in one new vector of exactly its length.
rhs is left as it was.
§Examples
use a::math::numbers::{ByteOrder, Number};
let mut value = Vec::with_capacity(4);
value.extend([0x0F, 0x00]);
let a = Number::new(value, ByteOrder::BigEndian);
let pointer = a.value.as_ptr();
let c = a & &Number::new_from_u8(0xF0, ByteOrder::LittleEndian);
assert_eq!(c.value, [0]);
assert_eq!(c.value.as_ptr(), pointer);Source§impl BitAnd<Number> for &Number
impl BitAnd<Number> for &Number
Source§fn bitand(self, rhs: Number) -> Number
fn bitand(self, rhs: Number) -> Number
Returns self & rhs, in the byte order of self, in the shortest
form.
The result is built in the vector of rhs when its capacity has room for it, also
when rhs is in the other byte order, and otherwise in one new vector of exactly
its length. self is left as it was.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_u16(0x0F00, ByteOrder::BigEndian);
let mut value = Vec::with_capacity(4);
value.extend([0xF0]);
let b = Number::new(value, ByteOrder::LittleEndian);
let pointer = b.value.as_ptr();
let c = &a & b;
assert_eq!(c.value, [0]);
assert_eq!(c.value.as_ptr(), pointer);
assert!(c.byte_order() == ByteOrder::BigEndian);Source§impl BitAndAssign for Number
impl BitAndAssign for Number
Source§fn bitand_assign(&mut self, rhs: Number)
fn bitand_assign(&mut self, rhs: Number)
Sets self to self & rhs, keeping its byte order, in the shortest form.
The result is built in the vector of self when its capacity has room for it, or
else in the vector of rhs when that has room, also when rhs is in the other
byte order, either of which allocates nothing; otherwise it gets one new vector of
exactly its length.
§Examples
use a::math::numbers::{ByteOrder, Number};
// `n` has room for one byte only; the vector of the right operand has more.
let mut value = Vec::with_capacity(1);
value.push(0xFF);
let mut n = Number::new(value, ByteOrder::BigEndian);
let mut value = Vec::with_capacity(4);
value.extend([0x00, 0x01]);
let b = Number::new(value, ByteOrder::LittleEndian);
let pointer = b.value.as_ptr();
n &= b;
// `0xFF & 0x01_00` is 0.
assert_eq!(n.value, [0]);
assert_eq!(n.value.as_ptr() == pointer, false);Source§impl BitAndAssign<&Number> for Number
impl BitAndAssign<&Number> for Number
Source§fn bitand_assign(&mut self, rhs: &Number)
fn bitand_assign(&mut self, rhs: &Number)
Sets self to self & rhs, keeping its byte order, in the shortest form.
The result is built in the vector of self when its capacity has room for it,
which allocates nothing, and otherwise in one new vector of exactly its length,
which replaces it. rhs is left as it was.
§Examples
use a::math::numbers::{ByteOrder, Number};
let mut n = Number::new_from_i32(12, ByteOrder::LittleEndian);
n &= &Number::new_from_i32(-10, ByteOrder::BigEndian);
assert_eq!(n.value, [4]);
assert_eq!(n.is_negative(), false);Source§impl BitOr for Number
impl BitOr for Number
Source§fn bitor(self, rhs: Number) -> Number
fn bitor(self, rhs: Number) -> Number
Returns self | rhs, in the byte order of self, in the shortest
form, as self |= rhs does.
The result is built in the vector of self when its capacity has room for it, or
else in the vector of rhs when that has room, also when rhs is in the other
byte order, either of which allocates nothing; otherwise it gets one new vector of
exactly its length.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_i32(12, ByteOrder::LittleEndian);
let b = Number::new_from_i32(-10, ByteOrder::BigEndian);
let c = a | b;
assert_eq!(c.value, [2]);
assert_eq!(c.is_negative(), true);Source§impl BitOr<&Number> for &Number
impl BitOr<&Number> for &Number
Source§fn bitor(self, rhs: &Number) -> Number
fn bitor(self, rhs: &Number) -> Number
Returns self | rhs, the bitwise or of the numbers in
infinite two’s complement, in the byte order of self, in the shortest form.
The result is built in one new vector of exactly its length (one byte for zero); both operands are left as they were. The operands may have any signs, byte orders and lengths, and need not be in the shortest form.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_i32(12, ByteOrder::BigEndian);
let b = Number::new_from_i32(-10, ByteOrder::LittleEndian);
let c = &a | &b;
// As for `i32`: `12 | -10` is -2.
assert_eq!(c.value, [2]);
assert_eq!(c.is_negative(), true);
assert!(c.byte_order() == ByteOrder::BigEndian);Source§impl BitOr<&Number> for Number
impl BitOr<&Number> for Number
Source§fn bitor(self, rhs: &Number) -> Number
fn bitor(self, rhs: &Number) -> Number
Returns self | rhs, in the byte order of self, in the shortest
form, as self |= rhs does.
The result is built in the vector of self when its capacity has room for it,
which allocates nothing, and otherwise in one new vector of exactly its length.
rhs is left as it was.
§Examples
use a::math::numbers::{ByteOrder, Number};
let mut value = Vec::with_capacity(4);
value.extend([0x0F, 0x00]);
let a = Number::new(value, ByteOrder::BigEndian);
let pointer = a.value.as_ptr();
let c = a | &Number::new_from_u8(0xF0, ByteOrder::LittleEndian);
assert_eq!(c.value, [0x0F, 0xF0]);
assert_eq!(c.value.as_ptr(), pointer);Source§impl BitOr<Number> for &Number
impl BitOr<Number> for &Number
Source§fn bitor(self, rhs: Number) -> Number
fn bitor(self, rhs: Number) -> Number
Returns self | rhs, in the byte order of self, in the shortest
form.
The result is built in the vector of rhs when its capacity has room for it, also
when rhs is in the other byte order, and otherwise in one new vector of exactly
its length. self is left as it was.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_u16(0x0F00, ByteOrder::BigEndian);
let mut value = Vec::with_capacity(4);
value.extend([0xF0]);
let b = Number::new(value, ByteOrder::LittleEndian);
let pointer = b.value.as_ptr();
let c = &a | b;
assert_eq!(c.value, [0x0F, 0xF0]);
assert_eq!(c.value.as_ptr(), pointer);
assert!(c.byte_order() == ByteOrder::BigEndian);Source§impl BitOrAssign for Number
impl BitOrAssign for Number
Source§fn bitor_assign(&mut self, rhs: Number)
fn bitor_assign(&mut self, rhs: Number)
Sets self to self | rhs, keeping its byte order, in the shortest form.
The result is built in the vector of self when its capacity has room for it, or
else in the vector of rhs when that has room, also when rhs is in the other
byte order, either of which allocates nothing; otherwise it gets one new vector of
exactly its length.
§Examples
use a::math::numbers::{ByteOrder, Number};
// `n` has room for one byte only; the vector of the right operand has more.
let mut value = Vec::with_capacity(1);
value.push(0xFF);
let mut n = Number::new(value, ByteOrder::BigEndian);
let mut value = Vec::with_capacity(4);
value.extend([0x00, 0x01]);
let b = Number::new(value, ByteOrder::LittleEndian);
let pointer = b.value.as_ptr();
n |= b;
// `0xFF | 0x01_00` is 0x01_FF.
assert_eq!(n.value, [0x01, 0xFF]);
assert_eq!(n.value.as_ptr() == pointer, true);Source§impl BitOrAssign<&Number> for Number
impl BitOrAssign<&Number> for Number
Source§fn bitor_assign(&mut self, rhs: &Number)
fn bitor_assign(&mut self, rhs: &Number)
Sets self to self | rhs, keeping its byte order, in the shortest form.
The result is built in the vector of self when its capacity has room for it,
which allocates nothing, and otherwise in one new vector of exactly its length,
which replaces it. rhs is left as it was.
§Examples
use a::math::numbers::{ByteOrder, Number};
let mut n = Number::new_from_i32(12, ByteOrder::LittleEndian);
n |= &Number::new_from_i32(-10, ByteOrder::BigEndian);
assert_eq!(n.value, [2]);
assert_eq!(n.is_negative(), true);Source§impl BitXor for Number
impl BitXor for Number
Source§fn bitxor(self, rhs: Number) -> Number
fn bitxor(self, rhs: Number) -> Number
Returns self ^ rhs, in the byte order of self, in the shortest
form, as self ^= rhs does.
The result is built in the vector of self when its capacity has room for it, or
else in the vector of rhs when that has room, also when rhs is in the other
byte order, either of which allocates nothing; otherwise it gets one new vector of
exactly its length.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_i32(12, ByteOrder::LittleEndian);
let b = Number::new_from_i32(-10, ByteOrder::BigEndian);
let c = a ^ b;
assert_eq!(c.value, [6]);
assert_eq!(c.is_negative(), true);Source§impl BitXor<&Number> for &Number
impl BitXor<&Number> for &Number
Source§fn bitxor(self, rhs: &Number) -> Number
fn bitxor(self, rhs: &Number) -> Number
Returns self ^ rhs, the bitwise exclusive or of the numbers in
infinite two’s complement, in the byte order of self, in the shortest form.
The result is built in one new vector of exactly its length (one byte for zero); both operands are left as they were. The operands may have any signs, byte orders and lengths, and need not be in the shortest form.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_i32(12, ByteOrder::BigEndian);
let b = Number::new_from_i32(-10, ByteOrder::LittleEndian);
let c = &a ^ &b;
// As for `i32`: `12 ^ -10` is -6.
assert_eq!(c.value, [6]);
assert_eq!(c.is_negative(), true);
assert!(c.byte_order() == ByteOrder::BigEndian);Source§impl BitXor<&Number> for Number
impl BitXor<&Number> for Number
Source§fn bitxor(self, rhs: &Number) -> Number
fn bitxor(self, rhs: &Number) -> Number
Returns self ^ rhs, in the byte order of self, in the shortest
form, as self ^= rhs does.
The result is built in the vector of self when its capacity has room for it,
which allocates nothing, and otherwise in one new vector of exactly its length.
rhs is left as it was.
§Examples
use a::math::numbers::{ByteOrder, Number};
let mut value = Vec::with_capacity(4);
value.extend([0x0F, 0x00]);
let a = Number::new(value, ByteOrder::BigEndian);
let pointer = a.value.as_ptr();
let c = a ^ &Number::new_from_u8(0xF0, ByteOrder::LittleEndian);
assert_eq!(c.value, [0x0F, 0xF0]);
assert_eq!(c.value.as_ptr(), pointer);Source§impl BitXor<Number> for &Number
impl BitXor<Number> for &Number
Source§fn bitxor(self, rhs: Number) -> Number
fn bitxor(self, rhs: Number) -> Number
Returns self ^ rhs, in the byte order of self, in the shortest
form.
The result is built in the vector of rhs when its capacity has room for it, also
when rhs is in the other byte order, and otherwise in one new vector of exactly
its length. self is left as it was.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_u16(0x0F00, ByteOrder::BigEndian);
let mut value = Vec::with_capacity(4);
value.extend([0xF0]);
let b = Number::new(value, ByteOrder::LittleEndian);
let pointer = b.value.as_ptr();
let c = &a ^ b;
assert_eq!(c.value, [0x0F, 0xF0]);
assert_eq!(c.value.as_ptr(), pointer);
assert!(c.byte_order() == ByteOrder::BigEndian);Source§impl BitXorAssign for Number
impl BitXorAssign for Number
Source§fn bitxor_assign(&mut self, rhs: Number)
fn bitxor_assign(&mut self, rhs: Number)
Sets self to self ^ rhs, keeping its byte order, in the shortest form.
The result is built in the vector of self when its capacity has room for it, or
else in the vector of rhs when that has room, also when rhs is in the other
byte order, either of which allocates nothing; otherwise it gets one new vector of
exactly its length.
§Examples
use a::math::numbers::{ByteOrder, Number};
// `n` has room for one byte only; the vector of the right operand has more.
let mut value = Vec::with_capacity(1);
value.push(0xFF);
let mut n = Number::new(value, ByteOrder::BigEndian);
let mut value = Vec::with_capacity(4);
value.extend([0x00, 0x01]);
let b = Number::new(value, ByteOrder::LittleEndian);
let pointer = b.value.as_ptr();
n ^= b;
// `0xFF ^ 0x01_00` is 0x01_FF.
assert_eq!(n.value, [0x01, 0xFF]);
assert_eq!(n.value.as_ptr() == pointer, true);Source§impl BitXorAssign<&Number> for Number
impl BitXorAssign<&Number> for Number
Source§fn bitxor_assign(&mut self, rhs: &Number)
fn bitxor_assign(&mut self, rhs: &Number)
Sets self to self ^ rhs, keeping its byte order, in the shortest form.
The result is built in the vector of self when its capacity has room for it,
which allocates nothing, and otherwise in one new vector of exactly its length,
which replaces it. rhs is left as it was.
§Examples
use a::math::numbers::{ByteOrder, Number};
let mut n = Number::new_from_i32(12, ByteOrder::LittleEndian);
n ^= &Number::new_from_i32(-10, ByteOrder::BigEndian);
assert_eq!(n.value, [6]);
assert_eq!(n.is_negative(), true);Source§impl Debug for Number
impl Debug for Number
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Writes the number in decimal, as Display does, so that numbers that are
equal look alike; the bytes are in the public fields.
§Examples
use a::math::numbers::{ByteOrder, Number};
let n = Number::new_from_i32(-1000, ByteOrder::BigEndian);
assert_eq!(format!("{n:?}"), "-1000");
assert_eq!(format!("{:?}", [n]), "[-1000]");Source§impl Default for Number
impl Default for Number
Source§fn default() -> Self
fn default() -> Self
Returns zero, little-endian, with an empty vector, which allocates nothing: mem::take of
a number leaves this behind. Little-endian is the order of the limbs of the arithmetic on
every target this crate has kernels for: the kernels read and write its bytes as limbs as
they are, where big-endian bytes are turned around.
§Examples
use a::math::numbers::{ByteOrder, Number};
let zero = Number::default();
assert!(zero.is_zero());
assert!(zero.byte_order() == ByteOrder::LittleEndian);
assert_eq!(zero.value.capacity(), 0);Source§impl Display for Number
impl Display for Number
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Writes the number in decimal: - for a negative number, then the digits; the formatter’s
flags, width and fill apply as they do to the primitive integers.
§Examples
use a::math::numbers::{ByteOrder, Number};
let n = Number::new_from_i64(-1_234_567_890_123, ByteOrder::LittleEndian);
assert_eq!(n.to_string(), "-1234567890123");
assert_eq!(format!("{:>16}", n), " -1234567890123");
assert_eq!(format!("{:+}", Number::new_from_u8(7, ByteOrder::BigEndian)), "+7");Source§impl Div for Number
impl Div for Number
Source§fn div(self, rhs: Number) -> Number
fn div(self, rhs: Number) -> Number
Returns self / rhs, truncated toward zero, in the byte order of self, in the shortest
form, as self /= rhs does.
The quotient is built in the vector of self, which has room for it unless self is an
empty vector, and then in the vector of rhs when that has room.
§Panics
When rhs is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
// -7 / -2 = 3.
let a = Number::new_from_i32(-7, ByteOrder::LittleEndian);
let b = Number::new_from_i32(-2, ByteOrder::BigEndian);
let quotient = a / b;
assert_eq!(quotient.value, [3]);
assert!(!quotient.is_negative());Source§impl Div<&Divisor> for &Number
impl Div<&Divisor> for &Number
Source§fn div(self, d: &Divisor) -> Number
fn div(self, d: &Divisor) -> Number
Returns self / d, truncated toward zero, as dividing by the number d was made from
does, in one new vector of exactly its length.
§Examples
use a::math::numbers::{ByteOrder, Divisor, Number};
let d = Divisor::new(&Number::new_from_u8(7, ByteOrder::BigEndian));
let q = &Number::new_from_i32(-100, ByteOrder::LittleEndian) / &d;
assert_eq!(q.value, [14]);
assert!(q.is_negative());Source§impl Div<&Divisor> for Number
impl Div<&Divisor> for Number
Source§fn div(self, d: &Divisor) -> Number
fn div(self, d: &Divisor) -> Number
Returns self / d, as self /= d does, in the vector of self.
§Examples
use a::math::numbers::{ByteOrder, Divisor, Number};
let d = Divisor::new(&Number::new_from_u8(16, ByteOrder::BigEndian));
let a = Number::new_from_u32(0x1234_5678, ByteOrder::BigEndian);
let pointer = a.value.as_ptr();
let q = a / &d;
assert_eq!(q.value, [0x01, 0x23, 0x45, 0x67]);
assert_eq!(q.value.as_ptr(), pointer);Source§impl Div<&Number> for &Number
impl Div<&Number> for &Number
Source§fn div(self, rhs: &Number) -> Number
fn div(self, rhs: &Number) -> Number
Returns self / rhs, truncated toward zero, in the byte order of self, in the shortest
form.
The quotient is built in one new vector of exactly its length (one byte for zero); both operands are left as they were. The operands may have any signs, byte orders and lengths, and need not be in the shortest form.
§Panics
When rhs is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
// -0x01_00 / 3 = -0x55, truncated toward zero, in the byte order of the left operand.
let a = Number::new_from_i32(-0x0100, ByteOrder::BigEndian);
let b = Number::new_from_i32(3, ByteOrder::LittleEndian);
let quotient = &a / &b;
assert_eq!(quotient.value, [0x55]);
assert!(quotient.is_negative());
assert!(quotient.byte_order() == ByteOrder::BigEndian);Source§impl Div<&Number> for Number
impl Div<&Number> for Number
Source§fn div(self, rhs: &Number) -> Number
fn div(self, rhs: &Number) -> Number
Returns self / rhs, truncated toward zero, in the byte order of self, in the shortest
form, as self /= rhs does.
The quotient is built in the vector of self, which has room for it unless self is an
empty vector; rhs is left as it was.
§Panics
When rhs is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new(vec![0x01, 0x00, 0x00], ByteOrder::BigEndian);
let pointer = a.value.as_ptr();
// 0x01_00_00 / 0x01_01 = 0xFF, in the vector of `a`.
let quotient = a / &Number::new_from_u16(0x0101, ByteOrder::LittleEndian);
assert_eq!(quotient.value, [0xFF]);
assert_eq!(quotient.value.as_ptr(), pointer);Source§impl Div<Number> for &Number
impl Div<Number> for &Number
Source§fn div(self, rhs: Number) -> Number
fn div(self, rhs: Number) -> Number
Returns self / rhs, truncated toward zero, in the byte order of self, in the shortest
form.
The quotient is built in the vector of rhs when its capacity has room for it, also when
rhs is in the other byte order; otherwise it gets one new vector of exactly its length.
self is left as it was.
§Panics
When rhs is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_u16(0x0300, ByteOrder::BigEndian);
let mut value = Vec::with_capacity(2);
value.push(3);
let b = Number::new(value, ByteOrder::LittleEndian);
let pointer = b.value.as_ptr();
// 0x03_00 / 3 = 0x01_00, big-endian, in the vector of `b`.
let quotient = &a / b;
assert_eq!(quotient.value, [0x01, 0x00]);
assert_eq!(quotient.value.as_ptr(), pointer);Source§impl DivAssign for Number
impl DivAssign for Number
Source§fn div_assign(&mut self, rhs: Number)
fn div_assign(&mut self, rhs: Number)
Divides self by rhs, truncating toward zero, keeping the byte order of self, in the
shortest form.
The quotient is built in the vector of self, which has room for it unless self is an
empty vector, and then in the vector of rhs when that has room.
§Panics
When rhs is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
let mut n = Number::new_from_u32(1_000_000, ByteOrder::BigEndian);
n /= Number::new_from_u16(1000, ByteOrder::BigEndian);
assert_eq!(n.value, [0x03, 0xE8]);Source§impl DivAssign<&Divisor> for Number
impl DivAssign<&Divisor> for Number
Source§fn div_assign(&mut self, d: &Divisor)
fn div_assign(&mut self, d: &Divisor)
Divides self by d, truncating toward zero, keeping the byte order of self, in its
vector, which has room for the quotient unless it is empty.
§Examples
use a::math::numbers::{ByteOrder, Divisor, Number};
let ten = Divisor::new(&Number::new_from_u8(10, ByteOrder::BigEndian));
let mut n = Number::new_from_u32(12_345, ByteOrder::LittleEndian);
n /= &ten;
n /= &ten;
assert_eq!(n.value, [123]);Source§impl DivAssign<&Number> for Number
impl DivAssign<&Number> for Number
Source§fn div_assign(&mut self, rhs: &Number)
fn div_assign(&mut self, rhs: &Number)
Divides self by rhs, truncating toward zero, keeping the byte order of self, in the
shortest form.
The quotient is built in the vector of self, which has room for it unless self is an
empty vector; rhs is left as it was.
§Panics
When rhs is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
let mut n = Number::new_from_u16(0x1000, ByteOrder::LittleEndian);
n /= &Number::new_from_u8(0x10, ByteOrder::BigEndian);
assert_eq!(n.value, [0x00, 0x01]);
n /= &Number::new_from_i8(-3, ByteOrder::BigEndian);
assert_eq!(n.value, [0x55]);
assert!(n.is_negative());Source§impl DivAssign<i8> for Number
impl DivAssign<i8> for Number
Source§fn div_assign(&mut self, rhs: i8)
fn div_assign(&mut self, rhs: i8)
Makes self self / rhs, as with rhs a number, in the vector of self when it has room.
Source§impl DivAssign<i16> for Number
impl DivAssign<i16> for Number
Source§fn div_assign(&mut self, rhs: i16)
fn div_assign(&mut self, rhs: i16)
Makes self self / rhs, as with rhs a number, in the vector of self when it has room.
Source§impl DivAssign<i32> for Number
impl DivAssign<i32> for Number
Source§fn div_assign(&mut self, rhs: i32)
fn div_assign(&mut self, rhs: i32)
Makes self self / rhs, as with rhs a number, in the vector of self when it has room.
Source§impl DivAssign<i64> for Number
impl DivAssign<i64> for Number
Source§fn div_assign(&mut self, rhs: i64)
fn div_assign(&mut self, rhs: i64)
Makes self self / rhs, as with rhs a number, in the vector of self when it has room.
Source§impl DivAssign<i128> for Number
impl DivAssign<i128> for Number
Source§fn div_assign(&mut self, rhs: i128)
fn div_assign(&mut self, rhs: i128)
Makes self self / rhs, as with rhs a number, in the vector of self when it has room.
Source§impl DivAssign<isize> for Number
impl DivAssign<isize> for Number
Source§fn div_assign(&mut self, rhs: isize)
fn div_assign(&mut self, rhs: isize)
Makes self self / rhs, as with rhs a number, in the vector of self when it has room.
Source§impl DivAssign<u8> for Number
impl DivAssign<u8> for Number
Source§fn div_assign(&mut self, rhs: u8)
fn div_assign(&mut self, rhs: u8)
Makes self self / rhs, as with rhs a number, in the vector of self when it has room.
Source§impl DivAssign<u16> for Number
impl DivAssign<u16> for Number
Source§fn div_assign(&mut self, rhs: u16)
fn div_assign(&mut self, rhs: u16)
Makes self self / rhs, as with rhs a number, in the vector of self when it has room.
Source§impl DivAssign<u32> for Number
impl DivAssign<u32> for Number
Source§fn div_assign(&mut self, rhs: u32)
fn div_assign(&mut self, rhs: u32)
Makes self self / rhs, as with rhs a number, in the vector of self when it has room.
Source§impl DivAssign<u64> for Number
impl DivAssign<u64> for Number
Source§fn div_assign(&mut self, rhs: u64)
fn div_assign(&mut self, rhs: u64)
Makes self self / rhs, as with rhs a number, in the vector of self when it has room.
Source§impl DivAssign<u128> for Number
impl DivAssign<u128> for Number
Source§fn div_assign(&mut self, rhs: u128)
fn div_assign(&mut self, rhs: u128)
Makes self self / rhs, as with rhs a number, in the vector of self when it has room.
Source§impl DivAssign<usize> for Number
impl DivAssign<usize> for Number
Source§fn div_assign(&mut self, rhs: usize)
fn div_assign(&mut self, rhs: usize)
Makes self self / rhs, as with rhs a number, in the vector of self when it has room.
impl Element for Number
impl Eq for Number
Source§impl FromStr for Number
impl FromStr for Number
Source§fn from_str(src: &str) -> Result<Number, ParseNumberError>
fn from_str(src: &str) -> Result<Number, ParseNumberError>
Reads a number from decimal text, as Number::from_str_radix does with radix 10, and
builds it little-endian, the byte order of Number::default.
§Examples
use a::math::numbers::Number;
let n: Number = "-123456789012345678901234567890".parse().unwrap();
assert!(n.is_negative());
assert_eq!(n.to_string(), "-123456789012345678901234567890");
assert!("12a".parse::<Number>().is_err());Source§type Err = ParseNumberError
type Err = ParseNumberError
Source§impl Hash for Number
impl Hash for Number
Source§fn hash<H: Hasher>(&self, state: &mut H)
fn hash<H: Hasher>(&self, state: &mut H)
Hashes the number by value, so that numbers that are equal hash alike, whatever their byte orders and however their magnitudes are stored: the sign, the significant length, then the significant bytes of the magnitude, the least significant first.
§Examples
use a::math::numbers::{ByteOrder, Number};
use std::collections::HashSet;
let mut seen = HashSet::new();
seen.insert(Number::new_from_u64(1 << 40, ByteOrder::BigEndian));
assert!(seen.contains(&Number::new(vec![0, 0, 0, 0, 0, 1, 0], ByteOrder::LittleEndian)));Source§impl LowerHex for Number
impl LowerHex for Number
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Writes the number in hexadecimal, lower-case: the sign and the digits of the magnitude, not
a two’s complement; 0x in front with the # flag.
§Examples
use a::math::numbers::{ByteOrder, Number};
let n = Number::new_from_i32(-0xBEEF, ByteOrder::BigEndian);
assert_eq!(format!("{n:x}"), "-beef");
assert_eq!(format!("{n:#x}"), "-0xbeef");Source§impl Mul for Number
impl Mul for Number
Source§fn mul(self, rhs: Number) -> Number
fn mul(self, rhs: Number) -> Number
Returns self * rhs, in the byte order of self, in the shortest form, as self *= rhs
does.
The product is built in the vector of self when its capacity has room for it, or else
in the vector of rhs when that has room, also when rhs is in the other byte order;
otherwise it gets one new vector of exactly its length.
§Examples
use a::math::numbers::{ByteOrder, Number};
// -0x01_00 * -0x02_FF = 0x02_FF_00.
let a = Number::new_from_i32(-0x0100, ByteOrder::LittleEndian);
let b = Number::new_from_i32(-0x02FF, ByteOrder::BigEndian);
let product = a * b;
assert_eq!(product.value, [0x00, 0xFF, 0x02]);
assert!(!product.is_negative());Source§impl Mul<&Number> for &Number
impl Mul<&Number> for &Number
Source§fn mul(self, rhs: &Number) -> Number
fn mul(self, rhs: &Number) -> Number
Returns self * rhs, in the byte order of self, in the shortest form.
The product is built in one new vector of exactly its length (one byte for zero); both
operands are left as they were. A number multiplied by itself (&a * &a) is squared, each
product of two different limbs computed once. The operands may have any signs, byte orders
and lengths, and need not be in the shortest form.
§Examples
use a::math::numbers::{ByteOrder, Number};
// 0x01_00 * -0x01_01 = -0x01_01_00, in the byte order of the left operand.
let a = Number::new_from_i32(0x0100, ByteOrder::BigEndian);
let b = Number::new_from_i32(-0x0101, ByteOrder::LittleEndian);
let product = &a * &b;
assert_eq!(product.value, [0x01, 0x01, 0x00]);
assert!(product.is_negative());
assert!(product.byte_order() == ByteOrder::BigEndian);
// Anything times zero is zero, one zero byte and not negative.
let zero = &b * &Number::new_from_u8(0, ByteOrder::BigEndian);
assert_eq!(zero.value, [0]);
assert!(!zero.is_negative());Source§impl Mul<&Number> for Number
impl Mul<&Number> for Number
Source§fn mul(self, rhs: &Number) -> Number
fn mul(self, rhs: &Number) -> Number
Returns self * rhs, in the byte order of self, in the shortest form, as self *= rhs
does.
The product is built in the vector of self when its capacity has room for it, and
otherwise in one new vector of exactly its length. rhs is left as it was.
§Examples
use a::math::numbers::{ByteOrder, Number};
let mut value = Vec::with_capacity(4);
value.extend([0xFF, 0xFF]);
let a = Number::new(value, ByteOrder::BigEndian);
let pointer = a.value.as_ptr();
// 0xFF_FF * 0xFF_FF = 0xFF_FE_00_01, in the vector of `a`.
let product = a * &Number::new_from_u16(0xFFFF, ByteOrder::LittleEndian);
assert_eq!(product.value, [0xFF, 0xFE, 0x00, 0x01]);
assert_eq!(product.value.as_ptr(), pointer);Source§impl Mul<Number> for &Number
impl Mul<Number> for &Number
Source§fn mul(self, rhs: Number) -> Number
fn mul(self, rhs: Number) -> Number
Returns self * rhs, in the byte order of self, in the shortest form.
The product is built in the vector of rhs when its capacity has room for it, also when
rhs is in the other byte order; otherwise it gets one new vector of exactly its length.
self is left as it was.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_u16(0x0102, ByteOrder::BigEndian);
let mut value = Vec::with_capacity(4);
value.extend([0x03]);
let b = Number::new(value, ByteOrder::LittleEndian);
let pointer = b.value.as_ptr();
// 0x01_02 * 3 = 0x03_06, big-endian, in the vector of `b`.
let product = &a * b;
assert_eq!(product.value, [0x03, 0x06]);
assert_eq!(product.value.as_ptr(), pointer);Source§impl MulAssign for Number
impl MulAssign for Number
Source§fn mul_assign(&mut self, rhs: Number)
fn mul_assign(&mut self, rhs: Number)
Multiplies self by rhs, keeping the byte order of self, in the shortest form.
The product is built in the vector of self when its capacity has room for it, or else
in the vector of rhs when that has room, also when rhs is in the other byte order;
otherwise it gets one new vector of exactly its length.
§Examples
use a::math::numbers::{ByteOrder, Number};
// `n` has no room for 0xFF * 0x01_02 = 0x01_00_FE, but the vector of the right operand
// has.
let mut n = Number::new_from_u8(0xFF, ByteOrder::BigEndian);
let mut value = Vec::with_capacity(3);
value.extend([1, 2]);
let b = Number::new(value, ByteOrder::BigEndian);
let pointer = b.value.as_ptr();
n *= b;
assert_eq!(n.value, [0x01, 0x00, 0xFE]);
assert_eq!(n.value.as_ptr(), pointer);Source§impl<C: Curve> MulAssign<&Number> for Point<'_, C>
impl<C: Curve> MulAssign<&Number> for Point<'_, C>
Source§fn mul_assign(&mut self, k: &Number)
fn mul_assign(&mut self, k: &Number)
*= operation. Read moreSource§impl MulAssign<&Number> for Number
impl MulAssign<&Number> for Number
Source§fn mul_assign(&mut self, rhs: &Number)
fn mul_assign(&mut self, rhs: &Number)
Multiplies self by rhs, keeping the byte order of self, in the shortest form.
The product is built in the vector of self when its capacity has room for it, and
otherwise in one new vector of exactly its length, which replaces it. rhs is left as it
was.
§Examples
use a::math::numbers::{ByteOrder, Number};
let mut n = Number::new_from_u8(0x10, ByteOrder::LittleEndian);
n *= &Number::new_from_u16(0x0101, ByteOrder::BigEndian);
assert_eq!(n.value, [0x10, 0x10]);
n *= &Number::new_from_i8(-2, ByteOrder::BigEndian);
assert_eq!(n.value, [0x20, 0x20]);
assert!(n.is_negative());Source§impl<C: Curve> MulAssign<Number> for Point<'_, C>
impl<C: Curve> MulAssign<Number> for Point<'_, C>
Source§fn mul_assign(&mut self, k: Number)
fn mul_assign(&mut self, k: Number)
*= operation. Read moreSource§impl MulAssign<i8> for Number
impl MulAssign<i8> for Number
Source§fn mul_assign(&mut self, rhs: i8)
fn mul_assign(&mut self, rhs: i8)
Makes self self * rhs, as with rhs a number, in the vector of self when it has room.
Source§impl MulAssign<i16> for Number
impl MulAssign<i16> for Number
Source§fn mul_assign(&mut self, rhs: i16)
fn mul_assign(&mut self, rhs: i16)
Makes self self * rhs, as with rhs a number, in the vector of self when it has room.
Source§impl MulAssign<i32> for Number
impl MulAssign<i32> for Number
Source§fn mul_assign(&mut self, rhs: i32)
fn mul_assign(&mut self, rhs: i32)
Makes self self * rhs, as with rhs a number, in the vector of self when it has room.
Source§impl MulAssign<i64> for Number
impl MulAssign<i64> for Number
Source§fn mul_assign(&mut self, rhs: i64)
fn mul_assign(&mut self, rhs: i64)
Makes self self * rhs, as with rhs a number, in the vector of self when it has room.
Source§impl MulAssign<i128> for Number
impl MulAssign<i128> for Number
Source§fn mul_assign(&mut self, rhs: i128)
fn mul_assign(&mut self, rhs: i128)
Makes self self * rhs, as with rhs a number, in the vector of self when it has room.
Source§impl MulAssign<isize> for Number
impl MulAssign<isize> for Number
Source§fn mul_assign(&mut self, rhs: isize)
fn mul_assign(&mut self, rhs: isize)
Makes self self * rhs, as with rhs a number, in the vector of self when it has room.
Source§impl MulAssign<u8> for Number
impl MulAssign<u8> for Number
Source§fn mul_assign(&mut self, rhs: u8)
fn mul_assign(&mut self, rhs: u8)
Makes self self * rhs, as with rhs a number, in the vector of self when it has room.
Source§impl MulAssign<u16> for Number
impl MulAssign<u16> for Number
Source§fn mul_assign(&mut self, rhs: u16)
fn mul_assign(&mut self, rhs: u16)
Makes self self * rhs, as with rhs a number, in the vector of self when it has room.
Source§impl MulAssign<u32> for Number
impl MulAssign<u32> for Number
Source§fn mul_assign(&mut self, rhs: u32)
fn mul_assign(&mut self, rhs: u32)
Makes self self * rhs, as with rhs a number, in the vector of self when it has room.
Source§impl MulAssign<u64> for Number
impl MulAssign<u64> for Number
Source§fn mul_assign(&mut self, rhs: u64)
fn mul_assign(&mut self, rhs: u64)
Makes self self * rhs, as with rhs a number, in the vector of self when it has room.
Source§impl MulAssign<u128> for Number
impl MulAssign<u128> for Number
Source§fn mul_assign(&mut self, rhs: u128)
fn mul_assign(&mut self, rhs: u128)
Makes self self * rhs, as with rhs a number, in the vector of self when it has room.
Source§impl MulAssign<usize> for Number
impl MulAssign<usize> for Number
Source§fn mul_assign(&mut self, rhs: usize)
fn mul_assign(&mut self, rhs: usize)
Makes self self * rhs, as with rhs a number, in the vector of self when it has room.
Source§impl Neg for Number
impl Neg for Number
Source§fn neg(self) -> Number
fn neg(self) -> Number
Returns -self: the same magnitude with the other sign, in the shortest form, in the
vector of self; zero stays zero, never negative.
§Examples
use a::math::numbers::{ByteOrder, Number};
let n = -Number::new_from_i32(1000, ByteOrder::BigEndian);
assert_eq!(n, -1000);
assert_eq!(-n, 1000);
assert!(!(-Number::new_from_u8(0, ByteOrder::BigEndian)).is_negative());Source§impl Not for Number
impl Not for Number
Source§fn not(self) -> Number
fn not(self) -> Number
Returns !self, every bit of the number in infinite two’s complement inverted, which is
-self - 1, in the byte order of self, in the shortest form.
The result is built in the vector of self when its capacity has room for it, which
allocates nothing, and otherwise in one new vector of exactly its length; it is at most one
byte longer. Only the bytes the carry of + 1 (or the borrow of - 1) passes through
are written, which for random numbers is the lowest one: a number in the shortest form
takes constant time.
§Examples
use a::math::numbers::{ByteOrder, Number};
// As for `i32`: `!0xFF` is `-0x100`, and `!-0x100` is `0xFF`.
let n = !Number::new_from_i32(0xFF, ByteOrder::BigEndian);
assert_eq!(n.value, [0x01, 0x00]);
assert!(n.is_negative());
let n = !n;
assert_eq!(n.value, [0xFF]);
assert!(!n.is_negative());Source§impl Not for &Number
impl Not for &Number
Source§fn not(self) -> Number
fn not(self) -> Number
Returns !self, which is -self - 1, in the byte order of self, in the shortest form.
The result is built in one new vector of exactly its length; self is left as it was.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_i32(-1, ByteOrder::LittleEndian);
let n = !&a;
assert_eq!(n.value, [0]);
assert!(!n.is_negative());Source§impl Ord for Number
impl Ord for Number
Source§fn cmp(&self, other: &Number) -> Ordering
fn cmp(&self, other: &Number) -> Ordering
Compares the numbers by value: every negative number is below zero, and zero below every positive one, whatever their byte orders and however their magnitudes are stored.
§Examples
use a::math::numbers::{ByteOrder, Number};
let mut v = vec![
Number::new_from_i32(300, ByteOrder::BigEndian),
Number::new_from_i32(-5, ByteOrder::LittleEndian),
Number::new_from_i32(7, ByteOrder::LittleEndian),
];
v.sort();
assert_eq!(v, [-5, 7, 300]);
assert!(Number::new_from_i32(-300, ByteOrder::BigEndian) < -5);1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for Number
impl PartialEq for Number
Source§fn eq(&self, other: &Number) -> bool
fn eq(&self, other: &Number) -> bool
Whether the numbers are the same integer, whatever their byte orders and however their magnitudes are stored.
§Examples
use a::math::numbers::{ByteOrder, Number};
let big = Number::new_from_u32(0x0102_0304, ByteOrder::BigEndian);
let little = Number::new_from_u32(0x0102_0304, ByteOrder::LittleEndian);
assert_eq!(big, little);
// Zero bytes above the most significant one count for nothing, and zero has no sign.
assert_eq!(Number::new_signed(vec![0], true, ByteOrder::BigEndian), Number::default());
assert_ne!(Number::new_from_i8(-1, ByteOrder::BigEndian), Number::new_from_i8(1, ByteOrder::BigEndian));Source§impl PartialOrd for Number
impl PartialOrd for Number
Source§fn partial_cmp(&self, other: &Number) -> Option<Ordering>
fn partial_cmp(&self, other: &Number) -> Option<Ordering>
Compares the numbers by value, as Ord::cmp does.
Source§impl PartialOrd<i8> for Number
impl PartialOrd<i8> for Number
Source§fn partial_cmp(&self, other: &i8) -> Option<Ordering>
fn partial_cmp(&self, other: &i8) -> Option<Ordering>
Compares the number with the integer other by value.
Source§impl PartialOrd<i16> for Number
impl PartialOrd<i16> for Number
Source§fn partial_cmp(&self, other: &i16) -> Option<Ordering>
fn partial_cmp(&self, other: &i16) -> Option<Ordering>
Compares the number with the integer other by value.
Source§impl PartialOrd<i32> for Number
impl PartialOrd<i32> for Number
Source§fn partial_cmp(&self, other: &i32) -> Option<Ordering>
fn partial_cmp(&self, other: &i32) -> Option<Ordering>
Compares the number with the integer other by value.
Source§impl PartialOrd<i64> for Number
impl PartialOrd<i64> for Number
Source§fn partial_cmp(&self, other: &i64) -> Option<Ordering>
fn partial_cmp(&self, other: &i64) -> Option<Ordering>
Compares the number with the integer other by value.
Source§impl PartialOrd<i128> for Number
impl PartialOrd<i128> for Number
Source§fn partial_cmp(&self, other: &i128) -> Option<Ordering>
fn partial_cmp(&self, other: &i128) -> Option<Ordering>
Compares the number with the integer other by value.
Source§impl PartialOrd<isize> for Number
impl PartialOrd<isize> for Number
Source§fn partial_cmp(&self, other: &isize) -> Option<Ordering>
fn partial_cmp(&self, other: &isize) -> Option<Ordering>
Compares the number with the integer other by value.
Source§impl PartialOrd<u8> for Number
impl PartialOrd<u8> for Number
Source§fn partial_cmp(&self, other: &u8) -> Option<Ordering>
fn partial_cmp(&self, other: &u8) -> Option<Ordering>
Compares the number with the integer other by value.
Source§impl PartialOrd<u16> for Number
impl PartialOrd<u16> for Number
Source§fn partial_cmp(&self, other: &u16) -> Option<Ordering>
fn partial_cmp(&self, other: &u16) -> Option<Ordering>
Compares the number with the integer other by value.
Source§impl PartialOrd<u32> for Number
impl PartialOrd<u32> for Number
Source§fn partial_cmp(&self, other: &u32) -> Option<Ordering>
fn partial_cmp(&self, other: &u32) -> Option<Ordering>
Compares the number with the integer other by value.
Source§impl PartialOrd<u64> for Number
impl PartialOrd<u64> for Number
Source§fn partial_cmp(&self, other: &u64) -> Option<Ordering>
fn partial_cmp(&self, other: &u64) -> Option<Ordering>
Compares the number with the integer other by value.
Source§impl PartialOrd<u128> for Number
impl PartialOrd<u128> for Number
Source§fn partial_cmp(&self, other: &u128) -> Option<Ordering>
fn partial_cmp(&self, other: &u128) -> Option<Ordering>
Compares the number with the integer other by value.
Source§impl PartialOrd<usize> for Number
impl PartialOrd<usize> for Number
Source§fn partial_cmp(&self, other: &usize) -> Option<Ordering>
fn partial_cmp(&self, other: &usize) -> Option<Ordering>
Compares the number with the integer other by value.
Source§impl Product for Number
impl Product for Number
Source§fn product<I: Iterator<Item = Number>>(iter: I) -> Number
fn product<I: Iterator<Item = Number>>(iter: I) -> Number
Returns the product of the numbers, in the byte order of the first and in the shortest form, multiplied as a balanced tree; one, little-endian, for none.
§Examples
use a::math::numbers::Number;
let factorial: Number = (1..=25u32).map(Number::from).product();
assert_eq!(factorial.to_string(), "15511210043330985984000000");Source§impl Rem for Number
impl Rem for Number
Source§fn rem(self, rhs: Number) -> Number
fn rem(self, rhs: Number) -> Number
Returns self % rhs as &self % &rhs does, as self %= rhs does.
The remainder is built in the vector of self, which has room for it unless self is an
empty vector, and then in the vector of rhs.
§Panics
When rhs is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_i32(-1000, ByteOrder::BigEndian);
let b = Number::new_from_i32(7, ByteOrder::BigEndian);
let remainder = a % b;
// -1000 = 7 (-142) - 6.
assert_eq!(remainder.value, [6]);
assert!(remainder.is_negative());Source§impl Rem<&Divisor> for &Number
impl Rem<&Divisor> for &Number
Source§fn rem(self, d: &Divisor) -> Number
fn rem(self, d: &Divisor) -> Number
Returns self % d, with the sign of self, as the remainder by the number d was made
from, in one new vector of exactly its length.
§Examples
use a::math::numbers::{ByteOrder, Divisor, Number};
let d = Divisor::new(&Number::new_from_i8(-7, ByteOrder::BigEndian));
let r = &Number::new_from_i32(100, ByteOrder::LittleEndian) % &d;
assert_eq!(r.value, [2]);
assert!(!r.is_negative());Source§impl Rem<&Divisor> for Number
impl Rem<&Divisor> for Number
Source§fn rem(self, d: &Divisor) -> Number
fn rem(self, d: &Divisor) -> Number
Returns self % d, as self %= d does, in the vector of self.
§Examples
use a::math::numbers::{ByteOrder, Divisor, Number};
let d = Divisor::new(&Number::new_from_u16(1000, ByteOrder::BigEndian));
let r = Number::new_from_u32(1_234_567, ByteOrder::BigEndian) % &d;
assert_eq!(r.value, [0x02, 0x37]);Source§impl Rem<&Number> for &Number
impl Rem<&Number> for &Number
Source§fn rem(self, rhs: &Number) -> Number
fn rem(self, rhs: &Number) -> Number
Returns self % rhs, the remainder of the division truncated toward zero, in the byte
order of self, in the shortest form: it has the sign of self, unless it is zero, and a
magnitude below that of rhs.
The remainder is built in one new vector of exactly its length (one byte for zero); both operands are left as they were.
§Panics
When rhs is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
// -7 % 2 = -1 and 7 % -2 = 1, as for `i32`.
let two = Number::new_from_i32(2, ByteOrder::BigEndian);
let remainder = &Number::new_from_i32(-7, ByteOrder::BigEndian) % &two;
assert_eq!(remainder.value, [1]);
assert!(remainder.is_negative());
let minus_two = Number::new_from_i32(-2, ByteOrder::BigEndian);
let remainder = &Number::new_from_i32(7, ByteOrder::BigEndian) % &minus_two;
assert_eq!(remainder.value, [1]);
assert!(!remainder.is_negative());Source§impl Rem<&Number> for Number
impl Rem<&Number> for Number
Source§fn rem(self, rhs: &Number) -> Number
fn rem(self, rhs: &Number) -> Number
Returns self % rhs as &self % rhs does, as self %= rhs does.
The remainder is built in the vector of self, which has room for it unless self is an
empty vector; rhs is left as it was.
§Panics
When rhs is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_u32(1_000_003, ByteOrder::LittleEndian);
let pointer = a.value.as_ptr();
let remainder = a % &Number::new_from_u16(1000, ByteOrder::BigEndian);
assert_eq!(remainder.value, [3]);
assert_eq!(remainder.value.as_ptr(), pointer);Source§impl Rem<Number> for &Number
impl Rem<Number> for &Number
Source§fn rem(self, rhs: Number) -> Number
fn rem(self, rhs: Number) -> Number
Returns self % rhs as &self % &rhs does.
The remainder is built in the vector of rhs, which always has room for it, as it is
shorter than rhs, also when rhs is in the other byte order. self is left as it was.
§Panics
When rhs is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_u32(0x01_02_03, ByteOrder::BigEndian);
let b = Number::new_from_u16(0x0100, ByteOrder::LittleEndian);
let pointer = b.value.as_ptr();
// 0x01_02_03 % 0x01_00 = 3, big-endian, in the vector of `b`.
let remainder = &a % b;
assert_eq!(remainder.value, [3]);
assert_eq!(remainder.value.as_ptr(), pointer);Source§impl RemAssign for Number
impl RemAssign for Number
Source§fn rem_assign(&mut self, rhs: Number)
fn rem_assign(&mut self, rhs: Number)
Sets self to self % rhs, keeping the byte order of self, in the shortest form.
The remainder is built in the vector of self, which has room for it unless self is an
empty vector, and then in the vector of rhs.
§Panics
When rhs is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
let mut n = Number::new_from_i32(-0x0105, ByteOrder::BigEndian);
n %= Number::new_from_i32(-0x0100, ByteOrder::BigEndian);
assert_eq!(n.value, [5]);
assert!(n.is_negative());Source§impl RemAssign<&Divisor> for Number
impl RemAssign<&Divisor> for Number
Source§fn rem_assign(&mut self, d: &Divisor)
fn rem_assign(&mut self, d: &Divisor)
Sets self to self % d, keeping the byte order of self, in its vector, which has room
for the remainder unless it is empty.
§Examples
use a::math::numbers::{ByteOrder, Divisor, Number};
let d = Divisor::new(&Number::new_from_u8(9, ByteOrder::BigEndian));
let mut n = Number::new_from_i32(-100, ByteOrder::LittleEndian);
n %= &d;
assert_eq!(n.value, [1]);
assert!(n.is_negative());Source§impl RemAssign<&Number> for Number
impl RemAssign<&Number> for Number
Source§fn rem_assign(&mut self, rhs: &Number)
fn rem_assign(&mut self, rhs: &Number)
Sets self to self % rhs, keeping the byte order of self, in the shortest form.
The remainder is built in the vector of self, which has room for it unless self is an
empty vector; rhs is left as it was.
§Panics
When rhs is zero.
§Examples
use a::math::numbers::{ByteOrder, Number};
let mut n = Number::new_from_u32(0x0102_0304, ByteOrder::LittleEndian);
n %= &Number::new_from_u32(0x0001_0000, ByteOrder::BigEndian);
assert_eq!(n.value, [0x04, 0x03]);Source§impl RemAssign<i8> for Number
impl RemAssign<i8> for Number
Source§fn rem_assign(&mut self, rhs: i8)
fn rem_assign(&mut self, rhs: i8)
Makes self self % rhs, as with rhs a number, in the vector of self when it has room.
Source§impl RemAssign<i16> for Number
impl RemAssign<i16> for Number
Source§fn rem_assign(&mut self, rhs: i16)
fn rem_assign(&mut self, rhs: i16)
Makes self self % rhs, as with rhs a number, in the vector of self when it has room.
Source§impl RemAssign<i32> for Number
impl RemAssign<i32> for Number
Source§fn rem_assign(&mut self, rhs: i32)
fn rem_assign(&mut self, rhs: i32)
Makes self self % rhs, as with rhs a number, in the vector of self when it has room.
Source§impl RemAssign<i64> for Number
impl RemAssign<i64> for Number
Source§fn rem_assign(&mut self, rhs: i64)
fn rem_assign(&mut self, rhs: i64)
Makes self self % rhs, as with rhs a number, in the vector of self when it has room.
Source§impl RemAssign<i128> for Number
impl RemAssign<i128> for Number
Source§fn rem_assign(&mut self, rhs: i128)
fn rem_assign(&mut self, rhs: i128)
Makes self self % rhs, as with rhs a number, in the vector of self when it has room.
Source§impl RemAssign<isize> for Number
impl RemAssign<isize> for Number
Source§fn rem_assign(&mut self, rhs: isize)
fn rem_assign(&mut self, rhs: isize)
Makes self self % rhs, as with rhs a number, in the vector of self when it has room.
Source§impl RemAssign<u8> for Number
impl RemAssign<u8> for Number
Source§fn rem_assign(&mut self, rhs: u8)
fn rem_assign(&mut self, rhs: u8)
Makes self self % rhs, as with rhs a number, in the vector of self when it has room.
Source§impl RemAssign<u16> for Number
impl RemAssign<u16> for Number
Source§fn rem_assign(&mut self, rhs: u16)
fn rem_assign(&mut self, rhs: u16)
Makes self self % rhs, as with rhs a number, in the vector of self when it has room.
Source§impl RemAssign<u32> for Number
impl RemAssign<u32> for Number
Source§fn rem_assign(&mut self, rhs: u32)
fn rem_assign(&mut self, rhs: u32)
Makes self self % rhs, as with rhs a number, in the vector of self when it has room.
Source§impl RemAssign<u64> for Number
impl RemAssign<u64> for Number
Source§fn rem_assign(&mut self, rhs: u64)
fn rem_assign(&mut self, rhs: u64)
Makes self self % rhs, as with rhs a number, in the vector of self when it has room.
Source§impl RemAssign<u128> for Number
impl RemAssign<u128> for Number
Source§fn rem_assign(&mut self, rhs: u128)
fn rem_assign(&mut self, rhs: u128)
Makes self self % rhs, as with rhs a number, in the vector of self when it has room.
Source§impl RemAssign<usize> for Number
impl RemAssign<usize> for Number
Source§fn rem_assign(&mut self, rhs: usize)
fn rem_assign(&mut self, rhs: usize)
Makes self self % rhs, as with rhs a number, in the vector of self when it has room.
Source§impl Shl<usize> for Number
impl Shl<usize> for Number
Source§fn shl(self, rhs: usize) -> Number
fn shl(self, rhs: usize) -> Number
Returns self · 2^rhs, in the byte order of self, in the shortest form, as
self <<= rhs does.
The result is built in the vector of self when its capacity has room for it, which
allocates nothing, and otherwise in one new vector of exactly its length.
§Panics
When the result is too long to be allocated.
§Examples
use a::math::numbers::{ByteOrder, Number};
let mut value = Vec::with_capacity(4);
value.extend([0x81, 0x02]);
let a = Number::new(value, ByteOrder::BigEndian);
let pointer = a.value.as_ptr();
// 0x81_02 << 9 = 0x01_02_04_00, in the vector of `a`.
let shifted = a << 9;
assert_eq!(shifted.value, [0x01, 0x02, 0x04, 0x00]);
assert_eq!(shifted.value.as_ptr(), pointer);Source§impl Shl<usize> for &Number
impl Shl<usize> for &Number
Source§fn shl(self, rhs: usize) -> Number
fn shl(self, rhs: usize) -> Number
Returns self · 2^rhs, in the byte order of self, in the shortest form.
The result is built in one new vector of exactly its length (one byte for zero), the
only allocation; self is left as it was. It may have any sign and length, and need not
be in the shortest form.
§Panics
When the result is too long to be allocated.
§Examples
use a::math::numbers::{ByteOrder, Number};
// -3 << 8 = -0x03_00.
let a = Number::new_from_i32(-3, ByteOrder::LittleEndian);
let shifted = &a << 8;
assert_eq!(shifted.value, [0x00, 0x03]);
assert!(shifted.is_negative());
// Zero stays zero, however far it is shifted.
let zero = Number::new_from_u8(0, ByteOrder::BigEndian);
assert_eq!((&zero << usize::MAX).value, [0]);Source§impl ShlAssign<usize> for Number
impl ShlAssign<usize> for Number
Source§fn shl_assign(&mut self, rhs: usize)
fn shl_assign(&mut self, rhs: usize)
Multiplies self by 2^rhs, keeping its byte order, in the shortest form.
The result is built in the vector of self when its capacity has room for it, which
allocates nothing, and otherwise in one new vector of exactly its length, which replaces
it.
§Panics
When the result is too long to be allocated.
§Examples
use a::math::numbers::{ByteOrder, Number};
let mut n = Number::new_from_u8(0xFF, ByteOrder::LittleEndian);
n <<= 4;
assert_eq!(n.value, [0xF0, 0x0F]);
n <<= 16;
assert_eq!(n.value, [0x00, 0x00, 0xF0, 0x0F]);Source§impl Shr<usize> for Number
impl Shr<usize> for Number
Source§fn shr(self, rhs: usize) -> Number
fn shr(self, rhs: usize) -> Number
Returns self / 2^rhs rounded towards negative infinity, in the byte order of self,
in the shortest form, as self >>= rhs does.
The result is built in the vector of self, which always has room for it unless it is
an empty vector without capacity; nothing is allocated then.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_u32(0x01_02_03, ByteOrder::BigEndian);
let pointer = a.value.as_ptr();
// 0x01_02_03 >> 9 = 0x81, in the vector of `a`.
let shifted = a >> 9;
assert_eq!(shifted.value, [0x81]);
assert_eq!(shifted.value.as_ptr(), pointer);Source§impl Shr<usize> for &Number
impl Shr<usize> for &Number
Source§fn shr(self, rhs: usize) -> Number
fn shr(self, rhs: usize) -> Number
Returns self / 2^rhs rounded towards negative infinity, in the byte order of self,
in the shortest form.
The result is built in one new vector of exactly its length (one byte for zero), the
only allocation; self is left as it was. It may have any sign and length, and need not
be in the shortest form.
§Examples
use a::math::numbers::{ByteOrder, Number};
// As for i32: -7 >> 1 = -4, 7 >> 1000 = 0 and -7 >> 1000 = -1.
let a = Number::new_from_i32(-7, ByteOrder::BigEndian);
let shifted = &a >> 1;
assert_eq!(shifted.value, [4]);
assert!(shifted.is_negative());
let shifted = &a >> 1000;
assert_eq!(shifted.value, [1]);
assert!(shifted.is_negative());
let b = Number::new_from_i32(7, ByteOrder::BigEndian);
assert_eq!((&b >> 1000).value, [0]);Source§impl ShrAssign<usize> for Number
impl ShrAssign<usize> for Number
Source§fn shr_assign(&mut self, rhs: usize)
fn shr_assign(&mut self, rhs: usize)
Divides self by 2^rhs, rounding towards negative infinity, keeping its byte order, in
the shortest form.
The result is built in the vector of self, which always has room for it unless it is
an empty vector without capacity; nothing is allocated then.
§Examples
use a::math::numbers::{ByteOrder, Number};
// -0x01_FF >> 1 = -0x01_00: the bit shifted out rounds the quotient down, which carries.
let mut n = Number::new_from_i32(-0x01FF, ByteOrder::LittleEndian);
n >>= 1;
assert_eq!(n.value, [0x00, 0x01]);
assert!(n.is_negative());
n >>= 9;
assert_eq!(n.value, [1]);
assert!(n.is_negative());Source§impl Sub for Number
impl Sub for Number
Source§fn sub(self, rhs: Number) -> Number
fn sub(self, rhs: Number) -> Number
Returns self - rhs, in the byte order of self, in the shortest form, as
self -= rhs does.
The difference is built in the vector of self when its capacity has room for it, or
else in the vector of rhs when that has room, also when rhs is in the other byte
order, either of which allocates nothing; otherwise it gets one new vector of exactly
its length.
§Examples
use a::math::numbers::{ByteOrder, Number};
// 0x01_00 - 0x02_FF = -0x01_FF.
let a = Number::new_from_i32(0x0100, ByteOrder::LittleEndian);
let b = Number::new_from_i32(0x02FF, ByteOrder::BigEndian);
let difference = a - b;
assert_eq!(difference.value, [0xFF, 0x01]);
assert!(difference.is_negative());Source§impl Sub<&Number> for &Number
impl Sub<&Number> for &Number
Source§fn sub(self, rhs: &Number) -> Number
fn sub(self, rhs: &Number) -> Number
Returns self - rhs, in the byte order of self, in the shortest form.
The difference is built in one new vector of exactly its length (one byte for zero), the only allocation; both operands are left as they were. The operands may have any signs, byte orders and lengths, and need not be in the shortest form.
§Examples
use a::math::numbers::{ByteOrder, Number};
// 0x01_00 - 0x01_01 = -1, in the byte order of the left operand.
let a = Number::new_from_i32(0x0100, ByteOrder::BigEndian);
let b = Number::new_from_i32(0x0101, ByteOrder::LittleEndian);
let difference = &a - &b;
assert_eq!(difference.value, [1]);
assert!(difference.is_negative());
assert!(difference.byte_order() == ByteOrder::BigEndian);
// A number minus itself is zero, one zero byte and not negative.
let zero = &a - &a;
assert_eq!(zero.value, [0]);
assert!(!zero.is_negative());Source§impl Sub<&Number> for Number
impl Sub<&Number> for Number
Source§fn sub(self, rhs: &Number) -> Number
fn sub(self, rhs: &Number) -> Number
Returns self - rhs, in the byte order of self, in the shortest form, as
self -= rhs does.
The difference is built in the vector of self when its capacity has room for it, which
allocates nothing, and otherwise in one new vector of exactly its length. rhs is left
as it was.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new(vec![1, 0, 0], ByteOrder::BigEndian);
let pointer = a.value.as_ptr();
let one = Number::new_from_u8(1, ByteOrder::BigEndian);
// 0x01_00_00 - 1 = 0xFF_FF, in the vector of `a`.
let difference = a - &one;
assert_eq!(difference.value, [0xFF, 0xFF]);
assert_eq!(difference.value.as_ptr(), pointer);Source§impl Sub<Number> for &Number
impl Sub<Number> for &Number
Source§fn sub(self, rhs: Number) -> Number
fn sub(self, rhs: Number) -> Number
Returns self - rhs, in the byte order of self, in the shortest form.
The difference is built in the vector of rhs when its capacity has room for it, which
allocates nothing, also when rhs is in the other byte order; otherwise it gets one new
vector of exactly its length. self is left as it was.
§Examples
use a::math::numbers::{ByteOrder, Number};
let a = Number::new_from_u32(0x01_02_03, ByteOrder::BigEndian);
let mut value = Vec::with_capacity(3);
value.extend([0x10, 0x20]);
let b = Number::new(value, ByteOrder::LittleEndian);
let pointer = b.value.as_ptr();
// 0x01_02_03 - 0x20_10 = 0xE1_F3, big-endian, in the vector of `b`.
let difference = &a - b;
assert_eq!(difference.value, [0xE1, 0xF3]);
assert_eq!(difference.value.as_ptr(), pointer);Source§impl SubAssign for Number
impl SubAssign for Number
Source§fn sub_assign(&mut self, rhs: Number)
fn sub_assign(&mut self, rhs: Number)
Subtracts rhs from self, keeping the byte order of self, in the shortest form.
The difference is built in the vector of self when its capacity has room for it, or
else in the vector of rhs when that has room, also when rhs is in the other byte
order, either of which allocates nothing; otherwise it gets one new vector of exactly
its length.
§Examples
use a::math::numbers::{ByteOrder, Number};
// `n` has no room for 0xFF - (-1) = 0x01_00, but the vector of the right operand has.
let mut n = Number::new_from_u8(0xFF, ByteOrder::BigEndian);
let mut value = Vec::with_capacity(2);
value.push(1);
let minus_one = Number::new_signed(value, true, ByteOrder::BigEndian);
let pointer = minus_one.value.as_ptr();
n -= minus_one;
assert_eq!(n.value, [1, 0]);
assert_eq!(n.value.as_ptr(), pointer);Source§impl SubAssign<&Number> for Number
impl SubAssign<&Number> for Number
Source§fn sub_assign(&mut self, rhs: &Number)
fn sub_assign(&mut self, rhs: &Number)
Subtracts rhs from self, keeping the byte order of self, in the shortest form.
The difference is built in the vector of self when its capacity has room for it, which
allocates nothing, and otherwise in one new vector of exactly its length, which replaces
it. rhs is left as it was.
§Examples
use a::math::numbers::{ByteOrder, Number};
let mut n = Number::new_from_u16(0x0200, ByteOrder::LittleEndian);
n -= &Number::new_from_u8(1, ByteOrder::BigEndian);
assert_eq!(n.value, [0xFF, 0x01]);
n -= &Number::new_from_u16(0x0200, ByteOrder::BigEndian);
assert_eq!(n.value, [1]);
assert!(n.is_negative());Source§impl SubAssign<i8> for Number
impl SubAssign<i8> for Number
Source§fn sub_assign(&mut self, rhs: i8)
fn sub_assign(&mut self, rhs: i8)
Makes self self - rhs, as with rhs a number, in the vector of self when it has room.
Source§impl SubAssign<i16> for Number
impl SubAssign<i16> for Number
Source§fn sub_assign(&mut self, rhs: i16)
fn sub_assign(&mut self, rhs: i16)
Makes self self - rhs, as with rhs a number, in the vector of self when it has room.
Source§impl SubAssign<i32> for Number
impl SubAssign<i32> for Number
Source§fn sub_assign(&mut self, rhs: i32)
fn sub_assign(&mut self, rhs: i32)
Makes self self - rhs, as with rhs a number, in the vector of self when it has room.
Source§impl SubAssign<i64> for Number
impl SubAssign<i64> for Number
Source§fn sub_assign(&mut self, rhs: i64)
fn sub_assign(&mut self, rhs: i64)
Makes self self - rhs, as with rhs a number, in the vector of self when it has room.
Source§impl SubAssign<i128> for Number
impl SubAssign<i128> for Number
Source§fn sub_assign(&mut self, rhs: i128)
fn sub_assign(&mut self, rhs: i128)
Makes self self - rhs, as with rhs a number, in the vector of self when it has room.
Source§impl SubAssign<isize> for Number
impl SubAssign<isize> for Number
Source§fn sub_assign(&mut self, rhs: isize)
fn sub_assign(&mut self, rhs: isize)
Makes self self - rhs, as with rhs a number, in the vector of self when it has room.
Source§impl SubAssign<u8> for Number
impl SubAssign<u8> for Number
Source§fn sub_assign(&mut self, rhs: u8)
fn sub_assign(&mut self, rhs: u8)
Makes self self - rhs, as with rhs a number, in the vector of self when it has room.
Source§impl SubAssign<u16> for Number
impl SubAssign<u16> for Number
Source§fn sub_assign(&mut self, rhs: u16)
fn sub_assign(&mut self, rhs: u16)
Makes self self - rhs, as with rhs a number, in the vector of self when it has room.
Source§impl SubAssign<u32> for Number
impl SubAssign<u32> for Number
Source§fn sub_assign(&mut self, rhs: u32)
fn sub_assign(&mut self, rhs: u32)
Makes self self - rhs, as with rhs a number, in the vector of self when it has room.
Source§impl SubAssign<u64> for Number
impl SubAssign<u64> for Number
Source§fn sub_assign(&mut self, rhs: u64)
fn sub_assign(&mut self, rhs: u64)
Makes self self - rhs, as with rhs a number, in the vector of self when it has room.
Source§impl SubAssign<u128> for Number
impl SubAssign<u128> for Number
Source§fn sub_assign(&mut self, rhs: u128)
fn sub_assign(&mut self, rhs: u128)
Makes self self - rhs, as with rhs a number, in the vector of self when it has room.
Source§impl SubAssign<usize> for Number
impl SubAssign<usize> for Number
Source§fn sub_assign(&mut self, rhs: usize)
fn sub_assign(&mut self, rhs: usize)
Makes self self - rhs, as with rhs a number, in the vector of self when it has room.
Source§impl Sum for Number
impl Sum for Number
Source§fn sum<I: Iterator<Item = Number>>(iter: I) -> Number
fn sum<I: Iterator<Item = Number>>(iter: I) -> Number
Returns the sum of the numbers, in the byte order of the first and in the shortest form, built in the vectors of the numbers; zero, little-endian, for none.
§Examples
use a::math::numbers::Number;
let sum: Number = (1..=100).map(Number::from).sum();
assert_eq!(sum, 5050);