#[repr(transparent)]pub struct Int<T>(pub T);
Expand description
🌐
Provides comprehensive integer operations on T
, most of them const.
It’s implemented for:
- all the integer primitives:
i8
, …,i128
,u8
, …,u128
.
Specific implementations can vary between signed and signed numeric types, but documentation is the same for all bit sizes:
i32
methods documentation related to: base, core, combinatorics, division, factors, modulo, primes, root.u32
methods documentation related to: base, core, combinatorics, division, factors, modulo, primes, root.
See also the related trait NumInt
.
Tuple Fields§
§0: T
Implementations§
Source§impl Int<i64>
Available on crate feature _int_i64
only.
impl Int<i64>
_int_i64
only.§Integer base related methods for i64
Sourcepub const fn digits(self) -> Int<i64>
pub const fn digits(self) -> Int<i64>
Returns the number of digits in base 10.
§Examples
assert_eq![Int(1), Int(0_i64).digits()];
assert_eq![Int(1), Int(-1_i64).digits()];
assert_eq![Int(3), Int(127_i64).digits()];
assert_eq![Int(3), Int(-128_i64).digits()];
Sourcepub const fn digits_sign(self) -> Int<i64>
pub const fn digits_sign(self) -> Int<i64>
Returns the number of digits in base 10, including the negative sign.
§Examples
assert_eq![Int(1), Int(0_i64).digits_sign()];
assert_eq![Int(2), Int(-1_i64).digits_sign()];
assert_eq![Int(3), Int(127_i64).digits_sign()];
assert_eq![Int(4), Int(-128_i64).digits_sign()];
Sourcepub const fn digits_base(self, base: i64) -> Int<i64>
pub const fn digits_base(self, base: i64) -> Int<i64>
Returns the number of digits in the given absolute base
.
If the base is 0, it returns 0.
§Examples
assert_eq![Int(2), Int(3_i64).digits_base(2)];
assert_eq![Int(2), Int(127_i64).digits_base(16)];
assert_eq![Int(2), Int(-128_i64).digits_base(16)];
assert_eq![Int(2), Int(-128_i64).digits_base(-16)];
assert_eq![Int(0), Int(100_i64).digits_base(0)];
assert_eq![Int(1), Int(0_i64).digits_base(100)];
Sourcepub const fn digits_base_sign(self, base: i64) -> Int<i64>
pub const fn digits_base_sign(self, base: i64) -> Int<i64>
Returns the number of digits in the given absolute base
,
including the negative sign.
If the base is 0, it returns 0.
§Examples
assert_eq![Int(2), Int(3_i64).digits_base_sign(2)];
assert_eq![Int(2), Int(127_i64).digits_base_sign(16)];
assert_eq![Int(3), Int(-128_i64).digits_base_sign(16)];
assert_eq![Int(3), Int(-128_i64).digits_base_sign(-16)];
assert_eq![Int(0), Int(100_i64).digits_base_sign(0)];
assert_eq![Int(1), Int(0_i64).digits_base_sign(100)];
Sourcepub const fn digital_root(self) -> Int<i64>
pub const fn digital_root(self) -> Int<i64>
Returns the digital root in base 10.
§Examples
assert_eq![Int(1), Int(127_i64).digital_root()];
assert_eq![Int(1), Int(-127_i64).digital_root()];
assert_eq![Int(9), Int(126_i64).digital_root()];
Sourcepub const fn digital_root_base(self, base: i64) -> Int<i64>
pub const fn digital_root_base(self, base: i64) -> Int<i64>
Returns the digital root in in the given absolute base
.
§Examples
assert_eq![Int(1), Int(127_i64).digital_root_base(10)];
assert_eq![Int(1), Int(127_i64).digital_root_base(-10)];
assert_eq![Int(1), Int(-127_i64).digital_root_base(-10)];
assert_eq![Int(9), Int(-126_i64).digital_root_base(10)];
assert_eq![Int(3), Int(-33_i64).digital_root_base(16)];
Source§impl Int<u32>
Available on crate feature _int_u32
only.
impl Int<u32>
_int_u32
only.§Integer base related methods for u32
Sourcepub const fn digits(self) -> Int<u32>
pub const fn digits(self) -> Int<u32>
Returns the number of digits in base 10.
§Examples
assert_eq![Int(1), Int(0_u32).digits()];
assert_eq![Int(3), Int(127_u32).digits()];
Sourcepub const fn digits_sign(self) -> Int<u32>
pub const fn digits_sign(self) -> Int<u32>
An alias of digits
.
Sourcepub const fn digits_base(self, base: u32) -> Int<u32>
pub const fn digits_base(self, base: u32) -> Int<u32>
Returns the number of digits in the given base
.
If the base is 0, it returns 0.
§Examples
assert_eq![Int(2), Int(3_u32).digits_base(2)];
assert_eq![Int(2), Int(127_u32).digits_base(16)];
assert_eq![Int(0), Int(100_u32).digits_base(0)];
assert_eq![Int(1), Int(0_u32).digits_base(100)];
Sourcepub const fn digits_base_sign(self, base: u32) -> Int<u32>
pub const fn digits_base_sign(self, base: u32) -> Int<u32>
An alias of digits_base
.
Sourcepub const fn digital_root(self) -> Int<u32>
pub const fn digital_root(self) -> Int<u32>
Returns the digital root in base 10.
§Examples
assert_eq![Int(1), Int(127_u32).digital_root()];
assert_eq![Int(9), Int(126_u32).digital_root()];
Source§impl Int<i64>
Available on crate feature _int_i64
only.
impl Int<i64>
_int_i64
only.§Integer combinatorics related methods for i64
Sourcepub const fn factorial(self) -> Result<Int<i64>>
pub const fn factorial(self) -> Result<Int<i64>>
Returns the factorial.
Permutations of n items, ordered, where $n = r$.
§Formula
$$ n! = 1 \cdot 2 \cdot 3 \cdot \ldots \cdot (n-1) \cdot n $$
These are the maximum numbers whose factorials can fit within standard signed integer types:
5 for i8
, 7 for i16
, 12 for i32
, 20 for i64
and 33 for i128
.
§Errors
Returns NonNegativeRequired
if $n<0$,
and Overflow
if the result can’t fit the type.
§Examples
assert_eq![Ok(Int(120)), Int(5_i64).factorial()];
assert_eq![Ok(Int(6)), Int(3_i64).factorial()];
assert_eq![Ok(Int(1)), Int(0_i64).factorial()];
assert![Int(-3_i64).factorial().is_err()];
assert![Int(i64::MAX).factorial().is_err()];
Sourcepub const fn subfactorial(self) -> Result<Int<i64>>
pub const fn subfactorial(self) -> Result<Int<i64>>
Returns the subfactorial, or the number of derangements.
Permutations of n items which no element appears in its original position.
§Formulation
§Algorithm
The current implementation uses the recursive definition: $$
- Base cases: ( !0 = 1 ) and ( !1 = 0 ).
- For ( n > 1 ), compute ( !(n - 1) ) and ( !(n - 2) ) recursively, and combine them: $$ \large !n = (n - 1) \cdot (!(n - 1) + !(n - 2)). $$
§Closed-Form Formulas
Other equivalent formulas for ( !n ) include:
- Summation Formula: $$ \large !n = n! \times \sum_{k=0}^{n} \frac{(-1)^k}{k!} $$
- Approximation Formula: $$ \large !n = \left\lfloor \frac{n!}{e} + \frac{1}{2} \right\rfloor = \left\lfloor n! \times \left(\frac{1}{e}\right) + \frac{1}{2} \right\rfloor $$
These are the maximum numbers whose subfactorials can fit within standard signed integer types:
5 for i8
, 8 for i16
, 12 for i32
, 20 for i64
and 35 for i128
.
§Errors
Returns NonNegativeRequired
if $n<0$,
and Overflow
if the result can’t fit the type.
§Panics
For very large values (e.g. i32:MAX
) the stack can overflow.
§Examples
assert_eq![Ok(Int(44)), Int(5_i64).subfactorial()];
assert_eq![Ok(Int(9)), Int(4_i64).subfactorial()];
assert_eq![Ok(Int(1)), Int(0_i64).subfactorial()];
assert![Int(-3_i64).subfactorial().is_err()];
assert![Int(127_i64).subfactorial().is_err()];
§Links
- The list of subfactorials is available in https://oeis.org/A000166.
Sourcepub const fn combine(self, r: i64) -> Result<Int<i64>>
pub const fn combine(self, r: i64) -> Result<Int<i64>>
Combinations of n
items taken r
at a time, ordered.
§Formula
$$ \large C(n,r) = {n \choose r} = \frac{n!}{(n−r)!r!} $$
§Errors
Returns NonNegativeRequired
if $n<0 \lor r<0$,
MismatchedSizes
if $r > n$, and
Overflow
if the result cant’t fit the type.
§Examples
assert_eq![Ok(Int(1)), Int(3_i64).combine(3)];
assert_eq![Ok(Int(3)), Int(3_i64).combine(2)];
assert_eq![Ok(Int(3)), Int(3_i64).combine(1)];
assert![Int(-3_i64).combine(3).is_err()];
assert![Int(3_i64).combine(-2).is_err()];
Sourcepub const fn combine_rep(self, r: i64) -> Result<Int<i64>>
pub const fn combine_rep(self, r: i64) -> Result<Int<i64>>
Combinations of n
items taken r
at a time with repetitions, unordered.
Also known as multichoose.
§Formula
$$ \large C(n+r-1,r) = {n+k-1 \choose r} = \frac{(n+r-1)!}{(n−1)!r!} $$
§Errors
Returns NonNegativeRequired
if $n<0 \lor r<0$,
Overflow
if the result cant’t fit the type.
§Examples
assert_eq![Ok(Int(10)), Int(3_i64).combine_rep(3)];
assert_eq![Ok(Int(6)), Int(3_i64).combine_rep(2)];
assert_eq![Ok(Int(3)), Int(3_i64).combine_rep(1)];
assert![Int(-3_i64).combine_rep(3).is_err()];
assert![Int(3_i64).combine_rep(-2).is_err()];
Sourcepub const fn permute(self, r: i64) -> Result<Int<i64>>
pub const fn permute(self, r: i64) -> Result<Int<i64>>
Permutations of n
items taken r
at a time, ordered.
When $n=r$ or $n=r-1$ the result is the same as calculating the factorial $n!$.
§Formula
$$ \large P(n,r) = \frac{n!}{(n−r)!} $$
§Errors
Returns NonNegativeRequired
if $n<0 \lor r<0$,
MismatchedSizes
if $|r| > |n|$, and
Overflow
if the result cant’t fit the type.
§Examples
assert_eq![Ok(Int(6)), Int(3_i64).permute(3)];
assert_eq![Ok(Int(6)), Int(3_i64).permute(2)];
assert_eq![Ok(Int(3)), Int(3_i64).permute(1)];
assert![Int(-3_i64).permute(3).is_err()];
assert![Int(3_i64).permute(-2).is_err()];
Sourcepub const fn permute_rep(self, r: i64) -> Result<Int<i64>>
Available on crate feature cast
only.
pub const fn permute_rep(self, r: i64) -> Result<Int<i64>>
cast
only.Permutations of n
items taken r
at a time with repetitions, ordered.
§Formula
$$ \large P_\text{rep}(n,r) = n_r $$
§Errors
Returns NonNegativeRequired
if $n<0 \lor r<0$,
and Overflow
if the result cant’t fit the type.
§Examples
assert_eq![Ok(Int(27)), Int(3_i64).permute_rep(3)];
assert_eq![Ok(Int(9)), Int(3_i64).permute_rep(2)];
assert_eq![Ok(Int(3)), Int(3_i64).permute_rep(1)];
assert![Int(-3_i64).permute_rep(3).is_err()];
assert![Int(3_i64).permute_rep(-2).is_err()];
Source§impl Int<u32>
Available on crate feature _int_u32
only.
impl Int<u32>
_int_u32
only.§Integer combinatorics related methods for u32
Sourcepub const fn factorial(self) -> Result<Int<u32>>
pub const fn factorial(self) -> Result<Int<u32>>
Returns the factorial.
Permutations of n items, ordered, where $n = r$.
§Formula
$$ n! = 1 \cdot 2 \cdot 3 \cdot \ldots \cdot (n-1) \cdot n $$
These are the maximum numbers whose factorials can fit within standard signed integer types:
5 for u8
, 8 for u16
, 12 for u32
, 20 for u64
and 34 for u128
.
§Errors
Returns Overflow
if the result can’t fit the type.
§Examples
assert_eq![Ok(Int(120)), Int(5_u32).factorial()];
assert_eq![Ok(Int(6)), Int(3_u32).factorial()];
assert_eq![Ok(Int(1)), Int(0_u32).factorial()];
assert![Int(u32::MAX).factorial().is_err()];
Sourcepub const fn subfactorial(self) -> Result<Int<u32>>
pub const fn subfactorial(self) -> Result<Int<u32>>
Returns the subfactorial, or the number of derangements.
Permutations of n items which no element appears in its original position.
§Formulation
§Algorithm
The current implementation uses the recursive definition: $$
- Base cases: ( !0 = 1 ) and ( !1 = 0 ).
- For ( n > 1 ), compute ( !(n - 1) ) and ( !(n - 2) ) recursively, and combine them: $$ \large !n = (n - 1) \cdot (!(n - 1) + !(n - 2)). $$
§Closed-Form Formulas
Other equivalent formulas for ( !n ) include:
- Summation Formula: $$ \large !n = n! \times \sum_{k=0}^{n} \frac{(-1)^k}{k!} $$
- Approximation Formula: $$ \large !n = \left\lfloor \frac{n!}{e} + \frac{1}{2} \right\rfloor = \left\lfloor n! \times \left(\frac{1}{e}\right) + \frac{1}{2} \right\rfloor $$
These are the maximum numbers whose subfactorials can fit within standard signed integer types:
5 for u8
, 8 for u16
, 13 for u32
, 20 for u64
and 35 for u128
.
§Errors
Returns Overflow
if the result can’t fit the type.
§Panics
For very large values (e.g. u32:MAX
) the stack can overflow.
§Examples
assert_eq![Ok(Int(44)), Int(5_u32).subfactorial()];
assert_eq![Ok(Int(9)), Int(4_u32).subfactorial()];
assert_eq![Ok(Int(1)), Int(0_u32).subfactorial()];
assert![Int(255_u32).subfactorial().is_err()];
§Links
- The list of subfactorials is available in https://oeis.org/A000166.
Sourcepub const fn combine(self, r: u32) -> Result<Int<u32>>
pub const fn combine(self, r: u32) -> Result<Int<u32>>
Combinations of n
items taken r
at a time, unordered.
§Formula
$$ \large C(n,r) = {n \choose r} = \frac{n!}{(n−r)!r!} $$
§Errors
Returns MismatchedSizes
if $r > n$ and
Overflow
if the result cant’t fit the type.
§Examples
assert_eq![Ok(Int(1)), Int(3_u32).combine(3)];
assert_eq![Ok(Int(3)), Int(3_u32).combine(2)];
assert_eq![Ok(Int(3)), Int(3_u32).combine(1)];
assert![Int(u32::MAX).combine(u32::MAX).is_err()];
Sourcepub const fn combine_rep(self, r: u32) -> Result<Int<u32>>
pub const fn combine_rep(self, r: u32) -> Result<Int<u32>>
Combinations of n
items taken r
at a time with repetitions, unordered.
Also known as multichoose.
§Formula
$$ \large C(n+r-1,r) = {n+k-1 \choose r} = \frac{(n+r-1)!}{(n−1)!r!} $$
§Errors
Returns Overflow
if the result cant’t fit the type.
§Examples
assert_eq![Ok(Int(10)), Int(3_u32).combine_rep(3)];
assert_eq![Ok(Int(6)), Int(3_u32).combine_rep(2)];
assert_eq![Ok(Int(3)), Int(3_u32).combine_rep(1)];
assert![Int(u32::MAX).combine_rep(u32::MAX).is_err()];
Sourcepub const fn permute(self, r: u32) -> Result<Int<u32>>
pub const fn permute(self, r: u32) -> Result<Int<u32>>
Permutations of n
items taken r
at a time, ordered.
When $n=r$ or $n=r-1$ the result is the same as calculating the factorial $n!$.
§Formula
$$ \large P(n,r) = \frac{n!}{(n−r)!} $$
§Errors
Returns MismatchedSizes
if $r > n$ and
Overflow
if the result cant’t fit the type.
§Examples
assert_eq![Ok(Int(6)), Int(3_u32).permute(3)];
assert_eq![Ok(Int(6)), Int(3_u32).permute(2)];
assert_eq![Ok(Int(3)), Int(3_u32).permute(1)];
assert![Int(3_u32).permute(4_u32).is_err()];
assert![Int(u32::MAX).permute(u32::MAX).is_err()];
Sourcepub const fn permute_rep(self, r: u32) -> Result<Int<u32>>
Available on crate feature cast
only.
pub const fn permute_rep(self, r: u32) -> Result<Int<u32>>
cast
only.Permutations of n
items taken r
at a time with repetitions, ordered.
§Formula
$$ \large P_\text{rep}(n,r) = n_r $$
§Errors
Returns Overflow
if the result cant’t fit the type.
§Examples
assert_eq![Ok(Int(27)), Int(3_u32).permute_rep(3)];
assert_eq![Ok(Int(9)), Int(3_u32).permute_rep(2)];
assert_eq![Ok(Int(3)), Int(3_u32).permute_rep(1)];
assert![Int(u32::MAX).permute_rep(u32::MAX).is_err()];
Source§impl Int<i64>
Available on crate feature _int_i64
only.
impl Int<i64>
_int_i64
only.§Integer core methods for i64
Sourcepub const fn is_even(self) -> bool
pub const fn is_even(self) -> bool
Returns true
if self
is an even number.
§Examples
assert![Int(2_i64).is_even()];
assert![Int(-2_i64).is_even()];
assert![!Int(3_i64).is_even()];
assert![Int(0_i64).is_even()];
Sourcepub const fn is_odd(self) -> bool
pub const fn is_odd(self) -> bool
Returns true
if self
is an odd number.
§Examples
assert![Int(3_i64).is_odd()];
assert![Int(-3_i64).is_odd()];
assert![!Int(2_i64).is_odd()];
assert![!Int(0_i64).is_odd()];
Sourcepub const fn gcd(self, b: i64) -> Int<i64>
pub const fn gcd(self, b: i64) -> Int<i64>
Returns the GCD.
Uses Stein’s algorithm which is much more efficient to compute than Euclid’s.
§Examples
assert_eq![Int(4), Int(64_i64).gcd(36)];
assert_eq![Int(4), Int(-64_i64).gcd(36)];
assert_eq![Int(4), Int(64_i64).gcd(-36)];
assert_eq![Int(4), Int(-64_i64).gcd(-36)];
assert_eq![Int(36), Int(0_i64).gcd(36)];
assert_eq![Int(64), Int(64_i64).gcd(0)];
Sourcepub const fn gcd_ext(self, b: i64) -> GcdReturn<Int<i64>, Int<i64>>
pub const fn gcd_ext(self, b: i64) -> GcdReturn<Int<i64>, Int<i64>>
Returns the GCD and the Bézout coeficients.
This version uses the extended Stein’s algorithm which is much more efficient to compute than Euclid’s. It uses only simple arithmetic operations and works by dividing the inputs by 2 until they are odd, and then subtracting the smaller number from the larger one.
The Bézout’s coefficients are not unique, and different algorithms can yield different coefficients that all satisfy Bézout’s identity.
Bézout’s identity states that for any two integers a and b, there exist integers x and y such that $ax + by = gcd(a, b)$.
§Examples
let (gcd, x, y) = Int(32_i64).gcd_ext(36).as_tuple();
assert_eq!(gcd.0, 4);
assert_eq!(x.0 * 32 + y.0 * 36, gcd.0);
Sourcepub const fn gcd_ext_euc(self, b: i64) -> GcdReturn<Int<i64>, Int<i64>>
pub const fn gcd_ext_euc(self, b: i64) -> GcdReturn<Int<i64>, Int<i64>>
Returns the GCD and the Bézout coeficients.
This version uses the extended Euclids’s algorithm, which uses a series of euclidean divisions and works by subtracting multiples of the smaller number from the larger one.
The Bézout’s coefficients are not unique, and different algorithms can yield different coefficients that all satisfy Bézout’s identity.
§Examples
let (gcd, x, y) = Int(32_i64).gcd_ext_euc(36).as_tuple();
assert_eq!(gcd.0, 4);
assert_eq!(x.0 * 32 + y.0 * 36, gcd.0);
Sourcepub const fn scale(self, min: i64, max: i64, a: i64, b: i64) -> Result<Int<i64>>
Available on crate feature cast
only.
pub const fn scale(self, min: i64, max: i64, a: i64, b: i64) -> Result<Int<i64>>
cast
only.Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as i128
for the checked operations.
If the value lies outside of [min..=max]
it will result in extrapolation.
§Errors
Can Overflow
for extrapolated values that can’t fit the type,
and for overflowing arithmetic operations in the following formula:
§Formula
$$ \large v’ = (b - a) \frac{v - min}{max - min} + a $$
§Examples
assert_eq![Ok(Int(40)), Int(60_i64).scale(0, 120, 30, 50)]; // interpolate
assert_eq![Ok(Int(112)), Int(100_i64).scale(0, 80, 0, 90)]; // extrapolate
assert![Int(100_i8).scale(0, 50, 0, 90).is_err()]; // extrapolate and overflow
Sourcepub const fn scale_wrap(self, min: i64, max: i64, a: i64, b: i64) -> Int<i64>
pub const fn scale_wrap(self, min: i64, max: i64, a: i64, b: i64) -> Int<i64>
Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as i128
.
If the value lies outside of [min..=max]
it will result in extrapolation, and
if the value doesn’t fit in the type it will wrap around its boundaries.
§Panics
Could panic for large values of i128
or u128
.
§Formula
$$ \large v’ = (b - a) \frac{v - min}{max - min} + a $$
§Examples
assert_eq![Int(40), Int(60_i64).scale_wrap(0, 120, 30, 50)]; // interpolate
assert_eq![Int(112), Int(100_i64).scale_wrap(0, 80, 0, 90)]; // extrapolate
assert_eq![Int(-76), Int(100_i8).scale_wrap(0, 50, 0, 90)]; // extrapolate and wrap
Source§impl Int<u32>
Available on crate feature _int_u32
only.
impl Int<u32>
_int_u32
only.§Integer core methods for u32
Sourcepub const fn is_even(self) -> bool
pub const fn is_even(self) -> bool
Returns true
if self
is an even number.
§Examples
assert![Int(2_u32).is_even()];
assert![!Int(3_u32).is_even()];
assert![Int(0_u32).is_even()];
Sourcepub const fn is_odd(self) -> bool
pub const fn is_odd(self) -> bool
Returns true
if self
is an odd number.
§Examples
assert![Int(3_u32).is_odd()];
assert![!Int(2_u32).is_odd()];
assert![!Int(0_u32).is_odd()];
Sourcepub const fn gcd(self, b: u32) -> Int<u32>
pub const fn gcd(self, b: u32) -> Int<u32>
Returns the GCD.
Uses Stein’s algorithm which is much more efficient to compute than Euclid’s.
§Examples
assert_eq![Int(4), Int(64_u32).gcd(36)];
assert_eq![Int(36), Int(0_u32).gcd(36)];
assert_eq![Int(64), Int(64_u32).gcd(0)];
Sourcepub const fn gcd_ext(self, b: u32) -> Result<GcdReturn<Int<u32>, Int<i64>>>
Available on crate features _int_i64
and cast
only.
pub const fn gcd_ext(self, b: u32) -> Result<GcdReturn<Int<u32>, Int<i64>>>
_int_i64
and cast
only.Returns the GCD and the Bézout coeficients.
Performs inner operations and returns coefficients as i64
.
This version uses the extended Stein’s algorithm which is much more efficient to compute than Euclid’s. It uses only simple arithmetic operations and works by dividing the inputs by 2 until they are odd, and then subtracting the smaller number from the larger one.
The Bézout’s coefficients are not unique, and different algorithms can yield different coefficients that all satisfy Bézout’s identity.
Bézout’s identity states that for any two integers a and b, there exist integers x and y such that $ax + by = gcd(a, b)$.
§Errors
Can return Overflow
for u128
.
§Examples
let (gcd, x, y) = Int(32_u32).gcd_ext(36).unwrap().as_tuple();
assert_eq!(gcd.0, 4);
assert_eq![x.0 * 32 + y.0 * 36, gcd.0 as i64];
Sourcepub const fn gcd_ext_euc(self, b: u32) -> Result<GcdReturn<Int<u32>, Int<i64>>>
Available on crate features _int_i64
and cast
only.
pub const fn gcd_ext_euc(self, b: u32) -> Result<GcdReturn<Int<u32>, Int<i64>>>
_int_i64
and cast
only.Returns the GCD and the Bézout coeficients.
Performs inner operations and returns coefficients as i64
.
This version uses the extended Euclids’s algorithm, which uses a series of euclidean divisions and works by subtracting multiples of the smaller number from the larger one.
The Bézout’s coefficients are not unique, and different algorithms can yield different coefficients that all satisfy Bézout’s identity.
§Errors
Can return Overflow
for u128
.
§Examples
let (gcd, x, y) = Int(32_u32).gcd_ext_euc(36).unwrap().as_tuple();
assert_eq!(gcd.0, 4);
assert_eq![x.0 * 32 + y.0 * 36, gcd.0 as i64];
Sourcepub const fn scale(self, min: u32, max: u32, a: u32, b: u32) -> Result<Int<u32>>
Available on crate feature cast
only.
pub const fn scale(self, min: u32, max: u32, a: u32, b: u32) -> Result<Int<u32>>
cast
only.Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as u64
for the checked operations.
If the value lies outside of [min..=max]
it will result in extrapolation.
§Errors
Can Overflow
for extrapolated values that can’t fit the type,
and for overflowing arithmetic operations in the following formula:
§Formula
$$ \large v’ = (b - a) \frac{v - min}{max - min} + a $$
§Examples
assert_eq![Ok(Int(40)), Int(60_u32).scale(0, 120, 30, 50)]; // interpolate
assert_eq![Ok(Int(112)), Int(100_u32).scale(0, 80, 0, 90)]; // extrapolate
assert![Int(200_u8).scale(0, 50, 0, 90).is_err()]; // extrapolate and overflow
Sourcepub const fn scale_wrap(self, min: u32, max: u32, a: u32, b: u32) -> Int<u32>
pub const fn scale_wrap(self, min: u32, max: u32, a: u32, b: u32) -> Int<u32>
Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as u64
.
If the value lies outside of [min..=max]
it will result in extrapolation, and
if the value doesn’t fit in the type it will wrap around its boundaries.
§Panics
Could panic for large values of i128
or u128
.
§Formula
$$ \large v’ = (b - a) \frac{v - min}{max - min} + a $$
§Examples
assert_eq![Int(40), Int(60_u32).scale_wrap(0, 120, 30, 50)]; // interpolate
assert_eq![Int(112), Int(100_u32).scale_wrap(0, 80, 0, 90)]; // extrapolate
assert_eq![Int(104), Int(200_u8).scale_wrap(0, 50, 0, 90)]; // extrapolate and wrap
Source§impl Int<i64>
Available on crate feature _int_i64
only.
impl Int<i64>
_int_i64
only.§Integer division related methods for i64
Sourcepub const fn div_rem(self, b: i64) -> [Int<i64>; 2]
Available on crate feature _int_i64
only.
pub const fn div_rem(self, b: i64) -> [Int<i64>; 2]
_int_i64
only.Returns the truncated quotient and the remainder.
Sourcepub const fn div_ceil(self, b: i64) -> Int<i64>
Available on crate feature _int_i64
only.
pub const fn div_ceil(self, b: i64) -> Int<i64>
_int_i64
only.Returns the quotient, rounding the result towards positive infinity. $$ \large \left\lceil \frac{x}{y} \right\rceil $$
§Formulation
$$ \large \text{div\_ceil}(a, b) = \begin{cases} \left\lfloor \frac{a}{b} \right\rfloor + 1 & \text{if } (r > 0 \land b > 0) \lor (r < 0 \land b < 0), \cr \left\lfloor \frac{a}{b} \right\rfloor & \text{otherwise.} \end{cases} $$
§Examples
assert_eq![Int(7_i64).div_ceil(3), Int(3)]; // == 2.33…
assert_eq![Int(7_i64).div_ceil(-3), Int(-2)];
assert_eq![Int(-7_i64).div_ceil(3), Int(-2)];
assert_eq![Int(-7_i64).div_ceil(-3), Int(3)];
assert_eq![Int(7_i64).div_ceil(5), Int(2)]; // == 1.4
assert_eq![Int(6_i64).div_ceil(4), Int(2)]; // == 1.5
assert_eq![Int(8_i64).div_ceil(5), Int(2)]; // == 1.6
assert_eq![Int(5_i64).div_ceil(2), Int(3)]; // == 2.5
assert_eq![Int(-7_i64).div_ceil(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i64).div_ceil(4), Int(-1)]; // == -1.5
assert_eq![Int(-8_i64).div_ceil(5), Int(-1)]; // == -1.6
assert_eq![Int(-5_i64).div_ceil(2), Int(-2)]; // == -2.5
Sourcepub const fn div_floor(self, b: i64) -> Int<i64>
Available on crate feature _int_i64
only.
pub const fn div_floor(self, b: i64) -> Int<i64>
_int_i64
only.Returns the quotient, rounding the result towards negative infinity. $$ \large \left\lfloor \frac{x}{y} \right\rfloor $$
§Examples
assert_eq![Int(7_i64).div_floor(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i64).div_floor(-3), Int(-3)];
assert_eq![Int(-7_i64).div_floor(3), Int(-3)];
assert_eq![Int(-7_i64).div_floor(-3), Int(2)];
assert_eq![Int(7_i64).div_floor(5), Int(1)]; // == 1.4
assert_eq![Int(6_i64).div_floor(4), Int(1)]; // == 1.5
assert_eq![Int(8_i64).div_floor(5), Int(1)]; // == 1.6
assert_eq![Int(5_i64).div_floor(2), Int(2)]; // == 2.5
assert_eq![Int(-7_i64).div_floor(5), Int(-2)]; // == -1.4
assert_eq![Int(-6_i64).div_floor(4), Int(-2)]; // == -1.5
assert_eq![Int(-8_i64).div_floor(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i64).div_floor(2), Int(-3)]; // == -2.5
Sourcepub const fn div_ties_away(self, b: i64) -> Int<i64>
Available on crate feature _int_i64
only.
pub const fn div_ties_away(self, b: i64) -> Int<i64>
_int_i64
only.Returns the quotient, rounding ties away from zero.
§Examples
assert_eq![Int(7_i64).div_ties_away(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i64).div_ties_away(-3), Int(-2)];
assert_eq![Int(-7_i64).div_ties_away(3), Int(-2)];
assert_eq![Int(-7_i64).div_ties_away(-3), Int(2)];
assert_eq![Int(7_i64).div_ties_away(5), Int(1)]; // == 1.4
assert_eq![Int(6_i64).div_ties_away(4), Int(2)]; // == 1.5
assert_eq![Int(8_i64).div_ties_away(5), Int(2)]; // == 1.6
assert_eq![Int(5_i64).div_ties_away(2), Int(3)]; // == 2.5
assert_eq![Int(-7_i64).div_ties_away(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i64).div_ties_away(4), Int(-2)]; // == -1.5
assert_eq![Int(-8_i64).div_ties_away(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i64).div_ties_away(2), Int(-3)]; // == -2.5
Sourcepub const fn div_ties_towards(self, b: i64) -> Int<i64>
Available on crate feature _int_i64
only.
pub const fn div_ties_towards(self, b: i64) -> Int<i64>
_int_i64
only.Returns the quotient, rounding ties towards zero.
§Examples
assert_eq![Int(7_i64).div_ties_towards(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i64).div_ties_towards(-3), Int(-2)];
assert_eq![Int(-7_i64).div_ties_towards(3), Int(-2)];
assert_eq![Int(-7_i64).div_ties_towards(-3), Int(2)];
assert_eq![Int(7_i64).div_ties_towards(5), Int(1)]; // == 1.4
assert_eq![Int(6_i64).div_ties_towards(4), Int(1)]; // == 1.5
assert_eq![Int(8_i64).div_ties_towards(5), Int(2)]; // == 1.6
assert_eq![Int(5_i64).div_ties_towards(2), Int(2)]; // == 2.5
assert_eq![Int(-7_i64).div_ties_towards(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i64).div_ties_towards(4), Int(-1)]; // == -1.5
assert_eq![Int(-8_i64).div_ties_towards(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i64).div_ties_towards(2), Int(-2)]; // == -2.5
Sourcepub const fn div_ties_even(self, b: i64) -> Int<i64>
Available on crate feature _int_i64
only.
pub const fn div_ties_even(self, b: i64) -> Int<i64>
_int_i64
only.Returns the quotient, rounding ties to the nearest even number.
§Examples
assert_eq![Int(7_i64).div_ties_even(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i64).div_ties_even(-3), Int(-2)];
assert_eq![Int(-7_i64).div_ties_even(3), Int(-2)];
assert_eq![Int(-7_i64).div_ties_even(-3), Int(2)];
assert_eq![Int(7_i64).div_ties_even(5), Int(1)]; // == 1.4
assert_eq![Int(6_i64).div_ties_even(4), Int(2)]; // == 1.5
assert_eq![Int(8_i64).div_ties_even(5), Int(2)]; // == 1.6
assert_eq![Int(5_i64).div_ties_even(2), Int(2)]; // == 2.5
assert_eq![Int(-7_i64).div_ties_even(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i64).div_ties_even(4), Int(-2)]; // == -1.5
assert_eq![Int(-8_i64).div_ties_even(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i64).div_ties_even(2), Int(-2)]; // == -2.5
Sourcepub const fn div_ties_odd(self, b: i64) -> Int<i64>
Available on crate feature _int_i64
only.
pub const fn div_ties_odd(self, b: i64) -> Int<i64>
_int_i64
only.Returns the quotient, rounding ties to the nearest odd number.
§Examples
assert_eq![Int(7_i64).div_ties_odd(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i64).div_ties_odd(-3), Int(-2)];
assert_eq![Int(-7_i64).div_ties_odd(3), Int(-2)];
assert_eq![Int(-7_i64).div_ties_odd(-3), Int(2)];
assert_eq![Int(7_i64).div_ties_odd(5), Int(1)]; // == 1.4
assert_eq![Int(6_i64).div_ties_odd(4), Int(1)]; // == 1.5
assert_eq![Int(8_i64).div_ties_odd(5), Int(2)]; // == 1.6
assert_eq![Int(5_i64).div_ties_odd(2), Int(3)]; // == 2.5
assert_eq![Int(-7_i64).div_ties_odd(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i64).div_ties_odd(4), Int(-1)]; // == -1.5
assert_eq![Int(-8_i64).div_ties_odd(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i64).div_ties_odd(2), Int(-3)]; // == -2.5
Source§impl Int<u32>
Available on crate feature _int_u32
only.
impl Int<u32>
_int_u32
only.§Integer division related methods for u32
Sourcepub const fn div_rem(self, b: u32) -> [Int<u32>; 2]
pub const fn div_rem(self, b: u32) -> [Int<u32>; 2]
Returns the truncated quotient and the remainder.
Sourcepub const fn div_ceil(self, b: u32) -> Int<u32>
pub const fn div_ceil(self, b: u32) -> Int<u32>
Returns the quotient, rounding the result towards positive infinity. $$ \large \left\lceil \frac{x}{y} \right\rceil $$
§Examples
assert_eq![Int(7_u32).div_ceil(3), Int(3)]; // == 2.33…
assert_eq![Int(7_u32).div_ceil(5), Int(2)]; // == 1.4
assert_eq![Int(6_u32).div_ceil(4), Int(2)]; // == 1.5
assert_eq![Int(8_u32).div_ceil(5), Int(2)]; // == 1.6
assert_eq![Int(5_u32).div_ceil(2), Int(3)]; // == 2.5
Sourcepub const fn div_floor(self, b: u32) -> Int<u32>
pub const fn div_floor(self, b: u32) -> Int<u32>
Returns the quotient, rounding the result towards negative infinity. $$ \large \left\lfloor \frac{x}{y} \right\rfloor $$
§Examples
assert_eq![Int(7_u32).div_floor(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u32).div_floor(5), Int(1)]; // == 1.4
assert_eq![Int(6_u32).div_floor(4), Int(1)]; // == 1.5
assert_eq![Int(8_u32).div_floor(5), Int(1)]; // == 1.6
assert_eq![Int(5_u32).div_floor(2), Int(2)]; // == 2.5
Sourcepub const fn div_ties_away(self, b: u32) -> Int<u32>
pub const fn div_ties_away(self, b: u32) -> Int<u32>
Returns the quotient, rounding ties away from zero.
§Examples
assert_eq![Int(7_u32).div_ties_away(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u32).div_ties_away(5), Int(1)]; // == 1.4
assert_eq![Int(6_u32).div_ties_away(4), Int(2)]; // == 1.5
assert_eq![Int(8_u32).div_ties_away(5), Int(2)]; // == 1.6
assert_eq![Int(5_u32).div_ties_away(2), Int(3)]; // == 2.5
Sourcepub const fn div_ties_towards(self, b: u32) -> Int<u32>
pub const fn div_ties_towards(self, b: u32) -> Int<u32>
Returns the quotient, rounding ties towards zero.
§Examples
assert_eq![Int(7_u32).div_ties_towards(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u32).div_ties_towards(5), Int(1)]; // == 1.4
assert_eq![Int(6_u32).div_ties_towards(4), Int(1)]; // == 1.5
assert_eq![Int(8_u32).div_ties_towards(5), Int(2)]; // == 1.6
assert_eq![Int(5_u32).div_ties_towards(2), Int(2)]; // == 2.5
Sourcepub const fn div_ties_even(self, b: u32) -> Int<u32>
pub const fn div_ties_even(self, b: u32) -> Int<u32>
Returns the quotient, rounding ties to the nearest even number.
§Examples
assert_eq![Int(7_u32).div_ties_even(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u32).div_ties_even(5), Int(1)]; // == 1.4
assert_eq![Int(6_u32).div_ties_even(4), Int(2)]; // == 1.5
assert_eq![Int(8_u32).div_ties_even(5), Int(2)]; // == 1.6
assert_eq![Int(5_u32).div_ties_even(2), Int(2)]; // == 2.5
Sourcepub const fn div_ties_odd(self, b: u32) -> Int<u32>
pub const fn div_ties_odd(self, b: u32) -> Int<u32>
Returns the quotient, rounding ties to the nearest even number.
§Examples
assert_eq![Int(7_u32).div_ties_odd(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u32).div_ties_odd(5), Int(1)]; // == 1.4
assert_eq![Int(6_u32).div_ties_odd(4), Int(1)]; // == 1.5
assert_eq![Int(8_u32).div_ties_odd(5), Int(2)]; // == 1.6
assert_eq![Int(5_u32).div_ties_odd(2), Int(3)]; // == 2.5
Source§impl Int<i64>
Available on crate feature _int_i64
only.
impl Int<i64>
_int_i64
only.§Integer factors related methods for i64
- Allocating:
- Not allocating:
Sourcepub fn factors(self) -> Vec<i64> ⓘ
Available on crate feature alloc
only.
pub fn factors(self) -> Vec<i64> ⓘ
alloc
only.Returns the factors (including 1 and self).
§Examples
assert_eq![Int(24_i64).factors(), vec![1, 2, 3, 4, 6, 8, 12, 24]];
assert_eq![Int(-24_i64).factors(), vec![1, 2, 3, 4, 6, 8, 12, 24]];
assert![Int(0_i64).factors().is_empty()];
assert_eq![Int(1_i64).factors(), vec![1]];
assert_eq![Int(7_i64).factors(), vec![1, 7]];
Sourcepub fn factors_proper(self) -> Vec<i64> ⓘ
Available on crate feature alloc
only.
pub fn factors_proper(self) -> Vec<i64> ⓘ
alloc
only.Returns the proper factors.
§Examples
assert_eq![Int(24_i64).factors_proper(), vec![2, 3, 4, 6, 8, 12]];
assert_eq![Int(-24_i64).factors_proper(), vec![2, 3, 4, 6, 8, 12]];
assert![Int(0_i64).factors_proper().is_empty()];
assert![Int(1_i64).factors_proper().is_empty()];
assert![Int(7_i64).factors_proper().is_empty()];
Sourcepub fn factors_prime(self) -> Vec<i64> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime(self) -> Vec<i64> ⓘ
alloc
only.Returns the prime factors.
§Examples
assert_eq![Int(24_i64).factors_prime(), vec![2, 2, 2, 3]];
assert_eq![Int(-24_i64).factors_prime(), vec![2, 2, 2, 3]];
assert![Int(0_i64).factors_prime().is_empty()];
assert![Int(1_i64).factors_prime().is_empty()];
assert_eq![Int(7_i64).factors_prime(), vec![7]];
Sourcepub fn factors_prime_unique(self) -> Vec<i64> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique(self) -> Vec<i64> ⓘ
alloc
only.Returns the unique prime factors.
§Examples
assert_eq![Int(24_i64).factors_prime_unique(), vec![2, 3]];
assert_eq![Int(-24_i64).factors_prime_unique(), vec![2, 3]];
Sourcepub fn factors_prime_unique_exp(self) -> Vec<(i64, u32)> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique_exp(self) -> Vec<(i64, u32)> ⓘ
alloc
only.Returns the unique prime factors with its exponent.
§Examples
assert_eq![Int(24_i64).factors_prime_unique_exp(), vec![(2, 3), (3, 1)]];
assert_eq![Int(-24_i64).factors_prime_unique_exp(), vec![(2, 3), (3, 1)]];
assert![Int(0_i64).factors_prime_unique_exp().is_empty()];
assert![Int(1_i64).factors_prime_unique_exp().is_empty()];
assert_eq![Int(7_i64).factors_prime_unique_exp(), vec![(7, 1)]];
Sourcepub fn factors_prime_count(self) -> usize
pub fn factors_prime_count(self) -> usize
Returns the count of prime factors.
§Examples
assert_eq![Int(24_i64).factors_prime_count(), 4];
assert_eq![Int(-24_i64).factors_prime_count(), 4];
assert_eq![Int(0_i64).factors_prime_count(), 0];
assert_eq![Int(1_i64).factors_prime_count(), 0];
assert_eq![Int(7_i64).factors_prime_count(), 1];
Sourcepub fn factors_prime_unique_count(self) -> usize
pub fn factors_prime_unique_count(self) -> usize
Returns the count of unique prime factors.
§Examples
assert_eq![Int(24_i64).factors_prime_unique_count(), 2];
assert_eq![Int(-24_i64).factors_prime_unique_count(), 2];
Sourcepub fn factors_buf(
self,
fbuf: &mut [i64],
upfbuf: &mut [i64],
) -> Result<(usize, usize)>
pub fn factors_buf( self, fbuf: &mut [i64], upfbuf: &mut [i64], ) -> Result<(usize, usize)>
Writes the factors in fbuf
, and the unique prime factors in upfbuf
.
Returns a tuple with the number of factors and unique prime factors found.
§Errors
Returns MismatchedSizes
if the total number of factors is greater
than the length of any buffer.
§Examples
let (mut fbuf, mut upbuf) = ([0; 20], [0; 20]);
assert_eq![Int(24_i64).factors_buf(&mut fbuf, &mut upbuf), Ok((8, 2))];
assert_eq![fbuf[..8], [1, 2, 3, 4, 6, 8, 12, 24]];
assert_eq![upbuf[..2], [2, 3]];
Sourcepub fn factors_proper_buf(
self,
fbuf: &mut [i64],
upfbuf: &mut [i64],
) -> Result<(usize, usize)>
pub fn factors_proper_buf( self, fbuf: &mut [i64], upfbuf: &mut [i64], ) -> Result<(usize, usize)>
Writes the proper factors in fbuf
, and the unique prime factors in upfbuf
.
Returns a tuple with the number of factors and unique prime factors found.
§Errors
Returns MismatchedSizes
if the total number of factors is greater
than the length of any buffer.
§Examples
let (mut fbuf, mut upbuf) = ([0; 20], [0; 20]);
assert_eq![Int(24_i64).factors_proper_buf(&mut fbuf, &mut upbuf), Ok((6, 2))];
assert_eq![fbuf[..6], [2, 3, 4, 6, 8, 12,]];
assert_eq![upbuf[..2], [2, 3]];
Sourcepub fn factors_prime_buf(self, buffer: &mut [i64]) -> Result<usize>
pub fn factors_prime_buf(self, buffer: &mut [i64]) -> Result<usize>
Writes the prime factors in the given buffer
.
Returns the number of factors found
§Errors
Returns MismatchedSizes
if the total number of factors
is greater than the length of the buffer
.
§Examples
let mut buf = [0; 5];
assert_eq![Int(24_i64).factors_prime_buf(&mut buf), Ok(4)];
assert_eq![buf[..4], [2, 2, 2, 3]];
assert![Int(24_i64 * 4).factors_prime_buf(&mut buf).is_err()];
assert_eq![buf, [2, 2, 2, 2, 2]]; // the factor of 3 didn't fit
assert_eq![Int(0_i64).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(1_i64).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(7_i64).factors_prime_buf(&mut buf), Ok(1)];
assert_eq![buf[..1], [7]];
Sourcepub fn factors_prime_unique_buf(self, buffer: &mut [i64]) -> Result<usize>
pub fn factors_prime_unique_buf(self, buffer: &mut [i64]) -> Result<usize>
Writes the prime factors in the given buffer
.
The buffer
must be large enough to hold all the non-unique factors of n
.
In that case the function will return the number of unique factors found.
§Errors
Returns MismatchedSizes
otherwise. In that case the buffer
will only contain the non-unique factors that could fit, same as
factors_prime_buf
.
§Examples
let mut uniq = [0; 5];
assert_eq![Int(24_i64).factors_prime_unique_buf(&mut uniq), Ok(2)];
assert_eq![uniq, [2, 3, 2, 3, 0]];
Sourcepub fn factors_prime_unique_exp_buf(
self,
fbuffer: &mut [i64],
ebuffer: &mut [u32],
) -> Result<usize>
pub fn factors_prime_unique_exp_buf( self, fbuffer: &mut [i64], ebuffer: &mut [u32], ) -> Result<usize>
Writes the unique prime factors in the given fbuffer
, and the
associated exponent in the given ebuffer
at the same index.
The fbuffer
must be large enough to hold all the non-unique factors of n
.
In that case the function will return the number of unique factors found.
§Errors
Returns MismatchedSizes
otherwise. In that case the buffer
will only contain the non-unique factors that could fit, same as
factors_prime_buf
.
Returns MismatchedSizes
if ebuffer
is not large enough as well.
In that case the number of unique factors will equal ebuffer.len()
.
§Examples
let mut fbuf = [0; 4];
let mut ebuf = [0; 2];
assert_eq![Int(40_i64).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(2)];
assert_eq![fbuf[..2], [2, 5]]; // 2^3, 5^1, …
assert_eq![ebuf[..2], [3, 1]];
assert_eq![Int(0_i64).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(1_i64).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(7_i64).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(1)];
assert_eq![fbuf[..1], [7]]; // 7^1
assert_eq![ebuf[..1], [1]];
// When `fbuffer` is not large enough:
let mut fbuf = [0; 3];
let mut ebuf = [0; 2];
assert![Int(24_i64).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf).is_err()];
assert_eq![fbuf, [2, 2, 2]]; // the factor of 5 didn't fit
assert_eq![ebuf, [0, 0]]; // the exponents didn't get written
// When `ebuffer` is not large enough:
let mut fbuf = [0; 4];
let mut ebuf = [0; 1];
assert![Int(24_i64).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf).is_err()];
assert_eq![fbuf[..ebuf.len()], [2]]; // 2^3, Err, …
assert_eq![ebuf[..], [3]];
Sourcepub fn factors_prime_unique_plus_buf(
self,
pfbuf: &mut [i64],
upfbuf: &mut [i64],
) -> Result<(usize, usize)>
pub fn factors_prime_unique_plus_buf( self, pfbuf: &mut [i64], upfbuf: &mut [i64], ) -> Result<(usize, usize)>
Writes the prime factors in pfbuf
, and the unique prime factors in upfbuf
.
Returns the number of factors found.
§Errors
Returns MismatchedSizes
if the total number of factors
is greater than the length of the buffer
.
§Examples
let (mut fac, mut uniq) = ([0; 5], [0; 5]);
assert_eq![Int(24_i64).factors_prime_unique_plus_buf(&mut fac, &mut uniq), Ok((4, 2))];
assert_eq![fac, [2, 2, 2, 3, 0]];
assert_eq![uniq, [2, 3, 0, 0, 0]];
Source§impl Int<u32>
Available on crate feature _int_u32
only.
impl Int<u32>
_int_u32
only.§Integer factors related methods for u32
- Allocating:
- Not allocating:
Sourcepub fn factors(self) -> Vec<u32> ⓘ
Available on crate feature alloc
only.
pub fn factors(self) -> Vec<u32> ⓘ
alloc
only.Returns the factors (including 1 and self).
§Examples
assert_eq![Int(24_u32).factors(), vec![1, 2, 3, 4, 6, 8, 12, 24]];
assert![Int(0_u32).factors().is_empty()];
assert_eq![Int(1_u32).factors(), vec![1]];
assert_eq![Int(7_u32).factors(), vec![1, 7]];
Sourcepub fn factors_proper(self) -> Vec<u32> ⓘ
Available on crate feature alloc
only.
pub fn factors_proper(self) -> Vec<u32> ⓘ
alloc
only.Returns the proper factors.
§Examples
assert_eq![Int(24_u32).factors_proper(), vec![2, 3, 4, 6, 8, 12]];
assert![Int(0_u32).factors_proper().is_empty()];
assert![Int(1_u32).factors_proper().is_empty()];
assert![Int(7_u32).factors_proper().is_empty()];
Sourcepub fn factors_prime(self) -> Vec<u32> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime(self) -> Vec<u32> ⓘ
alloc
only.Returns the prime factors.
§Examples
assert_eq![Int(24_u32).factors_prime(), vec![2, 2, 2, 3]];
assert![Int(0_u32).factors_prime().is_empty()];
assert![Int(1_u32).factors_prime().is_empty()];
assert_eq![Int(7_u32).factors_prime(), vec![7]];
Sourcepub fn factors_prime_unique(self) -> Vec<u32> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique(self) -> Vec<u32> ⓘ
alloc
only.Returns the unique prime factors.
§Examples
assert_eq![Int(24_u32).factors_prime_unique(), vec![2, 3]];
Sourcepub fn factors_prime_unique_exp(self) -> Vec<(u32, u32)> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique_exp(self) -> Vec<(u32, u32)> ⓘ
alloc
only.Returns the unique prime factors with its exponent.
§Examples
assert_eq![Int(24_u32).factors_prime_unique_exp(), vec![(2, 3), (3, 1)]];
assert![Int(0_u32).factors_prime_unique_exp().is_empty()];
assert![Int(1_u32).factors_prime_unique_exp().is_empty()];
assert_eq![Int(7_u32).factors_prime_unique_exp(), vec![(7, 1)]];
Sourcepub fn factors_prime_count(self) -> usize
pub fn factors_prime_count(self) -> usize
Returns the count of prime factors.
§Examples
assert_eq![Int(24_u32).factors_prime_count(), 4];
assert_eq![Int(0_u32).factors_prime_count(), 0];
assert_eq![Int(1_u32).factors_prime_count(), 0];
assert_eq![Int(7_u32).factors_prime_count(), 1];
Sourcepub fn factors_prime_unique_count(self) -> usize
pub fn factors_prime_unique_count(self) -> usize
Returns the count of unique prime factors.
§Examples
assert_eq![Int(24_u32).factors_prime_unique_count(), 2];
Sourcepub fn factors_buf(
self,
fbuf: &mut [u32],
upfbuf: &mut [u32],
) -> Result<(usize, usize)>
pub fn factors_buf( self, fbuf: &mut [u32], upfbuf: &mut [u32], ) -> Result<(usize, usize)>
Writes the factors in fbuf
, and the unique prime factors in upfbuf
.
Returns a tuple with the number of factors and unique prime factors found.
§Errors
Returns MismatchedSizes
if the total number of factors
is greater than the length of any buffer.
§Examples
let (mut fbuf, mut upbuf) = ([0; 20], [0; 20]);
assert_eq![Int(24_u32).factors_buf(&mut fbuf, &mut upbuf), Ok((8, 2))];
assert_eq![fbuf[..8], [1, 2, 3, 4, 6, 8, 12, 24]];
assert_eq![upbuf[..2], [2, 3]];
Sourcepub fn factors_proper_buf(
self,
fbuf: &mut [u32],
upfbuf: &mut [u32],
) -> Result<(usize, usize)>
pub fn factors_proper_buf( self, fbuf: &mut [u32], upfbuf: &mut [u32], ) -> Result<(usize, usize)>
Writes the proper factors in fbuf
, and the unique prime factors in upfbuf
.
Returns a tuple with the number of factors and unique prime factors found.
§Errors
Returns MismatchedSizes
if the total number of factors
is greater than the length of any buffer.
§Examples
let (mut fbuf, mut upbuf) = ([0; 20], [0; 20]);
assert_eq![Int(24_u32).factors_proper_buf(&mut fbuf, &mut upbuf), Ok((6, 2))];
assert_eq![fbuf[..6], [2, 3, 4, 6, 8, 12,]];
assert_eq![upbuf[..2], [2, 3]];
Sourcepub fn factors_prime_buf(self, buffer: &mut [u32]) -> Result<usize>
pub fn factors_prime_buf(self, buffer: &mut [u32]) -> Result<usize>
Writes the prime factors in the given buffer
.
Returns the number of factors found.
§Errors
Returns MismatchedSizes
if the total number of factors
is greater than the length of the buffer
.
§Examples
let mut buf = [0; 5];
assert_eq![Int(24_u32).factors_prime_buf(&mut buf), Ok(4)];
assert_eq![buf[..4], [2, 2, 2, 3]];
assert![Int(24_u32 * 4).factors_prime_buf(&mut buf).is_err()];
assert_eq![buf, [2, 2, 2, 2, 2]]; // the factor of 3 didn't fit
assert_eq![Int(0_u32).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(1_u32).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(7_u32).factors_prime_buf(&mut buf), Ok(1)];
assert_eq![buf[..1], [7]];
Sourcepub fn factors_prime_unique_buf(self, buffer: &mut [u32]) -> Result<usize>
pub fn factors_prime_unique_buf(self, buffer: &mut [u32]) -> Result<usize>
Writes the prime factors in the given buffer
.
The buffer
must be large enough to hold all the non-unique factors of n
.
In that case the function will return the number of unique factors found.
§Errors
Returns MismatchedSizes
otherwise. In that case the buffer
will only contain the non-unique factors that could fit, same as
factors_prime_buf
.
§Examples
let mut uniq = [0; 5];
assert_eq![Int(24_u32).factors_prime_unique_buf(&mut uniq), Ok(2)];
assert_eq![uniq, [2, 3, 2, 3, 0]];
Sourcepub fn factors_prime_unique_exp_buf(
self,
fbuffer: &mut [u32],
ebuffer: &mut [u32],
) -> Result<usize>
pub fn factors_prime_unique_exp_buf( self, fbuffer: &mut [u32], ebuffer: &mut [u32], ) -> Result<usize>
Writes the unique prime factors in the given fbuffer
, and the
associated exponent in the given ebuffer
at the same index.
The fbuffer
must be large enough to hold all the non-unique factors of n
.
In that case the function will return the number of unique factors found.
§Errors
Returns MismatchedSizes
otherwise. In that case the buffer
will only contain the non-unique factors that could fit, same as
factors_prime_buf
.
Returns MismatchedSizes
if ebuffer
is not large enough as well.
In that case the number of unique factors will equal ebuffer.len()
.
§Examples
let mut fbuf = [0; 4];
let mut ebuf = [0; 2];
assert_eq![Int(40_u32).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(2)];
assert_eq![fbuf[..2], [2, 5]]; // 2^3, 5^1, …
assert_eq![ebuf[..2], [3, 1]];
assert_eq![Int(0_u32).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(1_u32).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(7_u32).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(1)];
assert_eq![fbuf[..1], [7]]; // 7^1
assert_eq![ebuf[..1], [1]];
// When `fbuffer` is not large enough:
let mut fbuf = [0; 3];
let mut ebuf = [0; 2];
assert![Int(24_u32).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf).is_err()];
assert_eq![fbuf, [2, 2, 2]]; // the factor of 5 didn't fit
assert_eq![ebuf, [0, 0]]; // the exponents didn't get written
// When `ebuffer` is not large enough:
let mut fbuf = [0; 4];
let mut ebuf = [0; 1];
assert![Int(24_u32).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf).is_err()];
assert_eq![fbuf[..ebuf.len()], [2]]; // 2^3, Err, …
assert_eq![ebuf[..], [3]];
Sourcepub fn factors_prime_unique_plus_buf(
self,
pfbuf: &mut [u32],
upfbuf: &mut [u32],
) -> Result<(usize, usize)>
pub fn factors_prime_unique_plus_buf( self, pfbuf: &mut [u32], upfbuf: &mut [u32], ) -> Result<(usize, usize)>
Writes the prime factors in pfbuf
, and the unique factors in upfbuf
.
Returns the number of factors found.
§Errors
Returns MismatchedSizes
if the total number of factors
is greater than the length of the buffer
.
§Examples
let (mut fac, mut uniq) = ([0; 5], [0; 5]);
assert_eq![Int(24_u32).factors_prime_unique_plus_buf(&mut fac, &mut uniq), Ok((4, 2))];
assert_eq![fac, [2, 2, 2, 3, 0]];
assert_eq![uniq, [2, 3, 0, 0, 0]];
Source§impl Int<i64>
Available on crate feature _int_i64
only.
impl Int<i64>
_int_i64
only.§Integer modulo related methods for i64
- modulo (uc)
- modulo_cycles (uc)
- modulo_add (uc)
- modulo_add_cycles (uc)
- modulo_add_inv (uc)
- modulo_sub (uc)
- modulo_sub_cycles (uc)
- modulo_mul (uc)
- modulo_mul_cycles (uc)
- modulo_mul_inv (uc)
- modulo_div (uc)
Sourcepub const fn modulo(self, modulus: i64) -> Result<Int<i64>>
pub const fn modulo(self, modulus: i64) -> Result<Int<i64>>
Computes the non-negative modulo of self
over |modulus
|.
The result is non-negative and less than the absolute value of modulus
,
i.e., in the range $ [0, |\text{modulus}|) $.
Performs operations internally as i128
.
§Errors
Returns NonZeroRequired
if modulus == 0
, and for i128
it could also return Overflow
.
§Examples
let m = 3;
assert_eq![Int(-4_i64).modulo(m)?, 2];
assert_eq![Int(-3_i64).modulo(m)?, 0];
assert_eq![Int(-2_i64).modulo(m)?, 1];
assert_eq![Int(-1_i64).modulo(m)?, 2];
assert_eq![Int( 0_i64).modulo(m)?, 0];
assert_eq![Int( 1_i64).modulo(m)?, 1];
assert_eq![Int( 2_i64).modulo(m)?, 2];
assert_eq![Int( 3_i64).modulo(m)?, 0];
assert_eq![Int( 4_i64).modulo(m)?, 1];
assert_eq![Int(i64::MAX).modulo(i64::MIN)?, i64::MAX];
assert_eq![Int(i64::MIN).modulo(i64::MAX)?, i64::MAX - 1];
assert_eq![Int(i64::MIN).modulo(i64::MIN)?, 0];
assert![Int(i64::MIN).modulo(-1).is_ok()];
assert_eq![Int(1_i64).modulo(0), Err(NumError::NonZeroRequired)];
assert_eq![Int(i128::MIN).modulo(-1), Err(NumError::Overflow(None))];
Sourcepub const fn modulo_cycles(
self,
modulus: i64,
) -> Result<ValueQuant<Int<i64>, Int<i64>>>
pub const fn modulo_cycles( self, modulus: i64, ) -> Result<ValueQuant<Int<i64>, Int<i64>>>
Computes the non-negative modulo of self
over |modulus
|,
and the number of cycles the result is reduced.
Performs operations internally as i128
.
§Errors
Returns NonZeroRequired
if modulus == 0
, and for i128
if self == MIN && modulus == ±1
it can return Overflow
.
§Examples
let m = 3;
assert_eq![Int(-3_i64).modulo_cycles(m)?, (0, 1)];
assert_eq![Int(-2_i64).modulo_cycles(m)?, (1, 0)];
assert_eq![Int(-1_i64).modulo_cycles(m)?, (2, 0)];
assert_eq![Int( 0_i64).modulo_cycles(m)?, (0, 0)];
assert_eq![Int( 1_i64).modulo_cycles(m)?, (1, 0)];
assert_eq![Int( 2_i64).modulo_cycles(m)?, (2, 0)];
assert_eq![Int( 3_i64).modulo_cycles(m)?, (0, 1)];
Sourcepub const fn modulo_cycles_unchecked(
self,
modulus: i64,
) -> ValueQuant<Int<i64>, Int<i64>>
pub const fn modulo_cycles_unchecked( self, modulus: i64, ) -> ValueQuant<Int<i64>, Int<i64>>
Sourcepub const fn modulo_add(self, other: i64, modulus: i64) -> Result<Int<i64>>
pub const fn modulo_add(self, other: i64, modulus: i64) -> Result<Int<i64>>
Computes the non-negative modulo of self + other
over |modulus
|.
Performs operations internally as i128
.
§Features
Uses unsafe_hint
for performance optimizations with upcasted arithmetic.
§Errors
Returns NonZeroRequired
if modulus == 0
, and for i128
it could also return Overflow
.
§Examples
let m = 3;
assert_eq![Int(4_i64).modulo_add(-4, m)?, 0];
assert_eq![Int(4_i64).modulo_add(-3, m)?, 1];
assert_eq![Int(4_i64).modulo_add(-2, m)?, 2];
assert_eq![Int(4_i64).modulo_add(-1, m)?, 0];
assert_eq![Int(4_i64).modulo_add( 0, m)?, 1];
assert_eq![Int(4_i64).modulo_add( 1, m)?, 2];
assert_eq![Int(4_i64).modulo_add( 2, m)?, 0];
assert_eq![Int(4_i64).modulo_add( 3, m)?, 1];
assert_eq![Int(4_i64).modulo_add( 4, m)?, 2];
Sourcepub const fn modulo_add_cycles(
self,
other: i64,
modulus: i64,
) -> Result<ValueQuant<Int<i64>, Int<i64>>>
pub const fn modulo_add_cycles( self, other: i64, modulus: i64, ) -> Result<ValueQuant<Int<i64>, Int<i64>>>
Computes the non-negative modulo of self + other
over |modulus
|,
and the number of cycles the result is reduced.
Performs operations internally as i128
.
§Features
Uses unsafe_hint
for performance optimizations with upcasted arithmetic.
§Errors
Returns NonZeroRequired
if modulus == 0
, and for i128
it can also return Overflow
, more probably than in
modulo_add
since we can’t reduce
the operands beforehand in order to calculate times.
§Examples
let m = 3;
assert_eq![Int(4_i64).modulo_add_cycles(-4, m)?, (0, 0)];
assert_eq![Int(4_i64).modulo_add_cycles(-3, m)?, (1, 0)];
assert_eq![Int(4_i64).modulo_add_cycles(-2, m)?, (2, 0)];
assert_eq![Int(4_i64).modulo_add_cycles(-1, m)?, (0, 1)];
assert_eq![Int(4_i64).modulo_add_cycles( 0, m)?, (1, 1)];
assert_eq![Int(4_i64).modulo_add_cycles( 1, m)?, (2, 1)];
assert_eq![Int(4_i64).modulo_add_cycles( 2, m)?, (0, 2)];
assert_eq![Int(4_i64).modulo_add_cycles( 3, m)?, (1, 2)];
assert_eq![Int(4_i64).modulo_add_cycles( 4, m)?, (2, 2)];
Sourcepub const fn modulo_add_cycles_unchecked(
self,
other: i64,
modulus: i64,
) -> ValueQuant<Int<i64>, Int<i64>>
pub const fn modulo_add_cycles_unchecked( self, other: i64, modulus: i64, ) -> ValueQuant<Int<i64>, Int<i64>>
Computes the non-negative modulo of self + other
over |modulus
|,
and the number of cycles the result is reduced,
unchecked version.
Performs operations internally as i128
.
§Features
Uses unsafe_hint
for performance optimizations with upcasted arithmetic.
§Panics
Panics if modulus == 0
, and for i128
it can also panic on overflow,
more probably than in modulo_add_unchecked
since we can’t reduce the operands beforehand in order to calculate times.
Sourcepub const fn modulo_add_inv(self, modulus: i64) -> Result<Int<i64>>
pub const fn modulo_add_inv(self, modulus: i64) -> Result<Int<i64>>
Calculates the modular additive inverse.
The modular additive inverse of self modulo modulus is an integer b such that $ a+b \equiv 0 (\mod m) $.
The modular multiplicative inverse always exists and is simply
modulus - self
if self != 0
, or 0 otherwise.
§Errors
Returns NonZeroRequired
if modulus == 0
.
§Examples
let m = 3;
assert_eq![Int(-4_i64).modulo_add_inv(m)?, 1];
assert_eq![Int(-3_i64).modulo_add_inv(m)?, 0];
assert_eq![Int(-2_i64).modulo_add_inv(m)?, 2];
assert_eq![Int(-1_i64).modulo_add_inv(m)?, 1];
assert_eq![Int( 0_i64).modulo_add_inv(m)?, 0];
assert_eq![Int( 1_i64).modulo_add_inv(m)?, 2];
assert_eq![Int( 2_i64).modulo_add_inv(m)?, 1];
assert_eq![Int( 3_i64).modulo_add_inv(m)?, 0];
Sourcepub const fn modulo_add_inv_unchecked(self, modulus: i64) -> Int<i64>
pub const fn modulo_add_inv_unchecked(self, modulus: i64) -> Int<i64>
Calculates the modular additive inverse, unchecked version.
The modular additive inverse of self modulo modulus is an integer b such that $ a+b \equiv 0 (\mod m) $.
The modular multiplicative inverse always exists and is simply
modulus - self
if self != 0
, or 0 otherwise.
§Panics
Panics if modulus == 0
.
Sourcepub const fn modulo_sub(self, other: i64, modulus: i64) -> Result<Int<i64>>
pub const fn modulo_sub(self, other: i64, modulus: i64) -> Result<Int<i64>>
Computes the modulo of self - other
over |modulus
|.
Performs operations internally as i128
.
§Features
Uses unsafe_hint
for performance optimizations with upcasted arithmetic.
§Errors
Returns NonZeroRequired
if modulus == 0
.
§Examples
let m = 3;
assert_eq![Int(4_i64).modulo_sub(-4, m)?, 2];
assert_eq![Int(4_i64).modulo_sub(-3, m)?, 1];
assert_eq![Int(4_i64).modulo_sub(-2, m)?, 0];
assert_eq![Int(4_i64).modulo_sub(-1, m)?, 2];
assert_eq![Int(4_i64).modulo_sub( 0, m)?, 1];
assert_eq![Int(4_i64).modulo_sub( 1, m)?, 0];
assert_eq![Int(4_i64).modulo_sub( 2, m)?, 2];
assert_eq![Int(4_i64).modulo_sub( 3, m)?, 1];
assert_eq![Int(4_i64).modulo_sub( 4, m)?, 0];
Sourcepub const fn modulo_sub_cycles(
self,
other: i64,
modulus: i64,
) -> Result<ValueQuant<Int<i64>, Int<i64>>>
pub const fn modulo_sub_cycles( self, other: i64, modulus: i64, ) -> Result<ValueQuant<Int<i64>, Int<i64>>>
Computes the non-negative modulo of self - other
over |modulus
|,
and the number of cycles the result is reduced.
Performs operations internally as i128
.
§Features
Uses unsafe_hint
for performance optimizations with upcasted arithmetic.
§Errors
Returns NonZeroRequired
if modulus == 0
, and for i128
it can also return Overflow
(unlike modulo_sub
)
since we can’t reduce the operands beforehand in order to calculate times.
§Examples
let m = 3;
assert_eq![Int(4_i64).modulo_sub_cycles(-4, m)?, (2, 2)];
assert_eq![Int(4_i64).modulo_sub_cycles(-3, m)?, (1, 2)];
assert_eq![Int(4_i64).modulo_sub_cycles(-2, m)?, (0, 2)];
assert_eq![Int(4_i64).modulo_sub_cycles(-1, m)?, (2, 1)];
assert_eq![Int(4_i64).modulo_sub_cycles( 0, m)?, (1, 1)];
assert_eq![Int(4_i64).modulo_sub_cycles( 1, m)?, (0, 1)];
assert_eq![Int(4_i64).modulo_sub_cycles( 2, m)?, (2, 0)];
assert_eq![Int(4_i64).modulo_sub_cycles( 3, m)?, (1, 0)];
assert_eq![Int(4_i64).modulo_sub_cycles( 4, m)?, (0, 0)];
Sourcepub const fn modulo_sub_cycles_unchecked(
self,
other: i64,
modulus: i64,
) -> (Int<i64>, Int<i64>) ⓘ
pub const fn modulo_sub_cycles_unchecked( self, other: i64, modulus: i64, ) -> (Int<i64>, Int<i64>) ⓘ
Computes the non-negative modulo of self - other
over |modulus
|,
and the number of cycles the result is reduced,
unchecked version.
Performs operations internally as i128
.
§Panics
Panics if modulus == 0
, and for i128
it can also panic on overflow,
more probably than in modulo_sub_unchecked
since we can’t reduce the operands beforehand in order to calculate times.
Sourcepub const fn modulo_mul(self, other: i64, modulus: i64) -> Result<Int<i64>>
pub const fn modulo_mul(self, other: i64, modulus: i64) -> Result<Int<i64>>
Computes the non-negative modulo of self + other
over |modulus
|.
Performs operations internally as i128
.
§Features
Uses unsafe_hint
for performance optimizations with upcasted arithmetic.
§Errors
Returns NonZeroRequired
if modulus == 0
, and for i128
it could also return Overflow
.
§Examples
let m = 3;
assert_eq![Int(4_i64).modulo_mul(-4, m)?, 2];
assert_eq![Int(4_i64).modulo_mul(-3, m)?, 0];
assert_eq![Int(4_i64).modulo_mul(-2, m)?, 1];
assert_eq![Int(4_i64).modulo_mul(-1, m)?, 2];
assert_eq![Int(4_i64).modulo_mul( 0, m)?, 0];
assert_eq![Int(4_i64).modulo_mul( 1, m)?, 1];
assert_eq![Int(4_i64).modulo_mul( 2, m)?, 2];
assert_eq![Int(4_i64).modulo_mul( 3, m)?, 0];
assert_eq![Int(4_i64).modulo_mul( 4, m)?, 1];
Sourcepub const fn modulo_mul_cycles(
self,
other: i64,
modulus: i64,
) -> Result<ValueQuant<Int<i64>, Int<i64>>>
pub const fn modulo_mul_cycles( self, other: i64, modulus: i64, ) -> Result<ValueQuant<Int<i64>, Int<i64>>>
Computes the non-negative modulo of self + other
over |modulus
|,
and the number of cycles the result is reduced.
Performs operations internally as i128
.
§Features
Uses unsafe_hint
for performance optimizations with upcasted arithmetic.
§Errors
Returns NonZeroRequired
if modulus == 0
, and for i128
it can also return Overflow
, more probably than in
modulo_mul
since we can’t reduce
the operands beforehand in order to calculate times.
§Examples
let m = 3;
assert_eq![Int(4_i64).modulo_mul_cycles(-4, m)?, (2, 5)];
assert_eq![Int(4_i64).modulo_mul_cycles(-3, m)?, (0, 4)];
assert_eq![Int(4_i64).modulo_mul_cycles(-2, m)?, (1, 2)];
assert_eq![Int(4_i64).modulo_mul_cycles(-1, m)?, (2, 1)];
assert_eq![Int(4_i64).modulo_mul_cycles( 0, m)?, (0, 0)];
assert_eq![Int(4_i64).modulo_mul_cycles( 1, m)?, (1, 1)];
assert_eq![Int(4_i64).modulo_mul_cycles( 2, m)?, (2, 2)];
assert_eq![Int(4_i64).modulo_mul_cycles( 3, m)?, (0, 4)];
assert_eq![Int(4_i64).modulo_mul_cycles( 4, m)?, (1, 5)];
Sourcepub const fn modulo_mul_cycles_unchecked(
self,
other: i64,
modulus: i64,
) -> ValueQuant<Int<i64>, Int<i64>>
pub const fn modulo_mul_cycles_unchecked( self, other: i64, modulus: i64, ) -> ValueQuant<Int<i64>, Int<i64>>
Computes the non-negative modulo of self + other
over |modulus
|,
and the number of cycles the result is reduced,
unchecked version.
Performs operations internally as i128
.
§Panics
Panics if modulus == 0
, and for i128
it can also panic on overflow,
more probably than in modulo_mul_unchecked
since we can’t reduce the operands beforehand in order to calculate times.
Sourcepub const fn modulo_mul_inv(self, modulus: i64) -> Result<Int<i64>>
pub const fn modulo_mul_inv(self, modulus: i64) -> Result<Int<i64>>
Calculates the modular multiplicative inverse.
The modular multiplicative inverse of self modulo modulus is an integer b such that $ ab \equiv 1 (\mod m) $.
The modular multiplicative inverse exists only if self
and
modulus
are coprime, meaning their greatest common divisor is 1.
§Errors
Returns NonZeroRequired
if modulus == 0
,
or NoInverse
if there’s no inverse.
§Examples
let m = 5;
assert_eq![Int(-4_i64).modulo_mul_inv(m)?, 4];
assert_eq![Int(-3_i64).modulo_mul_inv(m)?, 2];
assert_eq![Int(-2_i64).modulo_mul_inv(m)?, 3];
assert_eq![Int(-1_i64).modulo_mul_inv(m)?, 1];
assert_eq![Int( 0_i64).modulo_mul_inv(m), Err(NumError::NoInverse)];
assert_eq![Int( 1_i64).modulo_mul_inv(m)?, 1];
assert_eq![Int( 2_i64).modulo_mul_inv(m)?, 3];
assert_eq![Int( 3_i64).modulo_mul_inv(m)?, 2];
assert_eq![Int( 4_i64).modulo_mul_inv(m)?, 4];
Sourcepub const fn modulo_mul_inv_unchecked(self, modulus: i64) -> Int<i64>
pub const fn modulo_mul_inv_unchecked(self, modulus: i64) -> Int<i64>
Calculates the modular multiplicative inverse, unchecked version.
The modular multiplicative inverse of self modulo modulus is an integer b such that $ ab \equiv 1 (\mod m) $.
The modular multiplicative inverse exists only if self
and
modulus
are coprime, meaning their greatest common divisor is 1.
§Panics
Panics if modulus == 0
, and if there’s no inverse.
Sourcepub const fn modulo_div(self, other: i64, modulus: i64) -> Result<Int<i64>>
pub const fn modulo_div(self, other: i64, modulus: i64) -> Result<Int<i64>>
Computes self / other
over |modulus
|.
Performs operations internally as i128
.
$a / b \mod m$ is equivalent to $a * b^{-1} \mod m$, where $b^{-1}$ is the modular multiplicative inverse of $b$ modulo $m$.
§Errors
Returns NonZeroRequired
if modulus == 0
,
and NoInverse
if there’s no multiplicative inverse of other
.
§Examples
let m = 3;
assert_eq![Int(-4_i64).modulo_div(2, m)?, 1];
assert_eq![Int(-3_i64).modulo_div(2, m)?, 0];
assert_eq![Int(-2_i64).modulo_div(2, m)?, 2];
assert_eq![Int(-1_i64).modulo_div(2, m)?, 1];
assert_eq![Int( 0_i64).modulo_div(2, m)?, 0];
assert_eq![Int( 1_i64).modulo_div(2, m)?, 2];
assert_eq![Int( 2_i64).modulo_div(2, m)?, 1];
assert_eq![Int( 3_i64).modulo_div(2, m)?, 0];
assert_eq![Int( 4_i64).modulo_div(2, m)?, 2];
Source§impl Int<u32>
Available on crate feature _int_u32
only.
impl Int<u32>
_int_u32
only.§Integer modulo related methods for u32
- modulo (uc)
- modulo_cycles (uc)
- modulo_add (uc)
- modulo_add_cycles (uc)
- modulo_add_inv (uc)
- modulo_sub (uc)
- modulo_sub_cycles (uc)
- modulo_mul (uc)
- modulo_mul_cycles (uc)
- modulo_mul_inv (uc)
- modulo_div (uc)
Sourcepub const fn modulo(self, modulus: u32) -> Result<Int<u32>>
pub const fn modulo(self, modulus: u32) -> Result<Int<u32>>
Computes the non-negative modulo of self
over modulus
.
The result is less than the value of modulus
,
i.e., in the range $ [0, \text{modulus}) $.
§Errors
Returns NonZeroRequired
if modulus == 0
.
§Examples
let m = 3;
assert_eq![Int(0_u32).modulo(m)?, 0];
assert_eq![Int(1_u32).modulo(m)?, 1];
assert_eq![Int(2_u32).modulo(m)?, 2];
assert_eq![Int(3_u32).modulo(m)?, 0];
assert_eq![Int(4_u32).modulo(m)?, 1];
Sourcepub const fn modulo_unchecked(self, modulus: u32) -> Int<u32>
pub const fn modulo_unchecked(self, modulus: u32) -> Int<u32>
Computes the non-negative modulo of self
over modulus
, unchecked version.
The result is less than the value of modulus
,
i.e., in the range $ [0, \text{modulus}) $.
§Panics
Panics if modulus == 0
.
§Examples
let m = 3;
assert_eq![Int(0_u32).modulo_unchecked(m), 0];
assert_eq![Int(1_u32).modulo_unchecked(m), 1];
assert_eq![Int(2_u32).modulo_unchecked(m), 2];
assert_eq![Int(3_u32).modulo_unchecked(m), 0];
assert_eq![Int(4_u32).modulo_unchecked(m), 1];
Sourcepub const fn modulo_cycles(
self,
modulus: u32,
) -> Result<ValueQuant<Int<u32>, Int<u32>>>
pub const fn modulo_cycles( self, modulus: u32, ) -> Result<ValueQuant<Int<u32>, Int<u32>>>
Computes the non-negative modulo of self
over modulus
,
and the number of cycles it is reduced.
§Errors
Returns NonZeroRequired
if modulus == 0
.
§Examples
let m = 3;
assert_eq![Int(0_u32).modulo_cycles(m)?, (0, 0)];
assert_eq![Int(1_u32).modulo_cycles(m)?, (1, 0)];
assert_eq![Int(2_u32).modulo_cycles(m)?, (2, 0)];
assert_eq![Int(3_u32).modulo_cycles(m)?, (0, 1)];
Sourcepub const fn modulo_cycles_unchecked(
self,
modulus: u32,
) -> ValueQuant<Int<u32>, Int<u32>>
pub const fn modulo_cycles_unchecked( self, modulus: u32, ) -> ValueQuant<Int<u32>, Int<u32>>
Computes the non-negative modulo of self
over modulus
,
and the number of cycles it is reduced,
unchecked version.
§Panics
Panics if modulus == 0
.
§Examples
let m = 3;
assert_eq![Int(0_u32).modulo_cycles_unchecked(m), (0, 0)];
assert_eq![Int(1_u32).modulo_cycles_unchecked(m), (1, 0)];
assert_eq![Int(2_u32).modulo_cycles_unchecked(m), (2, 0)];
assert_eq![Int(3_u32).modulo_cycles_unchecked(m), (0, 1)];
Sourcepub const fn modulo_add(self, other: u32, modulus: u32) -> Result<Int<u32>>
pub const fn modulo_add(self, other: u32, modulus: u32) -> Result<Int<u32>>
Computes the modulo of self + other
over modulus
.
Performs operations internally as u64
.
§Features
Uses unsafe_hint
for performance optimizations with upcasted arithmetic.
§Errors
Returns NonZeroRequired
if modulus == 0
, and for u128
it could also return Overflow
.
§Examples
let m = 3;
assert_eq![Int(4_u32).modulo_add(0, m)?, 1];
assert_eq![Int(4_u32).modulo_add(1, m)?, 2];
assert_eq![Int(4_u32).modulo_add(2, m)?, 0];
assert_eq![Int(4_u32).modulo_add(3, m)?, 1];
assert_eq![Int(4_u32).modulo_add(4, m)?, 2];
Sourcepub const fn modulo_add_cycles(
self,
other: u32,
modulus: u32,
) -> Result<ValueQuant<Int<u32>, Int<u32>>>
pub const fn modulo_add_cycles( self, other: u32, modulus: u32, ) -> Result<ValueQuant<Int<u32>, Int<u32>>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced.
Performs operations internally as u64
.
§Features
Uses unsafe_hint
for performance optimizations with upcasted arithmetic.
§Errors
Returns NonZeroRequired
if modulus == 0
, and for u128
it can also return Overflow
, more probably than in
modulo_add
since we can’t reduce
the operands beforehand in order to calculate times.
§Examples
let m = 3;
assert_eq![Int(4_u32).modulo_add_cycles(0, m)?, (1, 1)];
assert_eq![Int(4_u32).modulo_add_cycles(1, m)?, (2, 1)];
assert_eq![Int(4_u32).modulo_add_cycles(2, m)?, (0, 2)];
assert_eq![Int(4_u32).modulo_add_cycles(3, m)?, (1, 2)];
assert_eq![Int(4_u32).modulo_add_cycles(4, m)?, (2, 2)];
Sourcepub const fn modulo_add_cycles_unchecked(
self,
other: u32,
modulus: u32,
) -> ValueQuant<Int<u32>, Int<u32>>
pub const fn modulo_add_cycles_unchecked( self, other: u32, modulus: u32, ) -> ValueQuant<Int<u32>, Int<u32>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced,
unchecked version.
Performs operations internally as u64
.
§Panics
Panics if modulus == 0
, and for u128
it can also panic on overflow,
more probably than in modulo_add_unchecked
since we can’t reduce the operands beforehand in order to calculate times.
Sourcepub const fn modulo_add_inv(self, modulus: u32) -> Result<Int<u32>>
pub const fn modulo_add_inv(self, modulus: u32) -> Result<Int<u32>>
Calculates the modular additive inverse.
The modular additive inverse of self modulo modulus is an integer b such that $ a+b \equiv 0 (\mod m) $.
The modular multiplicative inverse always exists and is simply
modulus - self
if self != 0
, or 0 otherwise.
§Errors
Returns NonZeroRequired
if modulus == 0
.
§Examples
let m = 3;
assert_eq![Int(0_u32).modulo_add_inv(m)?, 0];
assert_eq![Int(1_u32).modulo_add_inv(m)?, 2];
assert_eq![Int(2_u32).modulo_add_inv(m)?, 1];
assert_eq![Int(3_u32).modulo_add_inv(m)?, 0];
Sourcepub const fn modulo_add_inv_unchecked(self, modulus: u32) -> Int<u32>
pub const fn modulo_add_inv_unchecked(self, modulus: u32) -> Int<u32>
Calculates the modular additive inverse, unchecked version.
The modular additive inverse of self modulo modulus is an integer b such that $ a+b \equiv 0 (\mod m) $.
The modular multiplicative inverse always exists and is simply
modulus - self
if self != 0
, or 0 otherwise.
§Panics
Panics if modulus == 0
.
Sourcepub const fn modulo_sub(self, other: u32, modulus: u32) -> Result<Int<u32>>
pub const fn modulo_sub(self, other: u32, modulus: u32) -> Result<Int<u32>>
Computes the modulo of self - other
over modulus
.
§Errors
Returns NonZeroRequired
if modulus == 0
, and it can also
return Overflow
if the result would be a negative value.
§Examples
let m = 3;
assert_eq![Int(4_u32).modulo_sub(0, m)?, 1];
assert_eq![Int(4_u32).modulo_sub(1, m)?, 0];
assert_eq![Int(4_u32).modulo_sub(2, m)?, 2];
assert_eq![Int(4_u32).modulo_sub(3, m)?, 1];
assert_eq![Int(4_u32).modulo_sub(4, m)?, 0];
Sourcepub const fn modulo_sub_unchecked(self, other: u32, modulus: u32) -> Int<u32>
pub const fn modulo_sub_unchecked(self, other: u32, modulus: u32) -> Int<u32>
Computes the modulo of self - other
over modulus
,
unchecked version.
§Panics
Panics if modulus == 0
, and if the result would be a negative value.
Sourcepub const fn modulo_sub_cycles(
self,
other: u32,
modulus: u32,
) -> Result<ValueQuant<Int<u32>, Int<u32>>>
pub const fn modulo_sub_cycles( self, other: u32, modulus: u32, ) -> Result<ValueQuant<Int<u32>, Int<u32>>>
Computes the modulo of self - other
over modulus
,
and the number of cycles the result is reduced.
§Errors
Returns NonZeroRequired
if modulus == 0
, and it can also
return Overflow
if the result would be a negative value.
§Examples
let m = 3;
assert_eq![Int(4_u32).modulo_sub_cycles(0, m)?, (1, 1)];
assert_eq![Int(4_u32).modulo_sub_cycles(1, m)?, (0, 1)];
assert_eq![Int(4_u32).modulo_sub_cycles(2, m)?, (2, 0)];
assert_eq![Int(4_u32).modulo_sub_cycles(3, m)?, (1, 0)];
assert_eq![Int(4_u32).modulo_sub_cycles(4, m)?, (0, 0)];
Sourcepub const fn modulo_sub_cycles_unchecked(
self,
other: u32,
modulus: u32,
) -> ValueQuant<Int<u32>, Int<u32>>
pub const fn modulo_sub_cycles_unchecked( self, other: u32, modulus: u32, ) -> ValueQuant<Int<u32>, Int<u32>>
Computes the modulo of self - other
over modulus
,
and the number of cycles the result is reduced,
unchecked version.
§Panics
Panics if modulus == 0
, and if the result would be a negative value.
Sourcepub const fn modulo_mul(self, other: u32, modulus: u32) -> Result<Int<u32>>
pub const fn modulo_mul(self, other: u32, modulus: u32) -> Result<Int<u32>>
Computes the modulo of self + other
over modulus
.
Performs operations internally as u64
.
§Features
Uses unsafe_hint
for performance optimizations with upcasted arithmetic.
§Errors
Returns NonZeroRequired
if modulus == 0
, and for u128
it could also return Overflow
.
§Examples
let m = 3;
assert_eq![Int(4_u32).modulo_mul(0, m)?, 0];
assert_eq![Int(4_u32).modulo_mul(1, m)?, 1];
assert_eq![Int(4_u32).modulo_mul(2, m)?, 2];
assert_eq![Int(4_u32).modulo_mul(3, m)?, 0];
assert_eq![Int(4_u32).modulo_mul(4, m)?, 1];
Sourcepub const fn modulo_mul_cycles(
self,
other: u32,
modulus: u32,
) -> Result<ValueQuant<Int<u32>, Int<u32>>>
pub const fn modulo_mul_cycles( self, other: u32, modulus: u32, ) -> Result<ValueQuant<Int<u32>, Int<u32>>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced.
Performs operations internally as u64
.
§Features
Uses unsafe_hint
for performance optimizations with upcasted arithmetic.
§Errors
Returns NonZeroRequired
if modulus == 0
, and for u128
it can also return Overflow
, more probably than in
modulo_mul
since we can’t reduce
the operands beforehand in order to calculate times.
§Examples
let m = 3;
assert_eq![Int(4_u32).modulo_mul_cycles(0, m)?, (0, 0)];
assert_eq![Int(4_u32).modulo_mul_cycles(1, m)?, (1, 1)];
assert_eq![Int(4_u32).modulo_mul_cycles(2, m)?, (2, 2)];
assert_eq![Int(4_u32).modulo_mul_cycles(3, m)?, (0, 4)];
assert_eq![Int(4_u32).modulo_mul_cycles(4, m)?, (1, 5)];
Sourcepub const fn modulo_mul_cycles_unchecked(
self,
other: u32,
modulus: u32,
) -> ValueQuant<Int<u32>, Int<u32>>
pub const fn modulo_mul_cycles_unchecked( self, other: u32, modulus: u32, ) -> ValueQuant<Int<u32>, Int<u32>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced,
unchecked version.
Performs operations internally as u64
.
§Panics
Panics if modulus == 0
, and for u128
it can also panic on overflow,
more probably than in modulo_mul_unchecked
since we can’t reduce the operands beforehand in order to calculate times.
Sourcepub const fn modulo_mul_inv(self, modulus: u32) -> Result<Int<u32>>
Available on crate features _int_i64
and cast
only.
pub const fn modulo_mul_inv(self, modulus: u32) -> Result<Int<u32>>
_int_i64
and cast
only.Calculates the modular multiplicative inverse.
Performs operations internally as i64
.
The modular multiplicative inverse of a modulo m is an integer b such that $ ab \equiv 1 (\mod m) $.
The modular multiplicative inverse exists only if self
and
modulus
are coprime, meaning their greatest common divisor is 1.
§Errors
Returns NonZeroRequired
if modulus == 0
,
NoInverse
if there’s no inverse,
and for u128
it could return Overflow
when casting
in the gcd_ext
calculation.
§Examples
let m = 5;
assert_eq![Int(0_u32).modulo_mul_inv(m), Err(NumError::NoInverse)];
assert_eq![Int(1_u32).modulo_mul_inv(m)?, 1];
assert_eq![Int(2_u32).modulo_mul_inv(m)?, 3];
assert_eq![Int(3_u32).modulo_mul_inv(m)?, 2];
assert_eq![Int(4_u32).modulo_mul_inv(m)?, 4];
Sourcepub const fn modulo_mul_inv_unchecked(self, modulus: u32) -> Int<u32>
Available on crate features _int_i64
and cast
only.
pub const fn modulo_mul_inv_unchecked(self, modulus: u32) -> Int<u32>
_int_i64
and cast
only.Calculates the modular multiplicative inverse, unchecked version.
Performs operations internally as i64
.
The modular multiplicative inverse of a modulo m is an integer b such that $ ab \equiv 1 (\mod m) $.
The modular multiplicative inverse exists only if self
and
modulus
are coprime, meaning their greatest common divisor is 1.
§Panics
Panics if modulus == 0
, if there’s no inverse,
and for u128
it could overflow when casting
in the gcd_ext
calculation.
Sourcepub const fn modulo_div(self, other: u32, modulus: u32) -> Result<Int<u32>>
Available on crate features _int_i64
and cast
only.
pub const fn modulo_div(self, other: u32, modulus: u32) -> Result<Int<u32>>
_int_i64
and cast
only.Computes self / other
over modulus
.
Performs operations internally as i64
.
$a / b \mod m$ is equivalent to $a * b^{-1} \mod m$, where $b^{-1}$ is the modular multiplicative inverse of $b$ modulo $m$.
§Errors
Returns NonZeroRequired
if modulus == 0
,
NoInverse
if there’s no multiplicative inverse of other
,
and for u128
it could return Overflow
when casting
in the gcd_ext
calculation.
§Examples
let m = 3;
assert_eq![Int(0_u32).modulo_div(2, m)?, 0];
assert_eq![Int(1_u32).modulo_div(2, m)?, 2];
assert_eq![Int(2_u32).modulo_div(2, m)?, 1];
assert_eq![Int(3_u32).modulo_div(2, m)?, 0];
assert_eq![Int(4_u32).modulo_div(2, m)?, 2];
Source§impl Int<i64>
Available on crate feature _int_i64
only.
impl Int<i64>
_int_i64
only.Sourcepub const fn is_prime(self) -> bool
pub const fn is_prime(self) -> bool
Returns true
if n
is prime.
This approach uses optimized trial division, which means it checks only odd numbers starting from 3 and up to the square root of the given number. This is based on the fact that if a number is divisible by a number larger than its square root, the result of the division will be smaller than the square root, and it would have already been checked in previous iterations.
§Examples
assert![Int(127_i64).is_prime()];
assert![Int(2_i64).is_prime()];
assert![!Int(1_i64).is_prime()];
assert![!Int(-2_i64).is_prime()];
§Features
This will only be const if the _cmp_i64 feature is enabled.
Sourcepub const fn prime_nth(self) -> Result<Int<i64>>
pub const fn prime_nth(self) -> Result<Int<i64>>
Finds the 0-indexed nth
prime number.
Note: If nth
is negative, this function treats it as its absolute
value. For example, a value of -3
will be treated as 3
, and the
function will return the 3rd prime number.
§Errors
Returns Overflow
if the result can’t fit the type.
§Examples
assert_eq![Ok(Int(2)), Int(0_i64).prime_nth()];
assert_eq![Ok(Int(3)), Int(1_i64).prime_nth()];
assert_eq![Ok(Int(127)), Int(30_i64).prime_nth()];
assert_eq![Ok(Int(127)), Int(-30_i64).prime_nth()];
assert![Int(31_i8).prime_nth().is_err()];
§Features
This will only be const if the _cmp_i64 feature is enabled.
Sourcepub const fn prime_pi(self) -> usize
pub const fn prime_pi(self) -> usize
Counts the number of primes upto and including n
.
$$ \pi(x) $$
It upcasts internally to i128
for the inner operations.
§Panics
It can panic if n == i128|u128
, at the last iteration of a loop
that would take an unfeasable amount of time.
§Examples
assert_eq![1, Int(2_i64).prime_pi()];
assert_eq![2, Int(3_i64).prime_pi()];
assert_eq![31, Int(127_i64).prime_pi()];
assert_eq![0, Int(-5_i64).prime_pi()];
§Links
- https://mathworld.wolfram.com/PrimeCountingFunction.html.
- https://en.wikipedia.org/wiki/Prime-counting_function.
§Features
This will only be const if the _cmp_i64 feature is enabled.
Sourcepub const fn totient(self) -> Int<i64>
pub const fn totient(self) -> Int<i64>
Counts the number of integers $<|n|$ that are relatively prime to n
.
Note: If n
is negative, this function treats it as its absolute
value. For example, a value of -3
will be treated as 3
.
§Formulation
§Algorithm
This function iterates through all numbers from 2 up to the square
root of $|n|$. If it finds a divisor, it reduces n
by its factors
and adjusts result accordingly. If after the loop, $n > 1$, it
means n
has a prime factor greater than its square root, and the
function adjusts result for this last factor.
$$\large\varphi(n) =n \prod_{p\mid |n|} \left(1-\frac{1}{p}\right)$$
§Examples
assert_eq![Int(2), Int(4_i64).totient()];
assert_eq![Int(6), Int(9_i64).totient()];
assert_eq![Int(12), Int(13_i64).totient()];
assert_eq![Int(22), Int(-23_i64).totient()];
assert_eq![Int(2), Int(-3_i64).totient()];
§Links
Source§impl Int<u32>
Available on crate feature _int_u32
only.
impl Int<u32>
_int_u32
only.Sourcepub const fn is_prime(self) -> bool
pub const fn is_prime(self) -> bool
Returns true
if n
is prime.
This approach uses optimized trial division, which means it checks only odd numbers starting from 3 and up to the square root of the given number. This is based on the fact that if a number is divisible by a number larger than its square root, the result of the division will be smaller than the square root, and it would have already been checked in previous iterations.
§Examples
assert![Int(127_u32).is_prime()];
assert![Int(2_u32).is_prime()];
assert![!Int(1_u32).is_prime()];
§Features
This will only be const if the _cmp_u32 feature is enabled.
Sourcepub const fn prime_nth(self) -> Result<Int<u32>>
pub const fn prime_nth(self) -> Result<Int<u32>>
Finds the 0-indexed nth
prime number.
§Errors
Returns Overflow
if the result can’t fit the type.
§Examples
assert_eq![Ok(Int(2)), Int(0_u32).prime_nth()];
assert_eq![Ok(Int(3)), Int(1_u32).prime_nth()];
assert_eq![Ok(Int(251)), Int(53_u32).prime_nth()];
// assert![Int(54_u8).prime_nth().is_err()];
§Features
This will only be const if the _cmp_u32 feature is enabled.
Sourcepub const fn prime_pi(self) -> usize
pub const fn prime_pi(self) -> usize
Counts the number of primes upto and including n
.
$$ \pi(x) $$
It upcasts internally to u64
for the inner operations.
§Panics
It can panic if n == i128|u128
, at the last iteration of a loop
that would take an unfeasable amount of time.
§Examples
assert_eq![1, Int(2_u32).prime_pi()];
assert_eq![2, Int(3_u32).prime_pi()];
assert_eq![31, Int(127_u32).prime_pi()];
§Links
- https://mathworld.wolfram.com/PrimeCountingFunction.html.
- https://en.wikipedia.org/wiki/Prime-counting_function.
§Features
This will only be const if the _cmp_u32 feature is enabled.
Sourcepub const fn totient(self) -> Int<u32>
pub const fn totient(self) -> Int<u32>
Counts the number of integers $<n$ that are relatively prime to n
.
§Formulation
§Algorithm
This function iterates through all numbers from 2 up to the square
root of $|n|$. If it finds a divisor, it reduces n
by its factors
and adjusts result accordingly. If after the loop, $n > 1$, it
means n
has a prime factor greater than its square root, and the
function adjusts result for this last factor.
$$\large\varphi(n) =n \prod_{p\mid |n|} \left(1-\frac{1}{p}\right)$$
§Examples
assert_eq![Int(2), Int(4_u32).totient()];
assert_eq![Int(6), Int(9_u32).totient()];
assert_eq![Int(12), Int(13_u32).totient()];
§Links
Source§impl Int<i64>
Available on crate feature _int_i64
only.
impl Int<i64>
_int_i64
only.§Integer root related methods for i64
Sourcepub const fn is_square(self) -> bool
pub const fn is_square(self) -> bool
Returns true
if it’s a perfect square.
Returns false
otherwise, which includes all negative values.
$$
\large
\text{is\textunderscore square}(a) = \begin{cases}
\text{true} & \text{if } \left(\lfloor \sqrt{a} \rfloor\right)^2 = a \cr
\text{false} & \text{if } \left(\lfloor \sqrt{a} \rfloor\right)^2 \neq a
\end{cases}
$$
§Examples
assert_eq![Int(12_i64).is_square(), false];
assert_eq![Int(13_i64).is_square(), false];
assert_eq![Int(16_i64).is_square(), true];
assert_eq![Int(20_i64).is_square(), false];
assert_eq![Int(21_i64).is_square(), false];
assert_eq![Int(-16_i64).is_square(), false];
§Features
This will only be const if the _cmp_i64 feature is enabled.
Sourcepub const fn sqrt_ceil(self) -> Result<Int<i64>>
pub const fn sqrt_ceil(self) -> Result<Int<i64>>
Returns the ceiled integer square root.
§Errors
Returns NonNegativeRequired
if self
is negative.
§Formulation
$$ \large \begin{align} \notag \left\lceil \sqrt{a} \thinspace\right\rceil = \begin{cases} n & \text{if } n^2 = a \cr n+1 & \text{if } n^2 < a \end{cases} \cr \notag \normalsize\text{where } n = \lfloor \sqrt{a} \rfloor & \end{align} $$
§Examples
assert_eq![Int(12_i64).sqrt_ceil(), Ok(Int(4))];
assert_eq![Int(13_i64).sqrt_ceil(), Ok(Int(4))];
assert_eq![Int(16_i64).sqrt_ceil(), Ok(Int(4))];
assert_eq![Int(20_i64).sqrt_ceil(), Ok(Int(5))];
assert_eq![Int(21_i64).sqrt_ceil(), Ok(Int(5))];
assert_eq![Int(-4_i64).sqrt_ceil(), Err(NonNegativeRequired)];
§Features
This will only be const if the _cmp_i64 feature is enabled.
Sourcepub const fn sqrt_floor(self) -> Result<Int<i64>>
pub const fn sqrt_floor(self) -> Result<Int<i64>>
Returns the floored integer square root.
§Errors
Returns NonNegativeRequired
if self
is negative.
§Formulation
$$ \large \left\lfloor \sqrt{a} \right\rfloor = n_{k} $$
Where $n_{k}$ is the result of a sequence of estimates that starts with an initial $n_{0} = a/2$ which is updated using Heron’s method:
$$ \large n_{i+1} = n_{i} - ( n_{i}^{2} - a) / 2n_{i}, \quad \small\text{for} \quad i = 0, 1, \ldots, k, $$
Where $n_{i}$ is the current estimate, $n_{i+1}$ is the next estimate, $a$ is self, and $k$ is the number of iterations needed to converge to a solution, on the order of the number of bits of self, about $O(\log_2 b)$, which for e.g. 128 bits would be $ ±7 $ iterations.
Hence, the function continues updating the estimate until
reaching $n_{k}$, which provides the largest integer less than
or equal to the square root of a
.
§Examples
assert_eq![Int(12_i64).sqrt_floor(), Ok(Int(3))];
assert_eq![Int(13_i64).sqrt_floor(), Ok(Int(3))];
assert_eq![Int(16_i64).sqrt_floor(), Ok(Int(4))];
assert_eq![Int(20_i64).sqrt_floor(), Ok(Int(4))];
assert_eq![Int(21_i64).sqrt_floor(), Ok(Int(4))];
assert_eq![Int(-4_i64).sqrt_floor(), Err(NonNegativeRequired)];
§Features
This will only be const if the _cmp_i64 feature is enabled.
Sourcepub const fn sqrt_round(self) -> Result<Int<i64>>
pub const fn sqrt_round(self) -> Result<Int<i64>>
Returns the rounded integer square root.
Performs operations internally as i128
.
§Features
Uses unsafe_hint
for performance optimizations with upcasted arithmetic.
§Errors
Returns NonNegativeRequired
if self
is negative, or possibly Overflow
if there’s no larger type to upcast and the value is close to its maximum.
§Formulation
$$ \begin{align} \notag \left\lfloor\sqrt{a} \thinspace\right\rceil = \begin{cases} n & \text{if } a - n^2 < (n+1)^2 - a \cr n+1 & \text{if } a - n^2 \geq (n+1)^2 - a \end{cases} \cr \notag \normalsize\text{where } n = \lfloor \sqrt{a} \rfloor & \end{align} $$
§Examples
assert_eq![Int(12_i64).sqrt_round(), Ok(Int(3))];
assert_eq![Int(13_i64).sqrt_round(), Ok(Int(4))];
assert_eq![Int(16_i64).sqrt_round(), Ok(Int(4))];
assert_eq![Int(20_i64).sqrt_round(), Ok(Int(4))];
assert_eq![Int(21_i64).sqrt_round(), Ok(Int(5))];
assert_eq![Int(-4_i64).sqrt_round(), Err(NonNegativeRequired)];
Sourcepub const fn root_ceil(self, nth: u32) -> Result<Int<i64>>
pub const fn root_ceil(self, nth: u32) -> Result<Int<i64>>
Returns the ceiled integer nth
root.
$$ \large \left\lceil |a|^{\frac{1}{n}} \right\rceil \cdot \text{sign}(a) = m $$
§Errors
Returns NonZeroRequired
if nth
is 0, or
NonNegativeRequired
if self
is negative and nth
is even.
§Examples
assert_eq![Int(48_i64).root_ceil(4), Ok(Int(3))];
assert_eq![Int(70_i64).root_ceil(4), Ok(Int(3))];
assert_eq![Int(81_i64).root_ceil(4), Ok(Int(3))];
assert_eq![Int(88_i64).root_ceil(4), Ok(Int(4))];
assert_eq![Int(114_i64).root_ceil(4), Ok(Int(4))];
assert_eq![Int(-81i64).root_ceil(4), Err(NonNegativeRequired)];
assert_eq![Int(i64::MAX).root_ceil(1), Ok(Int(i64::MAX))];
§Formulation
§Piece-wise
$$ \large \begin{align} \notag \text{If } n = 0, \text{ then error.} \cr \notag \text{If } n = 1, \text{ then output } a. \cr \notag \text{If } a = 0, \text{ then output } 0. \cr \notag \text{If } a < 0 \text{ and } n \% 2 = 0, \text{ then error.} \cr \notag m = \lfloor |a|^{\frac{1}{n}} \rfloor \cr \notag \left\lceil |a|^{\frac{1}{n}} \right\rceil = \begin{cases} m & \text{if } m^n = |a|, \cr m+1 & \text{if } m^n < |a|. \end{cases} \cr \notag \text{Output: } m \cdot \text{sign}(a) \end{align} $$
§Algorithm
The algorithm computes the smallest integer $ m $ such that: $$ \large m^n \geq |a|. $$ Subject to the condition: $$ \large a < 0 \quad \text{and} \quad n \% 2 = 0 \quad \text{is invalid.} $$ The process is as follows:
- Iteratively tests values starting from $ m = 1 $, calculating $ m^n $ until $ m^n > |a| $.
- Computes the floored nth root as $ m - 1 $.
- Checks if $ (m - 1)^n = |a| $.
- If true, returns $ m - 1 \cdot \text{sign}(a) $.
- Otherwise, returns $ m \cdot \text{sign}(a) $, the smallest integer such that $ m^n \geq |a| $.
Sourcepub const fn root_floor(self, nth: u32) -> Result<Int<i64>>
pub const fn root_floor(self, nth: u32) -> Result<Int<i64>>
Returns the floored integer nth
root.
$$ \large \left\lfloor |a|^{\frac{1}{n}} \right\rfloor = m \cdot \text{sign}(a) $$
§Errors
Returns NonZeroRequired
if nth
is 0, or
NonNegativeRequired
if self
is negative and nth
is even.
§Examples
assert_eq![Int(48_i64).root_floor(4), Ok(Int(2))];
assert_eq![Int(70_i64).root_floor(4), Ok(Int(2))];
assert_eq![Int(81_i64).root_floor(4), Ok(Int(3))];
assert_eq![Int(88_i64).root_floor(4), Ok(Int(3))];
assert_eq![Int(114_i64).root_floor(4), Ok(Int(3))];
assert_eq![Int(-81_i64).root_floor(4), Err(NonNegativeRequired)];
assert_eq![Int(i64::MAX).root_floor(1), Ok(Int(i64::MAX))];
§Formulations
§Piece-wise
$$ \large \begin{align} \notag \text{If } n = 0, \text{ then error.} \cr \notag \text{If } n = 1, \text{ then output } a. \cr \notag \text{If } a = 0, \text{ then output } 0. \cr \notag \text{If } a < 0 \text{ and } n % 2 = 0, \text{ then error.} \cr \notag m = \max { k \in \Z \mid k^n \leq |a| } \cr \notag \text{Output: } m \cdot \text{sign}(a) & \end{align} $$
§Algorithm
The algorithm computes the floored nth root, $ \left\lfloor |a|^{\frac{1}{n}} \right\rfloor = m \cdot \text{sign}(a) $, where $ m $ is the largest integer such that: $$ \large m^n \leq |a|, $$ subject to the condition: $$ \large a < 0 \quad \text{and} \quad n % 2 = 0 \quad \text{is invalid.} $$ The algorithm incrementally tests values starting from $ m = 1 $ and continues until the next value $ m+1 $ satisfies: $$ \large (m+1)^n > |a|. $$ The function then returns $ m \cdot \text{sign}(a) $, the largest integer such that $ m^n \leq |a| $, with the sign adjusted for signed integers.
Source§impl Int<u32>
Available on crate feature _int_u32
only.
impl Int<u32>
_int_u32
only.§Integer root related methods for u32
Sourcepub const fn is_square(self) -> bool
pub const fn is_square(self) -> bool
Returns true
if it’s a perfect square, false otherwise.
§Formulation
$$ \large \text{is\textunderscore square}(a) = \begin{cases} \text{true} & \text{if } \left(\lfloor \sqrt{a} \rfloor\right)^2 = a \cr \text{false} & \text{if } \left(\lfloor \sqrt{a} \rfloor\right)^2 \neq a \end{cases} $$
§Examples
assert_eq![Int(12_u32).is_square(), false];
assert_eq![Int(13_u32).is_square(), false];
assert_eq![Int(16_u32).is_square(), true];
assert_eq![Int(20_u32).is_square(), false];
assert_eq![Int(21_u32).is_square(), false];
§Features
This will only be const if the _cmp_u32 feature is enabled.
Sourcepub const fn sqrt_ceil(self) -> Int<u32>
pub const fn sqrt_ceil(self) -> Int<u32>
Returns the ceiled integer square root.
§Formulation
$$ \large \begin{align} \notag \left\lceil \sqrt{a} \thinspace\right\rceil = \begin{cases} n & \text{if } n^2 = a \cr n+1 & \text{if } n^2 < a \end{cases} \cr \notag \normalsize\text{where } n = \lfloor \sqrt{a} \rfloor & \end{align} $$
§Examples
assert_eq![Int(12_u32).sqrt_ceil(), Int(4)];
assert_eq![Int(13_u32).sqrt_ceil(), Int(4)];
assert_eq![Int(16_u32).sqrt_ceil(), Int(4)];
assert_eq![Int(20_u32).sqrt_ceil(), Int(5)];
assert_eq![Int(21_u32).sqrt_ceil(), Int(5)];
§Features
This will only be const if the _cmp_u32 feature is enabled.
Sourcepub const fn sqrt_floor(self) -> Int<u32>
pub const fn sqrt_floor(self) -> Int<u32>
Returns the floored integer square root.
§Formulation
$$ \large \left\lfloor \sqrt{a} \right\rfloor = n_{k} $$
Where $n_{k}$ is the result of a sequence of estimates that starts with an initial $n_{0} = a/2$ which is updated using Heron’s method:
$$ \large n_{i+1} = n_{i} - ( n_{i}^{2} - a) / 2n_{i}, \quad \small\text{for} \quad i = 0, 1, \ldots, k, $$
Where $n_{i}$ is the current estimate, $n_{i+1}$ is the next estimate, $a$ is self, and $k$ is the number of iterations needed to converge to a solution, on the order of the number of bits of self, about $O(\log_2 b)$, which for e.g. 128 bits would be $ ±7 $ iterations.
Hence, the function continues updating the estimate until
reaching $n_{k}$, which provides the largest integer less than
or equal to the square root of a
.
§Examples
assert_eq![Int(12_u32).sqrt_floor(), Int(3)];
assert_eq![Int(13_u32).sqrt_floor(), Int(3)];
assert_eq![Int(16_u32).sqrt_floor(), Int(4)];
assert_eq![Int(20_u32).sqrt_floor(), Int(4)];
assert_eq![Int(21_u32).sqrt_floor(), Int(4)];
§Features
This will only be const if the _cmp_u32 feature is enabled.
Sourcepub const fn sqrt_round(self) -> Result<Int<u32>>
pub const fn sqrt_round(self) -> Result<Int<u32>>
Returns the rounded integer square root.
Performs operations internally as u64
.
§Features
Uses unsafe_hint
for performance optimizations with upcasted arithmetic.
§Errors
Can returns Overflow
if there’s no larger type to upcast and the value
is close to its maximum.
§Formulation
$$ \begin{align} \notag \left\lfloor\sqrt{a} \thinspace\right\rceil = \begin{cases} n & \text{if } a - n^2 < (n+1)^2 - a \cr n+1 & \text{if } a - n^2 \geq (n+1)^2 - a \end{cases} \cr \notag \normalsize\text{where } n = \lfloor \sqrt{a} \rfloor & \end{align} $$
§Examples
assert_eq![Int(12_u32).sqrt_round(), Ok(Int(3))];
assert_eq![Int(13_u32).sqrt_round(), Ok(Int(4))];
assert_eq![Int(16_u32).sqrt_round(), Ok(Int(4))];
assert_eq![Int(20_u32).sqrt_round(), Ok(Int(4))];
assert_eq![Int(21_u32).sqrt_round(), Ok(Int(5))];
Sourcepub const fn root_ceil(self, nth: u32) -> Result<Int<u32>>
pub const fn root_ceil(self, nth: u32) -> Result<Int<u32>>
Returns the ceiled integer nth
root.
$$ \large \left\lceil a^{\frac{1}{n}} \right\rceil = m $$
§Errors
Returns NonZeroRequired
if nth
is 0.
§Examples
assert_eq![Int(48_u32).root_ceil(4), Ok(Int(3))];
assert_eq![Int(70_u32).root_ceil(4), Ok(Int(3))];
assert_eq![Int(81_u32).root_ceil(4), Ok(Int(3))];
assert_eq![Int(88_u32).root_ceil(4), Ok(Int(4))];
assert_eq![Int(114_u32).root_ceil(4), Ok(Int(4))];
assert_eq![Int(u32::MAX).root_ceil(1), Ok(Int(u32::MAX))];
§Formulation
§Piece-wise
$$ \large \begin{align} \notag \text{If } n = 0, \text{ then error.} \cr \notag \text{If } n = 1, \text{ then output } a. \cr \notag \text{If } a = 0, \text{ then output } 0. \cr \notag m = \lfloor a^{\frac{1}{n}} \rfloor \cr \notag \left\lceil a^{\frac{1}{n}} \right\rceil = \begin{cases} m & \text{if } m^n = a, \cr m+1 & \text{if } m^n < a. \end{cases} \end{align} $$
§Algorithm
The algorithm computes the smallest integer $ m $ such that: $$ \large m^n \geq a. $$ It first computes the floored nth root $ \lfloor a^{\frac{1}{n}} \rfloor $ and then:
- Checks if $ \lfloor a^{\frac{1}{n}} \rfloor^n = a $.
- If true, returns $ m = \lfloor a^{\frac{1}{n}} \rfloor $.
- Otherwise, returns $ m = \lfloor a^{\frac{1}{n}} \rfloor + 1 $.
Sourcepub const fn root_floor(self, nth: u32) -> Result<Int<u32>>
pub const fn root_floor(self, nth: u32) -> Result<Int<u32>>
Returns the floored integer nth
root.
$$ \large \left\lfloor a^{\frac{1}{n}} \right\rfloor = m $$
§Errors
Returns NonZeroRequired
if nth
is 0.
§Examples
assert_eq![Int(48_u32).root_floor(4), Ok(Int(2))];
assert_eq![Int(70_u32).root_floor(4), Ok(Int(2))];
assert_eq![Int(81_u32).root_floor(4), Ok(Int(3))];
assert_eq![Int(88_u32).root_floor(4), Ok(Int(3))];
assert_eq![Int(114_u32).root_floor(4), Ok(Int(3))];
assert_eq![Int(u32::MAX).root_floor(1), Ok(Int(u32::MAX))];
§Formulations
§Piece-wise
$$ \large \begin{align} \notag \text{If } n = 0, \text{ then error.} \cr \notag \text{If } n = 1, \text{ then output } a. \cr \notag \text{If } a = 0, \text{ then output } 0. \cr \notag m = \max { k \in \Z_{\geq 0} \mid k^n \leq a } \cr \notag \text{Output: } m & \end{align} $$
§Algorithm
The algorithm computes the floored nth root, $ \left\lfloor a^{\frac{1}{n}} \right\rfloor = m $, where $ m $ is the largest integer such that: $$ \large m^n \leq a. $$ It incrementally tests values starting from $ m = 1 $ and continues until the next value $ m+1 $ satisfies: $$ \large (m+1)^n > a. $$ The function then returns $ m $.
Trait Implementations§
Source§impl<'o> AddAssign<&'o i64> for Int<i64>
Available on crate feature _int_i64
only.
impl<'o> AddAssign<&'o i64> for Int<i64>
_int_i64
only.Source§fn add_assign(&mut self, other: &'o i64)
fn add_assign(&mut self, other: &'o i64)
+=
operation. Read moreSource§impl<'o> AddAssign<&'o u32> for Int<u32>
Available on crate feature _int_u32
only.
impl<'o> AddAssign<&'o u32> for Int<u32>
_int_u32
only.Source§fn add_assign(&mut self, other: &'o u32)
fn add_assign(&mut self, other: &'o u32)
+=
operation. Read moreSource§impl AddAssign<i64> for Int<i64>
Available on crate feature _int_i64
only.
impl AddAssign<i64> for Int<i64>
_int_i64
only.Source§fn add_assign(&mut self, other: i64)
fn add_assign(&mut self, other: i64)
+=
operation. Read moreSource§impl AddAssign<u32> for Int<u32>
Available on crate feature _int_u32
only.
impl AddAssign<u32> for Int<u32>
_int_u32
only.Source§fn add_assign(&mut self, other: u32)
fn add_assign(&mut self, other: u32)
+=
operation. Read moreSource§impl<'o> DivAssign<&'o i64> for Int<i64>
Available on crate feature _int_i64
only.
impl<'o> DivAssign<&'o i64> for Int<i64>
_int_i64
only.Source§fn div_assign(&mut self, other: &'o i64)
fn div_assign(&mut self, other: &'o i64)
/=
operation. Read moreSource§impl<'o> DivAssign<&'o u32> for Int<u32>
Available on crate feature _int_u32
only.
impl<'o> DivAssign<&'o u32> for Int<u32>
_int_u32
only.Source§fn div_assign(&mut self, other: &'o u32)
fn div_assign(&mut self, other: &'o u32)
/=
operation. Read moreSource§impl DivAssign<i64> for Int<i64>
Available on crate feature _int_i64
only.
impl DivAssign<i64> for Int<i64>
_int_i64
only.Source§fn div_assign(&mut self, other: i64)
fn div_assign(&mut self, other: i64)
/=
operation. Read moreSource§impl DivAssign<u32> for Int<u32>
Available on crate feature _int_u32
only.
impl DivAssign<u32> for Int<u32>
_int_u32
only.Source§fn div_assign(&mut self, other: u32)
fn div_assign(&mut self, other: u32)
/=
operation. Read moreSource§impl<T: Hasher> Hasher for Int<T>
impl<T: Hasher> Hasher for Int<T>
1.26.0 · Source§fn write_u128(&mut self, i: u128)
fn write_u128(&mut self, i: u128)
u128
into this hasher.1.3.0 · Source§fn write_usize(&mut self, i: usize)
fn write_usize(&mut self, i: usize)
usize
into this hasher.1.26.0 · Source§fn write_i128(&mut self, i: i128)
fn write_i128(&mut self, i: i128)
i128
into this hasher.1.3.0 · Source§fn write_isize(&mut self, i: isize)
fn write_isize(&mut self, i: isize)
isize
into this hasher.Source§fn write_length_prefix(&mut self, len: usize)
fn write_length_prefix(&mut self, len: usize)
hasher_prefixfree_extras
)Source§impl<'o> MulAssign<&'o i64> for Int<i64>
Available on crate feature _int_i64
only.
impl<'o> MulAssign<&'o i64> for Int<i64>
_int_i64
only.Source§fn mul_assign(&mut self, other: &'o i64)
fn mul_assign(&mut self, other: &'o i64)
*=
operation. Read moreSource§impl<'o> MulAssign<&'o u32> for Int<u32>
Available on crate feature _int_u32
only.
impl<'o> MulAssign<&'o u32> for Int<u32>
_int_u32
only.Source§fn mul_assign(&mut self, other: &'o u32)
fn mul_assign(&mut self, other: &'o u32)
*=
operation. Read moreSource§impl MulAssign<i64> for Int<i64>
Available on crate feature _int_i64
only.
impl MulAssign<i64> for Int<i64>
_int_i64
only.Source§fn mul_assign(&mut self, other: i64)
fn mul_assign(&mut self, other: i64)
*=
operation. Read moreSource§impl MulAssign<u32> for Int<u32>
Available on crate feature _int_u32
only.
impl MulAssign<u32> for Int<u32>
_int_u32
only.Source§fn mul_assign(&mut self, other: u32)
fn mul_assign(&mut self, other: u32)
*=
operation. Read moreSource§impl<T: Ord> Ord for Int<T>
impl<T: Ord> Ord for Int<T>
Source§impl<T: PartialOrd> PartialOrd<T> for Int<T>
impl<T: PartialOrd> PartialOrd<T> for Int<T>
Source§impl<T: PartialOrd> PartialOrd for Int<T>
impl<T: PartialOrd> PartialOrd for Int<T>
Source§impl<'o> RemAssign<&'o i64> for Int<i64>
Available on crate feature _int_i64
only.
impl<'o> RemAssign<&'o i64> for Int<i64>
_int_i64
only.Source§fn rem_assign(&mut self, other: &'o i64)
fn rem_assign(&mut self, other: &'o i64)
%=
operation. Read moreSource§impl<'o> RemAssign<&'o u32> for Int<u32>
Available on crate feature _int_u32
only.
impl<'o> RemAssign<&'o u32> for Int<u32>
_int_u32
only.Source§fn rem_assign(&mut self, other: &'o u32)
fn rem_assign(&mut self, other: &'o u32)
%=
operation. Read moreSource§impl RemAssign<i64> for Int<i64>
Available on crate feature _int_i64
only.
impl RemAssign<i64> for Int<i64>
_int_i64
only.Source§fn rem_assign(&mut self, other: i64)
fn rem_assign(&mut self, other: i64)
%=
operation. Read moreSource§impl RemAssign<u32> for Int<u32>
Available on crate feature _int_u32
only.
impl RemAssign<u32> for Int<u32>
_int_u32
only.Source§fn rem_assign(&mut self, other: u32)
fn rem_assign(&mut self, other: u32)
%=
operation. Read moreSource§impl<'o> SubAssign<&'o i64> for Int<i64>
Available on crate feature _int_i64
only.
impl<'o> SubAssign<&'o i64> for Int<i64>
_int_i64
only.Source§fn sub_assign(&mut self, other: &'o i64)
fn sub_assign(&mut self, other: &'o i64)
-=
operation. Read moreSource§impl<'o> SubAssign<&'o u32> for Int<u32>
Available on crate feature _int_u32
only.
impl<'o> SubAssign<&'o u32> for Int<u32>
_int_u32
only.Source§fn sub_assign(&mut self, other: &'o u32)
fn sub_assign(&mut self, other: &'o u32)
-=
operation. Read moreSource§impl SubAssign<i64> for Int<i64>
Available on crate feature _int_i64
only.
impl SubAssign<i64> for Int<i64>
_int_i64
only.Source§fn sub_assign(&mut self, other: i64)
fn sub_assign(&mut self, other: i64)
-=
operation. Read moreSource§impl SubAssign<u32> for Int<u32>
Available on crate feature _int_u32
only.
impl SubAssign<u32> for Int<u32>
_int_u32
only.Source§fn sub_assign(&mut self, other: u32)
fn sub_assign(&mut self, other: u32)
-=
operation. Read moreimpl<T: Copy> Copy for Int<T>
impl<T: Eq> Eq for Int<T>
Auto Trait Implementations§
impl<T> Freeze for Int<T>where
T: Freeze,
impl<T> RefUnwindSafe for Int<T>where
T: RefUnwindSafe,
impl<T> Send for Int<T>where
T: Send,
impl<T> Sync for Int<T>where
T: Sync,
impl<T> Unpin for Int<T>where
T: Unpin,
impl<T> UnwindSafe for Int<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> ByteSized for T
impl<T> ByteSized for T
Source§const BYTE_ALIGN: usize = _
const BYTE_ALIGN: usize = _
Source§fn byte_align(&self) -> usize
fn byte_align(&self) -> usize
Source§fn ptr_size_ratio(&self) -> [usize; 2]
fn ptr_size_ratio(&self) -> [usize; 2]
Source§impl<T, R> Chain<R> for Twhere
T: ?Sized,
impl<T, R> Chain<R> for Twhere
T: ?Sized,
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<T> ExtAny for T
impl<T> ExtAny for T
Source§fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
TypeId
of Self
using a custom hasher.Source§fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
Source§impl<T> ExtMem for Twhere
T: ?Sized,
impl<T> ExtMem for Twhere
T: ?Sized,
Source§const NEEDS_DROP: bool = _
const NEEDS_DROP: bool = _
Source§fn mem_align_of<T>() -> usize
fn mem_align_of<T>() -> usize
Source§fn mem_align_of_val(&self) -> usize
fn mem_align_of_val(&self) -> usize
Source§fn mem_size_of<T>() -> usize
fn mem_size_of<T>() -> usize
Source§fn mem_size_of_val(&self) -> usize
fn mem_size_of_val(&self) -> usize
Source§fn mem_needs_drop(&self) -> bool
fn mem_needs_drop(&self) -> bool
true
if dropping values of this type matters. Read moreSource§fn mem_forget(self)where
Self: Sized,
fn mem_forget(self)where
Self: Sized,
self
without running its destructor. Read moreSource§fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
Source§unsafe fn mem_zeroed<T>() -> T
unsafe fn mem_zeroed<T>() -> T
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§fn mem_as_bytes(&self) -> &[u8] ⓘ
fn mem_as_bytes(&self) -> &[u8] ⓘ
unsafe_slice
only.§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> Hook for T
impl<T> Hook for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more