#[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<i8>
Available on crate feature _int_i8
only.
impl Int<i8>
_int_i8
only.§Integer base related methods for i8
Sourcepub const fn digits(self) -> Int<i8>
pub const fn digits(self) -> Int<i8>
Returns the number of digits in base 10.
§Examples
assert_eq![Int(1), Int(0_i8).digits()];
assert_eq![Int(1), Int(-1_i8).digits()];
assert_eq![Int(3), Int(127_i8).digits()];
assert_eq![Int(3), Int(-128_i8).digits()];
Sourcepub const fn digits_sign(self) -> Int<i8>
pub const fn digits_sign(self) -> Int<i8>
Returns the number of digits in base 10, including the negative sign.
§Examples
assert_eq![Int(1), Int(0_i8).digits_sign()];
assert_eq![Int(2), Int(-1_i8).digits_sign()];
assert_eq![Int(3), Int(127_i8).digits_sign()];
assert_eq![Int(4), Int(-128_i8).digits_sign()];
Sourcepub const fn digits_base(self, base: i8) -> Int<i8>
pub const fn digits_base(self, base: i8) -> Int<i8>
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_i8).digits_base(2)];
assert_eq![Int(2), Int(127_i8).digits_base(16)];
assert_eq![Int(2), Int(-128_i8).digits_base(16)];
assert_eq![Int(2), Int(-128_i8).digits_base(-16)];
assert_eq![Int(0), Int(100_i8).digits_base(0)];
assert_eq![Int(1), Int(0_i8).digits_base(100)];
Sourcepub const fn digits_base_sign(self, base: i8) -> Int<i8>
pub const fn digits_base_sign(self, base: i8) -> Int<i8>
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_i8).digits_base_sign(2)];
assert_eq![Int(2), Int(127_i8).digits_base_sign(16)];
assert_eq![Int(3), Int(-128_i8).digits_base_sign(16)];
assert_eq![Int(3), Int(-128_i8).digits_base_sign(-16)];
assert_eq![Int(0), Int(100_i8).digits_base_sign(0)];
assert_eq![Int(1), Int(0_i8).digits_base_sign(100)];
Sourcepub const fn digital_root(self) -> Int<i8>
pub const fn digital_root(self) -> Int<i8>
Returns the digital root in base 10.
§Examples
assert_eq![Int(1), Int(127_i8).digital_root()];
assert_eq![Int(1), Int(-127_i8).digital_root()];
assert_eq![Int(9), Int(126_i8).digital_root()];
Sourcepub const fn digital_root_base(self, base: i8) -> Int<i8>
pub const fn digital_root_base(self, base: i8) -> Int<i8>
Returns the digital root in in the given absolute base
.
§Examples
assert_eq![Int(1), Int(127_i8).digital_root_base(10)];
assert_eq![Int(1), Int(127_i8).digital_root_base(-10)];
assert_eq![Int(1), Int(-127_i8).digital_root_base(-10)];
assert_eq![Int(9), Int(-126_i8).digital_root_base(10)];
assert_eq![Int(3), Int(-33_i8).digital_root_base(16)];
Source§impl Int<i16>
Available on crate feature _int_i16
only.
impl Int<i16>
_int_i16
only.§Integer base related methods for i16
Sourcepub const fn digits(self) -> Int<i16>
pub const fn digits(self) -> Int<i16>
Returns the number of digits in base 10.
§Examples
assert_eq![Int(1), Int(0_i16).digits()];
assert_eq![Int(1), Int(-1_i16).digits()];
assert_eq![Int(3), Int(127_i16).digits()];
assert_eq![Int(3), Int(-128_i16).digits()];
Sourcepub const fn digits_sign(self) -> Int<i16>
pub const fn digits_sign(self) -> Int<i16>
Returns the number of digits in base 10, including the negative sign.
§Examples
assert_eq![Int(1), Int(0_i16).digits_sign()];
assert_eq![Int(2), Int(-1_i16).digits_sign()];
assert_eq![Int(3), Int(127_i16).digits_sign()];
assert_eq![Int(4), Int(-128_i16).digits_sign()];
Sourcepub const fn digits_base(self, base: i16) -> Int<i16>
pub const fn digits_base(self, base: i16) -> Int<i16>
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_i16).digits_base(2)];
assert_eq![Int(2), Int(127_i16).digits_base(16)];
assert_eq![Int(2), Int(-128_i16).digits_base(16)];
assert_eq![Int(2), Int(-128_i16).digits_base(-16)];
assert_eq![Int(0), Int(100_i16).digits_base(0)];
assert_eq![Int(1), Int(0_i16).digits_base(100)];
Sourcepub const fn digits_base_sign(self, base: i16) -> Int<i16>
pub const fn digits_base_sign(self, base: i16) -> Int<i16>
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_i16).digits_base_sign(2)];
assert_eq![Int(2), Int(127_i16).digits_base_sign(16)];
assert_eq![Int(3), Int(-128_i16).digits_base_sign(16)];
assert_eq![Int(3), Int(-128_i16).digits_base_sign(-16)];
assert_eq![Int(0), Int(100_i16).digits_base_sign(0)];
assert_eq![Int(1), Int(0_i16).digits_base_sign(100)];
Sourcepub const fn digital_root(self) -> Int<i16>
pub const fn digital_root(self) -> Int<i16>
Returns the digital root in base 10.
§Examples
assert_eq![Int(1), Int(127_i16).digital_root()];
assert_eq![Int(1), Int(-127_i16).digital_root()];
assert_eq![Int(9), Int(126_i16).digital_root()];
Sourcepub const fn digital_root_base(self, base: i16) -> Int<i16>
pub const fn digital_root_base(self, base: i16) -> Int<i16>
Returns the digital root in in the given absolute base
.
§Examples
assert_eq![Int(1), Int(127_i16).digital_root_base(10)];
assert_eq![Int(1), Int(127_i16).digital_root_base(-10)];
assert_eq![Int(1), Int(-127_i16).digital_root_base(-10)];
assert_eq![Int(9), Int(-126_i16).digital_root_base(10)];
assert_eq![Int(3), Int(-33_i16).digital_root_base(16)];
Source§impl Int<i32>
Available on crate feature _int_i32
only.
impl Int<i32>
_int_i32
only.§Integer base related methods for i32
Sourcepub const fn digits(self) -> Int<i32>
pub const fn digits(self) -> Int<i32>
Returns the number of digits in base 10.
§Examples
assert_eq![Int(1), Int(0_i32).digits()];
assert_eq![Int(1), Int(-1_i32).digits()];
assert_eq![Int(3), Int(127_i32).digits()];
assert_eq![Int(3), Int(-128_i32).digits()];
Sourcepub const fn digits_sign(self) -> Int<i32>
pub const fn digits_sign(self) -> Int<i32>
Returns the number of digits in base 10, including the negative sign.
§Examples
assert_eq![Int(1), Int(0_i32).digits_sign()];
assert_eq![Int(2), Int(-1_i32).digits_sign()];
assert_eq![Int(3), Int(127_i32).digits_sign()];
assert_eq![Int(4), Int(-128_i32).digits_sign()];
Sourcepub const fn digits_base(self, base: i32) -> Int<i32>
pub const fn digits_base(self, base: i32) -> Int<i32>
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_i32).digits_base(2)];
assert_eq![Int(2), Int(127_i32).digits_base(16)];
assert_eq![Int(2), Int(-128_i32).digits_base(16)];
assert_eq![Int(2), Int(-128_i32).digits_base(-16)];
assert_eq![Int(0), Int(100_i32).digits_base(0)];
assert_eq![Int(1), Int(0_i32).digits_base(100)];
Sourcepub const fn digits_base_sign(self, base: i32) -> Int<i32>
pub const fn digits_base_sign(self, base: i32) -> Int<i32>
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_i32).digits_base_sign(2)];
assert_eq![Int(2), Int(127_i32).digits_base_sign(16)];
assert_eq![Int(3), Int(-128_i32).digits_base_sign(16)];
assert_eq![Int(3), Int(-128_i32).digits_base_sign(-16)];
assert_eq![Int(0), Int(100_i32).digits_base_sign(0)];
assert_eq![Int(1), Int(0_i32).digits_base_sign(100)];
Sourcepub const fn digital_root(self) -> Int<i32>
pub const fn digital_root(self) -> Int<i32>
Returns the digital root in base 10.
§Examples
assert_eq![Int(1), Int(127_i32).digital_root()];
assert_eq![Int(1), Int(-127_i32).digital_root()];
assert_eq![Int(9), Int(126_i32).digital_root()];
Sourcepub const fn digital_root_base(self, base: i32) -> Int<i32>
pub const fn digital_root_base(self, base: i32) -> Int<i32>
Returns the digital root in in the given absolute base
.
§Examples
assert_eq![Int(1), Int(127_i32).digital_root_base(10)];
assert_eq![Int(1), Int(127_i32).digital_root_base(-10)];
assert_eq![Int(1), Int(-127_i32).digital_root_base(-10)];
assert_eq![Int(9), Int(-126_i32).digital_root_base(10)];
assert_eq![Int(3), Int(-33_i32).digital_root_base(16)];
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<i128>
Available on crate feature _int_i128
only.
impl Int<i128>
_int_i128
only.§Integer base related methods for i128
Sourcepub const fn digits(self) -> Int<i128>
pub const fn digits(self) -> Int<i128>
Returns the number of digits in base 10.
§Examples
assert_eq![Int(1), Int(0_i128).digits()];
assert_eq![Int(1), Int(-1_i128).digits()];
assert_eq![Int(3), Int(127_i128).digits()];
assert_eq![Int(3), Int(-128_i128).digits()];
Sourcepub const fn digits_sign(self) -> Int<i128>
pub const fn digits_sign(self) -> Int<i128>
Returns the number of digits in base 10, including the negative sign.
§Examples
assert_eq![Int(1), Int(0_i128).digits_sign()];
assert_eq![Int(2), Int(-1_i128).digits_sign()];
assert_eq![Int(3), Int(127_i128).digits_sign()];
assert_eq![Int(4), Int(-128_i128).digits_sign()];
Sourcepub const fn digits_base(self, base: i128) -> Int<i128>
pub const fn digits_base(self, base: i128) -> Int<i128>
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_i128).digits_base(2)];
assert_eq![Int(2), Int(127_i128).digits_base(16)];
assert_eq![Int(2), Int(-128_i128).digits_base(16)];
assert_eq![Int(2), Int(-128_i128).digits_base(-16)];
assert_eq![Int(0), Int(100_i128).digits_base(0)];
assert_eq![Int(1), Int(0_i128).digits_base(100)];
Sourcepub const fn digits_base_sign(self, base: i128) -> Int<i128>
pub const fn digits_base_sign(self, base: i128) -> Int<i128>
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_i128).digits_base_sign(2)];
assert_eq![Int(2), Int(127_i128).digits_base_sign(16)];
assert_eq![Int(3), Int(-128_i128).digits_base_sign(16)];
assert_eq![Int(3), Int(-128_i128).digits_base_sign(-16)];
assert_eq![Int(0), Int(100_i128).digits_base_sign(0)];
assert_eq![Int(1), Int(0_i128).digits_base_sign(100)];
Sourcepub const fn digital_root(self) -> Int<i128>
pub const fn digital_root(self) -> Int<i128>
Returns the digital root in base 10.
§Examples
assert_eq![Int(1), Int(127_i128).digital_root()];
assert_eq![Int(1), Int(-127_i128).digital_root()];
assert_eq![Int(9), Int(126_i128).digital_root()];
Sourcepub const fn digital_root_base(self, base: i128) -> Int<i128>
pub const fn digital_root_base(self, base: i128) -> Int<i128>
Returns the digital root in in the given absolute base
.
§Examples
assert_eq![Int(1), Int(127_i128).digital_root_base(10)];
assert_eq![Int(1), Int(127_i128).digital_root_base(-10)];
assert_eq![Int(1), Int(-127_i128).digital_root_base(-10)];
assert_eq![Int(9), Int(-126_i128).digital_root_base(10)];
assert_eq![Int(3), Int(-33_i128).digital_root_base(16)];
Source§impl Int<isize>
Available on crate feature _int_isize
only.
impl Int<isize>
_int_isize
only.§Integer base related methods for isize
Sourcepub const fn digits(self) -> Int<isize>
pub const fn digits(self) -> Int<isize>
Returns the number of digits in base 10.
§Examples
assert_eq![Int(1), Int(0_isize).digits()];
assert_eq![Int(1), Int(-1_isize).digits()];
assert_eq![Int(3), Int(127_isize).digits()];
assert_eq![Int(3), Int(-128_isize).digits()];
Sourcepub const fn digits_sign(self) -> Int<isize>
pub const fn digits_sign(self) -> Int<isize>
Returns the number of digits in base 10, including the negative sign.
§Examples
assert_eq![Int(1), Int(0_isize).digits_sign()];
assert_eq![Int(2), Int(-1_isize).digits_sign()];
assert_eq![Int(3), Int(127_isize).digits_sign()];
assert_eq![Int(4), Int(-128_isize).digits_sign()];
Sourcepub const fn digits_base(self, base: isize) -> Int<isize>
pub const fn digits_base(self, base: isize) -> Int<isize>
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_isize).digits_base(2)];
assert_eq![Int(2), Int(127_isize).digits_base(16)];
assert_eq![Int(2), Int(-128_isize).digits_base(16)];
assert_eq![Int(2), Int(-128_isize).digits_base(-16)];
assert_eq![Int(0), Int(100_isize).digits_base(0)];
assert_eq![Int(1), Int(0_isize).digits_base(100)];
Sourcepub const fn digits_base_sign(self, base: isize) -> Int<isize>
pub const fn digits_base_sign(self, base: isize) -> Int<isize>
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_isize).digits_base_sign(2)];
assert_eq![Int(2), Int(127_isize).digits_base_sign(16)];
assert_eq![Int(3), Int(-128_isize).digits_base_sign(16)];
assert_eq![Int(3), Int(-128_isize).digits_base_sign(-16)];
assert_eq![Int(0), Int(100_isize).digits_base_sign(0)];
assert_eq![Int(1), Int(0_isize).digits_base_sign(100)];
Sourcepub const fn digital_root(self) -> Int<isize>
pub const fn digital_root(self) -> Int<isize>
Returns the digital root in base 10.
§Examples
assert_eq![Int(1), Int(127_isize).digital_root()];
assert_eq![Int(1), Int(-127_isize).digital_root()];
assert_eq![Int(9), Int(126_isize).digital_root()];
Sourcepub const fn digital_root_base(self, base: isize) -> Int<isize>
pub const fn digital_root_base(self, base: isize) -> Int<isize>
Returns the digital root in in the given absolute base
.
§Examples
assert_eq![Int(1), Int(127_isize).digital_root_base(10)];
assert_eq![Int(1), Int(127_isize).digital_root_base(-10)];
assert_eq![Int(1), Int(-127_isize).digital_root_base(-10)];
assert_eq![Int(9), Int(-126_isize).digital_root_base(10)];
assert_eq![Int(3), Int(-33_isize).digital_root_base(16)];
Source§impl Int<u8>
Available on crate feature _int_u8
only.
impl Int<u8>
_int_u8
only.§Integer base related methods for u8
Sourcepub const fn digits(self) -> Int<u8>
pub const fn digits(self) -> Int<u8>
Returns the number of digits in base 10.
§Examples
assert_eq![Int(1), Int(0_u8).digits()];
assert_eq![Int(3), Int(127_u8).digits()];
Sourcepub const fn digits_sign(self) -> Int<u8>
pub const fn digits_sign(self) -> Int<u8>
An alias of digits
.
Sourcepub const fn digits_base(self, base: u8) -> Int<u8>
pub const fn digits_base(self, base: u8) -> Int<u8>
Returns the number of digits in the given base
.
If the base is 0, it returns 0.
§Examples
assert_eq![Int(2), Int(3_u8).digits_base(2)];
assert_eq![Int(2), Int(127_u8).digits_base(16)];
assert_eq![Int(0), Int(100_u8).digits_base(0)];
assert_eq![Int(1), Int(0_u8).digits_base(100)];
Sourcepub const fn digits_base_sign(self, base: u8) -> Int<u8>
pub const fn digits_base_sign(self, base: u8) -> Int<u8>
An alias of digits_base
.
Sourcepub const fn digital_root(self) -> Int<u8>
pub const fn digital_root(self) -> Int<u8>
Returns the digital root in base 10.
§Examples
assert_eq![Int(1), Int(127_u8).digital_root()];
assert_eq![Int(9), Int(126_u8).digital_root()];
Source§impl Int<u16>
Available on crate feature _int_u16
only.
impl Int<u16>
_int_u16
only.§Integer base related methods for u16
Sourcepub const fn digits(self) -> Int<u16>
pub const fn digits(self) -> Int<u16>
Returns the number of digits in base 10.
§Examples
assert_eq![Int(1), Int(0_u16).digits()];
assert_eq![Int(3), Int(127_u16).digits()];
Sourcepub const fn digits_sign(self) -> Int<u16>
pub const fn digits_sign(self) -> Int<u16>
An alias of digits
.
Sourcepub const fn digits_base(self, base: u16) -> Int<u16>
pub const fn digits_base(self, base: u16) -> Int<u16>
Returns the number of digits in the given base
.
If the base is 0, it returns 0.
§Examples
assert_eq![Int(2), Int(3_u16).digits_base(2)];
assert_eq![Int(2), Int(127_u16).digits_base(16)];
assert_eq![Int(0), Int(100_u16).digits_base(0)];
assert_eq![Int(1), Int(0_u16).digits_base(100)];
Sourcepub const fn digits_base_sign(self, base: u16) -> Int<u16>
pub const fn digits_base_sign(self, base: u16) -> Int<u16>
An alias of digits_base
.
Sourcepub const fn digital_root(self) -> Int<u16>
pub const fn digital_root(self) -> Int<u16>
Returns the digital root in base 10.
§Examples
assert_eq![Int(1), Int(127_u16).digital_root()];
assert_eq![Int(9), Int(126_u16).digital_root()];
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<u64>
Available on crate feature _int_u64
only.
impl Int<u64>
_int_u64
only.§Integer base related methods for u64
Sourcepub const fn digits(self) -> Int<u64>
pub const fn digits(self) -> Int<u64>
Returns the number of digits in base 10.
§Examples
assert_eq![Int(1), Int(0_u64).digits()];
assert_eq![Int(3), Int(127_u64).digits()];
Sourcepub const fn digits_sign(self) -> Int<u64>
pub const fn digits_sign(self) -> Int<u64>
An alias of digits
.
Sourcepub const fn digits_base(self, base: u64) -> Int<u64>
pub const fn digits_base(self, base: u64) -> Int<u64>
Returns the number of digits in the given base
.
If the base is 0, it returns 0.
§Examples
assert_eq![Int(2), Int(3_u64).digits_base(2)];
assert_eq![Int(2), Int(127_u64).digits_base(16)];
assert_eq![Int(0), Int(100_u64).digits_base(0)];
assert_eq![Int(1), Int(0_u64).digits_base(100)];
Sourcepub const fn digits_base_sign(self, base: u64) -> Int<u64>
pub const fn digits_base_sign(self, base: u64) -> Int<u64>
An alias of digits_base
.
Sourcepub const fn digital_root(self) -> Int<u64>
pub const fn digital_root(self) -> Int<u64>
Returns the digital root in base 10.
§Examples
assert_eq![Int(1), Int(127_u64).digital_root()];
assert_eq![Int(9), Int(126_u64).digital_root()];
Source§impl Int<u128>
Available on crate feature _int_u128
only.
impl Int<u128>
_int_u128
only.§Integer base related methods for u128
Sourcepub const fn digits(self) -> Int<u128>
pub const fn digits(self) -> Int<u128>
Returns the number of digits in base 10.
§Examples
assert_eq![Int(1), Int(0_u128).digits()];
assert_eq![Int(3), Int(127_u128).digits()];
Sourcepub const fn digits_sign(self) -> Int<u128>
pub const fn digits_sign(self) -> Int<u128>
An alias of digits
.
Sourcepub const fn digits_base(self, base: u128) -> Int<u128>
pub const fn digits_base(self, base: u128) -> Int<u128>
Returns the number of digits in the given base
.
If the base is 0, it returns 0.
§Examples
assert_eq![Int(2), Int(3_u128).digits_base(2)];
assert_eq![Int(2), Int(127_u128).digits_base(16)];
assert_eq![Int(0), Int(100_u128).digits_base(0)];
assert_eq![Int(1), Int(0_u128).digits_base(100)];
Sourcepub const fn digits_base_sign(self, base: u128) -> Int<u128>
pub const fn digits_base_sign(self, base: u128) -> Int<u128>
An alias of digits_base
.
Sourcepub const fn digital_root(self) -> Int<u128>
pub const fn digital_root(self) -> Int<u128>
Returns the digital root in base 10.
§Examples
assert_eq![Int(1), Int(127_u128).digital_root()];
assert_eq![Int(9), Int(126_u128).digital_root()];
Source§impl Int<usize>
Available on crate feature _int_usize
only.
impl Int<usize>
_int_usize
only.§Integer base related methods for usize
Sourcepub const fn digits(self) -> Int<usize>
pub const fn digits(self) -> Int<usize>
Returns the number of digits in base 10.
§Examples
assert_eq![Int(1), Int(0_usize).digits()];
assert_eq![Int(3), Int(127_usize).digits()];
Sourcepub const fn digits_sign(self) -> Int<usize>
pub const fn digits_sign(self) -> Int<usize>
An alias of digits
.
Sourcepub const fn digits_base(self, base: usize) -> Int<usize>
pub const fn digits_base(self, base: usize) -> Int<usize>
Returns the number of digits in the given base
.
If the base is 0, it returns 0.
§Examples
assert_eq![Int(2), Int(3_usize).digits_base(2)];
assert_eq![Int(2), Int(127_usize).digits_base(16)];
assert_eq![Int(0), Int(100_usize).digits_base(0)];
assert_eq![Int(1), Int(0_usize).digits_base(100)];
Sourcepub const fn digits_base_sign(self, base: usize) -> Int<usize>
pub const fn digits_base_sign(self, base: usize) -> Int<usize>
An alias of digits_base
.
Sourcepub const fn digital_root(self) -> Int<usize>
pub const fn digital_root(self) -> Int<usize>
Returns the digital root in base 10.
§Examples
assert_eq![Int(1), Int(127_usize).digital_root()];
assert_eq![Int(9), Int(126_usize).digital_root()];
Source§impl Int<i8>
Available on crate feature _int_i8
only.
impl Int<i8>
_int_i8
only.§Integer combinatorics related methods for i8
Sourcepub const fn factorial(self) -> Result<Int<i8>>
pub const fn factorial(self) -> Result<Int<i8>>
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_i8).factorial()];
assert_eq![Ok(Int(6)), Int(3_i8).factorial()];
assert_eq![Ok(Int(1)), Int(0_i8).factorial()];
assert![Int(-3_i8).factorial().is_err()];
assert![Int(i8::MAX).factorial().is_err()];
Sourcepub const fn subfactorial(self) -> Result<Int<i8>>
pub const fn subfactorial(self) -> Result<Int<i8>>
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_i8).subfactorial()];
assert_eq![Ok(Int(9)), Int(4_i8).subfactorial()];
assert_eq![Ok(Int(1)), Int(0_i8).subfactorial()];
assert![Int(-3_i8).subfactorial().is_err()];
assert![Int(127_i8).subfactorial().is_err()];
§Links
- The list of subfactorials is available in https://oeis.org/A000166.
Sourcepub const fn combine(self, r: i8) -> Result<Int<i8>>
pub const fn combine(self, r: i8) -> Result<Int<i8>>
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_i8).combine(3)];
assert_eq![Ok(Int(3)), Int(3_i8).combine(2)];
assert_eq![Ok(Int(3)), Int(3_i8).combine(1)];
assert![Int(-3_i8).combine(3).is_err()];
assert![Int(3_i8).combine(-2).is_err()];
Sourcepub const fn combine_rep(self, r: i8) -> Result<Int<i8>>
pub const fn combine_rep(self, r: i8) -> Result<Int<i8>>
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_i8).combine_rep(3)];
assert_eq![Ok(Int(6)), Int(3_i8).combine_rep(2)];
assert_eq![Ok(Int(3)), Int(3_i8).combine_rep(1)];
assert![Int(-3_i8).combine_rep(3).is_err()];
assert![Int(3_i8).combine_rep(-2).is_err()];
Sourcepub const fn permute(self, r: i8) -> Result<Int<i8>>
pub const fn permute(self, r: i8) -> Result<Int<i8>>
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_i8).permute(3)];
assert_eq![Ok(Int(6)), Int(3_i8).permute(2)];
assert_eq![Ok(Int(3)), Int(3_i8).permute(1)];
assert![Int(-3_i8).permute(3).is_err()];
assert![Int(3_i8).permute(-2).is_err()];
Sourcepub const fn permute_rep(self, r: i8) -> Result<Int<i8>>
Available on crate feature cast
only.
pub const fn permute_rep(self, r: i8) -> Result<Int<i8>>
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_i8).permute_rep(3)];
assert_eq![Ok(Int(9)), Int(3_i8).permute_rep(2)];
assert_eq![Ok(Int(3)), Int(3_i8).permute_rep(1)];
assert![Int(-3_i8).permute_rep(3).is_err()];
assert![Int(3_i8).permute_rep(-2).is_err()];
Source§impl Int<i16>
Available on crate feature _int_i16
only.
impl Int<i16>
_int_i16
only.§Integer combinatorics related methods for i16
Sourcepub const fn factorial(self) -> Result<Int<i16>>
pub const fn factorial(self) -> Result<Int<i16>>
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_i16).factorial()];
assert_eq![Ok(Int(6)), Int(3_i16).factorial()];
assert_eq![Ok(Int(1)), Int(0_i16).factorial()];
assert![Int(-3_i16).factorial().is_err()];
assert![Int(i16::MAX).factorial().is_err()];
Sourcepub const fn subfactorial(self) -> Result<Int<i16>>
pub const fn subfactorial(self) -> Result<Int<i16>>
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_i16).subfactorial()];
assert_eq![Ok(Int(9)), Int(4_i16).subfactorial()];
assert_eq![Ok(Int(1)), Int(0_i16).subfactorial()];
assert![Int(-3_i16).subfactorial().is_err()];
assert![Int(127_i16).subfactorial().is_err()];
§Links
- The list of subfactorials is available in https://oeis.org/A000166.
Sourcepub const fn combine(self, r: i16) -> Result<Int<i16>>
pub const fn combine(self, r: i16) -> Result<Int<i16>>
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_i16).combine(3)];
assert_eq![Ok(Int(3)), Int(3_i16).combine(2)];
assert_eq![Ok(Int(3)), Int(3_i16).combine(1)];
assert![Int(-3_i16).combine(3).is_err()];
assert![Int(3_i16).combine(-2).is_err()];
Sourcepub const fn combine_rep(self, r: i16) -> Result<Int<i16>>
pub const fn combine_rep(self, r: i16) -> Result<Int<i16>>
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_i16).combine_rep(3)];
assert_eq![Ok(Int(6)), Int(3_i16).combine_rep(2)];
assert_eq![Ok(Int(3)), Int(3_i16).combine_rep(1)];
assert![Int(-3_i16).combine_rep(3).is_err()];
assert![Int(3_i16).combine_rep(-2).is_err()];
Sourcepub const fn permute(self, r: i16) -> Result<Int<i16>>
pub const fn permute(self, r: i16) -> Result<Int<i16>>
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_i16).permute(3)];
assert_eq![Ok(Int(6)), Int(3_i16).permute(2)];
assert_eq![Ok(Int(3)), Int(3_i16).permute(1)];
assert![Int(-3_i16).permute(3).is_err()];
assert![Int(3_i16).permute(-2).is_err()];
Sourcepub const fn permute_rep(self, r: i16) -> Result<Int<i16>>
Available on crate feature cast
only.
pub const fn permute_rep(self, r: i16) -> Result<Int<i16>>
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_i16).permute_rep(3)];
assert_eq![Ok(Int(9)), Int(3_i16).permute_rep(2)];
assert_eq![Ok(Int(3)), Int(3_i16).permute_rep(1)];
assert![Int(-3_i16).permute_rep(3).is_err()];
assert![Int(3_i16).permute_rep(-2).is_err()];
Source§impl Int<i32>
Available on crate feature _int_i32
only.
impl Int<i32>
_int_i32
only.§Integer combinatorics related methods for i32
Sourcepub const fn factorial(self) -> Result<Int<i32>>
pub const fn factorial(self) -> Result<Int<i32>>
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_i32).factorial()];
assert_eq![Ok(Int(6)), Int(3_i32).factorial()];
assert_eq![Ok(Int(1)), Int(0_i32).factorial()];
assert![Int(-3_i32).factorial().is_err()];
assert![Int(i32::MAX).factorial().is_err()];
Sourcepub const fn subfactorial(self) -> Result<Int<i32>>
pub const fn subfactorial(self) -> Result<Int<i32>>
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_i32).subfactorial()];
assert_eq![Ok(Int(9)), Int(4_i32).subfactorial()];
assert_eq![Ok(Int(1)), Int(0_i32).subfactorial()];
assert![Int(-3_i32).subfactorial().is_err()];
assert![Int(127_i32).subfactorial().is_err()];
§Links
- The list of subfactorials is available in https://oeis.org/A000166.
Sourcepub const fn combine(self, r: i32) -> Result<Int<i32>>
pub const fn combine(self, r: i32) -> Result<Int<i32>>
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_i32).combine(3)];
assert_eq![Ok(Int(3)), Int(3_i32).combine(2)];
assert_eq![Ok(Int(3)), Int(3_i32).combine(1)];
assert![Int(-3_i32).combine(3).is_err()];
assert![Int(3_i32).combine(-2).is_err()];
Sourcepub const fn combine_rep(self, r: i32) -> Result<Int<i32>>
pub const fn combine_rep(self, r: i32) -> Result<Int<i32>>
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_i32).combine_rep(3)];
assert_eq![Ok(Int(6)), Int(3_i32).combine_rep(2)];
assert_eq![Ok(Int(3)), Int(3_i32).combine_rep(1)];
assert![Int(-3_i32).combine_rep(3).is_err()];
assert![Int(3_i32).combine_rep(-2).is_err()];
Sourcepub const fn permute(self, r: i32) -> Result<Int<i32>>
pub const fn permute(self, r: i32) -> Result<Int<i32>>
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_i32).permute(3)];
assert_eq![Ok(Int(6)), Int(3_i32).permute(2)];
assert_eq![Ok(Int(3)), Int(3_i32).permute(1)];
assert![Int(-3_i32).permute(3).is_err()];
assert![Int(3_i32).permute(-2).is_err()];
Sourcepub const fn permute_rep(self, r: i32) -> Result<Int<i32>>
Available on crate feature cast
only.
pub const fn permute_rep(self, r: i32) -> Result<Int<i32>>
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_i32).permute_rep(3)];
assert_eq![Ok(Int(9)), Int(3_i32).permute_rep(2)];
assert_eq![Ok(Int(3)), Int(3_i32).permute_rep(1)];
assert![Int(-3_i32).permute_rep(3).is_err()];
assert![Int(3_i32).permute_rep(-2).is_err()];
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<i128>
Available on crate feature _int_i128
only.
impl Int<i128>
_int_i128
only.§Integer combinatorics related methods for i128
Sourcepub const fn factorial(self) -> Result<Int<i128>>
pub const fn factorial(self) -> Result<Int<i128>>
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_i128).factorial()];
assert_eq![Ok(Int(6)), Int(3_i128).factorial()];
assert_eq![Ok(Int(1)), Int(0_i128).factorial()];
assert![Int(-3_i128).factorial().is_err()];
assert![Int(i128::MAX).factorial().is_err()];
Sourcepub const fn subfactorial(self) -> Result<Int<i128>>
pub const fn subfactorial(self) -> Result<Int<i128>>
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_i128).subfactorial()];
assert_eq![Ok(Int(9)), Int(4_i128).subfactorial()];
assert_eq![Ok(Int(1)), Int(0_i128).subfactorial()];
assert![Int(-3_i128).subfactorial().is_err()];
assert![Int(127_i128).subfactorial().is_err()];
§Links
- The list of subfactorials is available in https://oeis.org/A000166.
Sourcepub const fn combine(self, r: i128) -> Result<Int<i128>>
pub const fn combine(self, r: i128) -> Result<Int<i128>>
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_i128).combine(3)];
assert_eq![Ok(Int(3)), Int(3_i128).combine(2)];
assert_eq![Ok(Int(3)), Int(3_i128).combine(1)];
assert![Int(-3_i128).combine(3).is_err()];
assert![Int(3_i128).combine(-2).is_err()];
Sourcepub const fn combine_rep(self, r: i128) -> Result<Int<i128>>
pub const fn combine_rep(self, r: i128) -> Result<Int<i128>>
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_i128).combine_rep(3)];
assert_eq![Ok(Int(6)), Int(3_i128).combine_rep(2)];
assert_eq![Ok(Int(3)), Int(3_i128).combine_rep(1)];
assert![Int(-3_i128).combine_rep(3).is_err()];
assert![Int(3_i128).combine_rep(-2).is_err()];
Sourcepub const fn permute(self, r: i128) -> Result<Int<i128>>
pub const fn permute(self, r: i128) -> Result<Int<i128>>
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_i128).permute(3)];
assert_eq![Ok(Int(6)), Int(3_i128).permute(2)];
assert_eq![Ok(Int(3)), Int(3_i128).permute(1)];
assert![Int(-3_i128).permute(3).is_err()];
assert![Int(3_i128).permute(-2).is_err()];
Sourcepub const fn permute_rep(self, r: i128) -> Result<Int<i128>>
Available on crate feature cast
only.
pub const fn permute_rep(self, r: i128) -> Result<Int<i128>>
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_i128).permute_rep(3)];
assert_eq![Ok(Int(9)), Int(3_i128).permute_rep(2)];
assert_eq![Ok(Int(3)), Int(3_i128).permute_rep(1)];
assert![Int(-3_i128).permute_rep(3).is_err()];
assert![Int(3_i128).permute_rep(-2).is_err()];
Source§impl Int<isize>
Available on crate feature _int_isize
only.
impl Int<isize>
_int_isize
only.§Integer combinatorics related methods for isize
Sourcepub const fn factorial(self) -> Result<Int<isize>>
pub const fn factorial(self) -> Result<Int<isize>>
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_isize).factorial()];
assert_eq![Ok(Int(6)), Int(3_isize).factorial()];
assert_eq![Ok(Int(1)), Int(0_isize).factorial()];
assert![Int(-3_isize).factorial().is_err()];
assert![Int(isize::MAX).factorial().is_err()];
Sourcepub const fn subfactorial(self) -> Result<Int<isize>>
pub const fn subfactorial(self) -> Result<Int<isize>>
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_isize).subfactorial()];
assert_eq![Ok(Int(9)), Int(4_isize).subfactorial()];
assert_eq![Ok(Int(1)), Int(0_isize).subfactorial()];
assert![Int(-3_isize).subfactorial().is_err()];
assert![Int(127_isize).subfactorial().is_err()];
§Links
- The list of subfactorials is available in https://oeis.org/A000166.
Sourcepub const fn combine(self, r: isize) -> Result<Int<isize>>
pub const fn combine(self, r: isize) -> Result<Int<isize>>
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_isize).combine(3)];
assert_eq![Ok(Int(3)), Int(3_isize).combine(2)];
assert_eq![Ok(Int(3)), Int(3_isize).combine(1)];
assert![Int(-3_isize).combine(3).is_err()];
assert![Int(3_isize).combine(-2).is_err()];
Sourcepub const fn combine_rep(self, r: isize) -> Result<Int<isize>>
pub const fn combine_rep(self, r: isize) -> Result<Int<isize>>
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_isize).combine_rep(3)];
assert_eq![Ok(Int(6)), Int(3_isize).combine_rep(2)];
assert_eq![Ok(Int(3)), Int(3_isize).combine_rep(1)];
assert![Int(-3_isize).combine_rep(3).is_err()];
assert![Int(3_isize).combine_rep(-2).is_err()];
Sourcepub const fn permute(self, r: isize) -> Result<Int<isize>>
pub const fn permute(self, r: isize) -> Result<Int<isize>>
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_isize).permute(3)];
assert_eq![Ok(Int(6)), Int(3_isize).permute(2)];
assert_eq![Ok(Int(3)), Int(3_isize).permute(1)];
assert![Int(-3_isize).permute(3).is_err()];
assert![Int(3_isize).permute(-2).is_err()];
Sourcepub const fn permute_rep(self, r: isize) -> Result<Int<isize>>
Available on crate feature cast
only.
pub const fn permute_rep(self, r: isize) -> Result<Int<isize>>
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_isize).permute_rep(3)];
assert_eq![Ok(Int(9)), Int(3_isize).permute_rep(2)];
assert_eq![Ok(Int(3)), Int(3_isize).permute_rep(1)];
assert![Int(-3_isize).permute_rep(3).is_err()];
assert![Int(3_isize).permute_rep(-2).is_err()];
Source§impl Int<u8>
Available on crate feature _int_u8
only.
impl Int<u8>
_int_u8
only.§Integer combinatorics related methods for u8
Sourcepub const fn factorial(self) -> Result<Int<u8>>
pub const fn factorial(self) -> Result<Int<u8>>
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_u8).factorial()];
assert_eq![Ok(Int(6)), Int(3_u8).factorial()];
assert_eq![Ok(Int(1)), Int(0_u8).factorial()];
assert![Int(u8::MAX).factorial().is_err()];
Sourcepub const fn subfactorial(self) -> Result<Int<u8>>
pub const fn subfactorial(self) -> Result<Int<u8>>
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_u8).subfactorial()];
assert_eq![Ok(Int(9)), Int(4_u8).subfactorial()];
assert_eq![Ok(Int(1)), Int(0_u8).subfactorial()];
assert![Int(255_u8).subfactorial().is_err()];
§Links
- The list of subfactorials is available in https://oeis.org/A000166.
Sourcepub const fn combine(self, r: u8) -> Result<Int<u8>>
pub const fn combine(self, r: u8) -> Result<Int<u8>>
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_u8).combine(3)];
assert_eq![Ok(Int(3)), Int(3_u8).combine(2)];
assert_eq![Ok(Int(3)), Int(3_u8).combine(1)];
assert![Int(u8::MAX).combine(u8::MAX).is_err()];
Sourcepub const fn combine_rep(self, r: u8) -> Result<Int<u8>>
pub const fn combine_rep(self, r: u8) -> Result<Int<u8>>
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_u8).combine_rep(3)];
assert_eq![Ok(Int(6)), Int(3_u8).combine_rep(2)];
assert_eq![Ok(Int(3)), Int(3_u8).combine_rep(1)];
assert![Int(u8::MAX).combine_rep(u8::MAX).is_err()];
Sourcepub const fn permute(self, r: u8) -> Result<Int<u8>>
pub const fn permute(self, r: u8) -> Result<Int<u8>>
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_u8).permute(3)];
assert_eq![Ok(Int(6)), Int(3_u8).permute(2)];
assert_eq![Ok(Int(3)), Int(3_u8).permute(1)];
assert![Int(3_u8).permute(4_u8).is_err()];
assert![Int(u8::MAX).permute(u8::MAX).is_err()];
Sourcepub const fn permute_rep(self, r: u8) -> Result<Int<u8>>
Available on crate feature cast
only.
pub const fn permute_rep(self, r: u8) -> Result<Int<u8>>
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_u8).permute_rep(3)];
assert_eq![Ok(Int(9)), Int(3_u8).permute_rep(2)];
assert_eq![Ok(Int(3)), Int(3_u8).permute_rep(1)];
assert![Int(u8::MAX).permute_rep(u8::MAX).is_err()];
Source§impl Int<u16>
Available on crate feature _int_u16
only.
impl Int<u16>
_int_u16
only.§Integer combinatorics related methods for u16
Sourcepub const fn factorial(self) -> Result<Int<u16>>
pub const fn factorial(self) -> Result<Int<u16>>
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_u16).factorial()];
assert_eq![Ok(Int(6)), Int(3_u16).factorial()];
assert_eq![Ok(Int(1)), Int(0_u16).factorial()];
assert![Int(u16::MAX).factorial().is_err()];
Sourcepub const fn subfactorial(self) -> Result<Int<u16>>
pub const fn subfactorial(self) -> Result<Int<u16>>
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_u16).subfactorial()];
assert_eq![Ok(Int(9)), Int(4_u16).subfactorial()];
assert_eq![Ok(Int(1)), Int(0_u16).subfactorial()];
assert![Int(255_u16).subfactorial().is_err()];
§Links
- The list of subfactorials is available in https://oeis.org/A000166.
Sourcepub const fn combine(self, r: u16) -> Result<Int<u16>>
pub const fn combine(self, r: u16) -> Result<Int<u16>>
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_u16).combine(3)];
assert_eq![Ok(Int(3)), Int(3_u16).combine(2)];
assert_eq![Ok(Int(3)), Int(3_u16).combine(1)];
assert![Int(u16::MAX).combine(u16::MAX).is_err()];
Sourcepub const fn combine_rep(self, r: u16) -> Result<Int<u16>>
pub const fn combine_rep(self, r: u16) -> Result<Int<u16>>
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_u16).combine_rep(3)];
assert_eq![Ok(Int(6)), Int(3_u16).combine_rep(2)];
assert_eq![Ok(Int(3)), Int(3_u16).combine_rep(1)];
assert![Int(u16::MAX).combine_rep(u16::MAX).is_err()];
Sourcepub const fn permute(self, r: u16) -> Result<Int<u16>>
pub const fn permute(self, r: u16) -> Result<Int<u16>>
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_u16).permute(3)];
assert_eq![Ok(Int(6)), Int(3_u16).permute(2)];
assert_eq![Ok(Int(3)), Int(3_u16).permute(1)];
assert![Int(3_u16).permute(4_u16).is_err()];
assert![Int(u16::MAX).permute(u16::MAX).is_err()];
Sourcepub const fn permute_rep(self, r: u16) -> Result<Int<u16>>
Available on crate feature cast
only.
pub const fn permute_rep(self, r: u16) -> Result<Int<u16>>
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_u16).permute_rep(3)];
assert_eq![Ok(Int(9)), Int(3_u16).permute_rep(2)];
assert_eq![Ok(Int(3)), Int(3_u16).permute_rep(1)];
assert![Int(u16::MAX).permute_rep(u16::MAX).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<u64>
Available on crate feature _int_u64
only.
impl Int<u64>
_int_u64
only.§Integer combinatorics related methods for u64
Sourcepub const fn factorial(self) -> Result<Int<u64>>
pub const fn factorial(self) -> Result<Int<u64>>
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_u64).factorial()];
assert_eq![Ok(Int(6)), Int(3_u64).factorial()];
assert_eq![Ok(Int(1)), Int(0_u64).factorial()];
assert![Int(u64::MAX).factorial().is_err()];
Sourcepub const fn subfactorial(self) -> Result<Int<u64>>
pub const fn subfactorial(self) -> Result<Int<u64>>
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_u64).subfactorial()];
assert_eq![Ok(Int(9)), Int(4_u64).subfactorial()];
assert_eq![Ok(Int(1)), Int(0_u64).subfactorial()];
assert![Int(255_u64).subfactorial().is_err()];
§Links
- The list of subfactorials is available in https://oeis.org/A000166.
Sourcepub const fn combine(self, r: u64) -> Result<Int<u64>>
pub const fn combine(self, r: u64) -> Result<Int<u64>>
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_u64).combine(3)];
assert_eq![Ok(Int(3)), Int(3_u64).combine(2)];
assert_eq![Ok(Int(3)), Int(3_u64).combine(1)];
assert![Int(u64::MAX).combine(u64::MAX).is_err()];
Sourcepub const fn combine_rep(self, r: u64) -> Result<Int<u64>>
pub const fn combine_rep(self, r: u64) -> Result<Int<u64>>
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_u64).combine_rep(3)];
assert_eq![Ok(Int(6)), Int(3_u64).combine_rep(2)];
assert_eq![Ok(Int(3)), Int(3_u64).combine_rep(1)];
assert![Int(u64::MAX).combine_rep(u64::MAX).is_err()];
Sourcepub const fn permute(self, r: u64) -> Result<Int<u64>>
pub const fn permute(self, r: u64) -> Result<Int<u64>>
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_u64).permute(3)];
assert_eq![Ok(Int(6)), Int(3_u64).permute(2)];
assert_eq![Ok(Int(3)), Int(3_u64).permute(1)];
assert![Int(3_u64).permute(4_u64).is_err()];
assert![Int(u64::MAX).permute(u64::MAX).is_err()];
Sourcepub const fn permute_rep(self, r: u64) -> Result<Int<u64>>
Available on crate feature cast
only.
pub const fn permute_rep(self, r: u64) -> Result<Int<u64>>
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_u64).permute_rep(3)];
assert_eq![Ok(Int(9)), Int(3_u64).permute_rep(2)];
assert_eq![Ok(Int(3)), Int(3_u64).permute_rep(1)];
assert![Int(u64::MAX).permute_rep(u64::MAX).is_err()];
Source§impl Int<u128>
Available on crate feature _int_u128
only.
impl Int<u128>
_int_u128
only.§Integer combinatorics related methods for u128
Sourcepub const fn factorial(self) -> Result<Int<u128>>
pub const fn factorial(self) -> Result<Int<u128>>
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_u128).factorial()];
assert_eq![Ok(Int(6)), Int(3_u128).factorial()];
assert_eq![Ok(Int(1)), Int(0_u128).factorial()];
assert![Int(u128::MAX).factorial().is_err()];
Sourcepub const fn subfactorial(self) -> Result<Int<u128>>
pub const fn subfactorial(self) -> Result<Int<u128>>
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_u128).subfactorial()];
assert_eq![Ok(Int(9)), Int(4_u128).subfactorial()];
assert_eq![Ok(Int(1)), Int(0_u128).subfactorial()];
assert![Int(255_u128).subfactorial().is_err()];
§Links
- The list of subfactorials is available in https://oeis.org/A000166.
Sourcepub const fn combine(self, r: u128) -> Result<Int<u128>>
pub const fn combine(self, r: u128) -> Result<Int<u128>>
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_u128).combine(3)];
assert_eq![Ok(Int(3)), Int(3_u128).combine(2)];
assert_eq![Ok(Int(3)), Int(3_u128).combine(1)];
assert![Int(u128::MAX).combine(u128::MAX).is_err()];
Sourcepub const fn combine_rep(self, r: u128) -> Result<Int<u128>>
pub const fn combine_rep(self, r: u128) -> Result<Int<u128>>
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_u128).combine_rep(3)];
assert_eq![Ok(Int(6)), Int(3_u128).combine_rep(2)];
assert_eq![Ok(Int(3)), Int(3_u128).combine_rep(1)];
assert![Int(u128::MAX).combine_rep(u128::MAX).is_err()];
Sourcepub const fn permute(self, r: u128) -> Result<Int<u128>>
pub const fn permute(self, r: u128) -> Result<Int<u128>>
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_u128).permute(3)];
assert_eq![Ok(Int(6)), Int(3_u128).permute(2)];
assert_eq![Ok(Int(3)), Int(3_u128).permute(1)];
assert![Int(3_u128).permute(4_u128).is_err()];
assert![Int(u128::MAX).permute(u128::MAX).is_err()];
Sourcepub const fn permute_rep(self, r: u128) -> Result<Int<u128>>
Available on crate feature cast
only.
pub const fn permute_rep(self, r: u128) -> Result<Int<u128>>
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_u128).permute_rep(3)];
assert_eq![Ok(Int(9)), Int(3_u128).permute_rep(2)];
assert_eq![Ok(Int(3)), Int(3_u128).permute_rep(1)];
assert![Int(u128::MAX).permute_rep(u128::MAX).is_err()];
Source§impl Int<usize>
Available on crate feature _int_usize
only.
impl Int<usize>
_int_usize
only.§Integer combinatorics related methods for usize
Sourcepub const fn factorial(self) -> Result<Int<usize>>
pub const fn factorial(self) -> Result<Int<usize>>
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_usize).factorial()];
assert_eq![Ok(Int(6)), Int(3_usize).factorial()];
assert_eq![Ok(Int(1)), Int(0_usize).factorial()];
assert![Int(usize::MAX).factorial().is_err()];
Sourcepub const fn subfactorial(self) -> Result<Int<usize>>
pub const fn subfactorial(self) -> Result<Int<usize>>
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_usize).subfactorial()];
assert_eq![Ok(Int(9)), Int(4_usize).subfactorial()];
assert_eq![Ok(Int(1)), Int(0_usize).subfactorial()];
assert![Int(255_usize).subfactorial().is_err()];
§Links
- The list of subfactorials is available in https://oeis.org/A000166.
Sourcepub const fn combine(self, r: usize) -> Result<Int<usize>>
pub const fn combine(self, r: usize) -> Result<Int<usize>>
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_usize).combine(3)];
assert_eq![Ok(Int(3)), Int(3_usize).combine(2)];
assert_eq![Ok(Int(3)), Int(3_usize).combine(1)];
assert![Int(usize::MAX).combine(usize::MAX).is_err()];
Sourcepub const fn combine_rep(self, r: usize) -> Result<Int<usize>>
pub const fn combine_rep(self, r: usize) -> Result<Int<usize>>
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_usize).combine_rep(3)];
assert_eq![Ok(Int(6)), Int(3_usize).combine_rep(2)];
assert_eq![Ok(Int(3)), Int(3_usize).combine_rep(1)];
assert![Int(usize::MAX).combine_rep(usize::MAX).is_err()];
Sourcepub const fn permute(self, r: usize) -> Result<Int<usize>>
pub const fn permute(self, r: usize) -> Result<Int<usize>>
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_usize).permute(3)];
assert_eq![Ok(Int(6)), Int(3_usize).permute(2)];
assert_eq![Ok(Int(3)), Int(3_usize).permute(1)];
assert![Int(3_usize).permute(4_usize).is_err()];
assert![Int(usize::MAX).permute(usize::MAX).is_err()];
Sourcepub const fn permute_rep(self, r: usize) -> Result<Int<usize>>
Available on crate feature cast
only.
pub const fn permute_rep(self, r: usize) -> Result<Int<usize>>
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_usize).permute_rep(3)];
assert_eq![Ok(Int(9)), Int(3_usize).permute_rep(2)];
assert_eq![Ok(Int(3)), Int(3_usize).permute_rep(1)];
assert![Int(usize::MAX).permute_rep(usize::MAX).is_err()];
Source§impl Int<i8>
Available on crate feature _int_i8
only.
impl Int<i8>
_int_i8
only.§Integer core methods for i8
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_i8).is_even()];
assert![Int(-2_i8).is_even()];
assert![!Int(3_i8).is_even()];
assert![Int(0_i8).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_i8).is_odd()];
assert![Int(-3_i8).is_odd()];
assert![!Int(2_i8).is_odd()];
assert![!Int(0_i8).is_odd()];
Sourcepub const fn gcd(self, b: i8) -> Int<i8>
pub const fn gcd(self, b: i8) -> Int<i8>
Returns the GCD.
Uses Stein’s algorithm which is much more efficient to compute than Euclid’s.
§Examples
assert_eq![Int(4), Int(64_i8).gcd(36)];
assert_eq![Int(4), Int(-64_i8).gcd(36)];
assert_eq![Int(4), Int(64_i8).gcd(-36)];
assert_eq![Int(4), Int(-64_i8).gcd(-36)];
assert_eq![Int(36), Int(0_i8).gcd(36)];
assert_eq![Int(64), Int(64_i8).gcd(0)];
Sourcepub const fn gcd_ext(self, b: i8) -> GcdReturn<Int<i8>, Int<i8>>
pub const fn gcd_ext(self, b: i8) -> GcdReturn<Int<i8>, Int<i8>>
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_i8).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: i8) -> GcdReturn<Int<i8>, Int<i8>>
pub const fn gcd_ext_euc(self, b: i8) -> GcdReturn<Int<i8>, Int<i8>>
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_i8).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: i8, max: i8, a: i8, b: i8) -> Result<Int<i8>>
Available on crate feature cast
only.
pub const fn scale(self, min: i8, max: i8, a: i8, b: i8) -> Result<Int<i8>>
cast
only.Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as i16
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_i8).scale(0, 120, 30, 50)]; // interpolate
assert_eq![Ok(Int(112)), Int(100_i8).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: i8, max: i8, a: i8, b: i8) -> Int<i8>
pub const fn scale_wrap(self, min: i8, max: i8, a: i8, b: i8) -> Int<i8>
Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as i16
.
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_i8).scale_wrap(0, 120, 30, 50)]; // interpolate
assert_eq![Int(112), Int(100_i8).scale_wrap(0, 80, 0, 90)]; // extrapolate
assert_eq![Int(-76), Int(100_i8).scale_wrap(0, 50, 0, 90)]; // extrapolate and wrap
Sourcepub const fn midpoint(self, other: i8) -> Int<i8>
Available on crate feature _int_u8
only.
pub const fn midpoint(self, other: i8) -> Int<i8>
_int_u8
only.Calculates the middle point of self
and other
.
The result is always rounded towards negative infinity.
§Examples
assert_eq![Int(0_i8).midpoint(4), 2];
assert_eq![Int(0_i8).midpoint(-1), -1];
assert_eq![Int(-1_i8).midpoint(0), -1];
Source§impl Int<i16>
Available on crate feature _int_i16
only.
impl Int<i16>
_int_i16
only.§Integer core methods for i16
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_i16).is_even()];
assert![Int(-2_i16).is_even()];
assert![!Int(3_i16).is_even()];
assert![Int(0_i16).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_i16).is_odd()];
assert![Int(-3_i16).is_odd()];
assert![!Int(2_i16).is_odd()];
assert![!Int(0_i16).is_odd()];
Sourcepub const fn gcd(self, b: i16) -> Int<i16>
pub const fn gcd(self, b: i16) -> Int<i16>
Returns the GCD.
Uses Stein’s algorithm which is much more efficient to compute than Euclid’s.
§Examples
assert_eq![Int(4), Int(64_i16).gcd(36)];
assert_eq![Int(4), Int(-64_i16).gcd(36)];
assert_eq![Int(4), Int(64_i16).gcd(-36)];
assert_eq![Int(4), Int(-64_i16).gcd(-36)];
assert_eq![Int(36), Int(0_i16).gcd(36)];
assert_eq![Int(64), Int(64_i16).gcd(0)];
Sourcepub const fn gcd_ext(self, b: i16) -> GcdReturn<Int<i16>, Int<i16>>
pub const fn gcd_ext(self, b: i16) -> GcdReturn<Int<i16>, Int<i16>>
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_i16).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: i16) -> GcdReturn<Int<i16>, Int<i16>>
pub const fn gcd_ext_euc(self, b: i16) -> GcdReturn<Int<i16>, Int<i16>>
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_i16).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: i16, max: i16, a: i16, b: i16) -> Result<Int<i16>>
Available on crate feature cast
only.
pub const fn scale(self, min: i16, max: i16, a: i16, b: i16) -> Result<Int<i16>>
cast
only.Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as i32
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_i16).scale(0, 120, 30, 50)]; // interpolate
assert_eq![Ok(Int(112)), Int(100_i16).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: i16, max: i16, a: i16, b: i16) -> Int<i16>
pub const fn scale_wrap(self, min: i16, max: i16, a: i16, b: i16) -> Int<i16>
Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as i32
.
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_i16).scale_wrap(0, 120, 30, 50)]; // interpolate
assert_eq![Int(112), Int(100_i16).scale_wrap(0, 80, 0, 90)]; // extrapolate
assert_eq![Int(-76), Int(100_i8).scale_wrap(0, 50, 0, 90)]; // extrapolate and wrap
Sourcepub const fn midpoint(self, other: i16) -> Int<i16>
Available on crate feature _int_u16
only.
pub const fn midpoint(self, other: i16) -> Int<i16>
_int_u16
only.Calculates the middle point of self
and other
.
The result is always rounded towards negative infinity.
§Examples
assert_eq![Int(0_i16).midpoint(4), 2];
assert_eq![Int(0_i16).midpoint(-1), -1];
assert_eq![Int(-1_i16).midpoint(0), -1];
Source§impl Int<i32>
Available on crate feature _int_i32
only.
impl Int<i32>
_int_i32
only.§Integer core methods for i32
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_i32).is_even()];
assert![Int(-2_i32).is_even()];
assert![!Int(3_i32).is_even()];
assert![Int(0_i32).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_i32).is_odd()];
assert![Int(-3_i32).is_odd()];
assert![!Int(2_i32).is_odd()];
assert![!Int(0_i32).is_odd()];
Sourcepub const fn gcd(self, b: i32) -> Int<i32>
pub const fn gcd(self, b: i32) -> Int<i32>
Returns the GCD.
Uses Stein’s algorithm which is much more efficient to compute than Euclid’s.
§Examples
assert_eq![Int(4), Int(64_i32).gcd(36)];
assert_eq![Int(4), Int(-64_i32).gcd(36)];
assert_eq![Int(4), Int(64_i32).gcd(-36)];
assert_eq![Int(4), Int(-64_i32).gcd(-36)];
assert_eq![Int(36), Int(0_i32).gcd(36)];
assert_eq![Int(64), Int(64_i32).gcd(0)];
Sourcepub const fn gcd_ext(self, b: i32) -> GcdReturn<Int<i32>, Int<i32>>
pub const fn gcd_ext(self, b: i32) -> GcdReturn<Int<i32>, Int<i32>>
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_i32).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: i32) -> GcdReturn<Int<i32>, Int<i32>>
pub const fn gcd_ext_euc(self, b: i32) -> GcdReturn<Int<i32>, Int<i32>>
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_i32).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: i32, max: i32, a: i32, b: i32) -> Result<Int<i32>>
Available on crate feature cast
only.
pub const fn scale(self, min: i32, max: i32, a: i32, b: i32) -> Result<Int<i32>>
cast
only.Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as i64
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_i32).scale(0, 120, 30, 50)]; // interpolate
assert_eq![Ok(Int(112)), Int(100_i32).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: i32, max: i32, a: i32, b: i32) -> Int<i32>
pub const fn scale_wrap(self, min: i32, max: i32, a: i32, b: i32) -> Int<i32>
Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as i64
.
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_i32).scale_wrap(0, 120, 30, 50)]; // interpolate
assert_eq![Int(112), Int(100_i32).scale_wrap(0, 80, 0, 90)]; // extrapolate
assert_eq![Int(-76), Int(100_i8).scale_wrap(0, 50, 0, 90)]; // extrapolate and wrap
Sourcepub const fn midpoint(self, other: i32) -> Int<i32>
Available on crate feature _int_u32
only.
pub const fn midpoint(self, other: i32) -> Int<i32>
_int_u32
only.Calculates the middle point of self
and other
.
The result is always rounded towards negative infinity.
§Examples
assert_eq![Int(0_i32).midpoint(4), 2];
assert_eq![Int(0_i32).midpoint(-1), -1];
assert_eq![Int(-1_i32).midpoint(0), -1];
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
Sourcepub const fn midpoint(self, other: i64) -> Int<i64>
Available on crate feature _int_u64
only.
pub const fn midpoint(self, other: i64) -> Int<i64>
_int_u64
only.Calculates the middle point of self
and other
.
The result is always rounded towards negative infinity.
§Examples
assert_eq![Int(0_i64).midpoint(4), 2];
assert_eq![Int(0_i64).midpoint(-1), -1];
assert_eq![Int(-1_i64).midpoint(0), -1];
Source§impl Int<i128>
Available on crate feature _int_i128
only.
impl Int<i128>
_int_i128
only.§Integer core methods for i128
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_i128).is_even()];
assert![Int(-2_i128).is_even()];
assert![!Int(3_i128).is_even()];
assert![Int(0_i128).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_i128).is_odd()];
assert![Int(-3_i128).is_odd()];
assert![!Int(2_i128).is_odd()];
assert![!Int(0_i128).is_odd()];
Sourcepub const fn gcd(self, b: i128) -> Int<i128>
pub const fn gcd(self, b: i128) -> Int<i128>
Returns the GCD.
Uses Stein’s algorithm which is much more efficient to compute than Euclid’s.
§Examples
assert_eq![Int(4), Int(64_i128).gcd(36)];
assert_eq![Int(4), Int(-64_i128).gcd(36)];
assert_eq![Int(4), Int(64_i128).gcd(-36)];
assert_eq![Int(4), Int(-64_i128).gcd(-36)];
assert_eq![Int(36), Int(0_i128).gcd(36)];
assert_eq![Int(64), Int(64_i128).gcd(0)];
Sourcepub const fn gcd_ext(self, b: i128) -> GcdReturn<Int<i128>, Int<i128>>
pub const fn gcd_ext(self, b: i128) -> GcdReturn<Int<i128>, Int<i128>>
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_i128).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: i128) -> GcdReturn<Int<i128>, Int<i128>>
pub const fn gcd_ext_euc(self, b: i128) -> GcdReturn<Int<i128>, Int<i128>>
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_i128).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: i128,
max: i128,
a: i128,
b: i128,
) -> Result<Int<i128>>
Available on crate feature cast
only.
pub const fn scale( self, min: i128, max: i128, a: i128, b: i128, ) -> Result<Int<i128>>
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_i128).scale(0, 120, 30, 50)]; // interpolate
assert_eq![Ok(Int(112)), Int(100_i128).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: i128,
max: i128,
a: i128,
b: i128,
) -> Int<i128>
pub const fn scale_wrap( self, min: i128, max: i128, a: i128, b: i128, ) -> Int<i128>
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_i128).scale_wrap(0, 120, 30, 50)]; // interpolate
assert_eq![Int(112), Int(100_i128).scale_wrap(0, 80, 0, 90)]; // extrapolate
assert_eq![Int(-76), Int(100_i8).scale_wrap(0, 50, 0, 90)]; // extrapolate and wrap
Sourcepub const fn midpoint(self, other: i128) -> Int<i128>
Available on crate feature _int_u128
only.
pub const fn midpoint(self, other: i128) -> Int<i128>
_int_u128
only.Calculates the middle point of self
and other
.
The result is always rounded towards negative infinity.
§Examples
assert_eq![Int(0_i128).midpoint(4), 2];
assert_eq![Int(0_i128).midpoint(-1), -1];
assert_eq![Int(-1_i128).midpoint(0), -1];
Source§impl Int<isize>
Available on crate feature _int_isize
only.
impl Int<isize>
_int_isize
only.§Integer core methods for isize
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_isize).is_even()];
assert![Int(-2_isize).is_even()];
assert![!Int(3_isize).is_even()];
assert![Int(0_isize).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_isize).is_odd()];
assert![Int(-3_isize).is_odd()];
assert![!Int(2_isize).is_odd()];
assert![!Int(0_isize).is_odd()];
Sourcepub const fn gcd(self, b: isize) -> Int<isize>
pub const fn gcd(self, b: isize) -> Int<isize>
Returns the GCD.
Uses Stein’s algorithm which is much more efficient to compute than Euclid’s.
§Examples
assert_eq![Int(4), Int(64_isize).gcd(36)];
assert_eq![Int(4), Int(-64_isize).gcd(36)];
assert_eq![Int(4), Int(64_isize).gcd(-36)];
assert_eq![Int(4), Int(-64_isize).gcd(-36)];
assert_eq![Int(36), Int(0_isize).gcd(36)];
assert_eq![Int(64), Int(64_isize).gcd(0)];
Sourcepub const fn gcd_ext(self, b: isize) -> GcdReturn<Int<isize>, Int<isize>>
pub const fn gcd_ext(self, b: isize) -> GcdReturn<Int<isize>, Int<isize>>
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_isize).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: isize) -> GcdReturn<Int<isize>, Int<isize>>
pub const fn gcd_ext_euc(self, b: isize) -> GcdReturn<Int<isize>, Int<isize>>
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_isize).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: isize,
max: isize,
a: isize,
b: isize,
) -> Result<Int<isize>>
Available on crate feature cast
only.
pub const fn scale( self, min: isize, max: isize, a: isize, b: isize, ) -> Result<Int<isize>>
cast
only.Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as isize_up
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_isize).scale(0, 120, 30, 50)]; // interpolate
assert_eq![Ok(Int(112)), Int(100_isize).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: isize,
max: isize,
a: isize,
b: isize,
) -> Int<isize>
pub const fn scale_wrap( self, min: isize, max: isize, a: isize, b: isize, ) -> Int<isize>
Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as isize_up
.
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_isize).scale_wrap(0, 120, 30, 50)]; // interpolate
assert_eq![Int(112), Int(100_isize).scale_wrap(0, 80, 0, 90)]; // extrapolate
assert_eq![Int(-76), Int(100_i8).scale_wrap(0, 50, 0, 90)]; // extrapolate and wrap
Sourcepub const fn midpoint(self, other: isize) -> Int<isize>
Available on crate feature _int_usize
only.
pub const fn midpoint(self, other: isize) -> Int<isize>
_int_usize
only.Calculates the middle point of self
and other
.
The result is always rounded towards negative infinity.
§Examples
assert_eq![Int(0_isize).midpoint(4), 2];
assert_eq![Int(0_isize).midpoint(-1), -1];
assert_eq![Int(-1_isize).midpoint(0), -1];
Source§impl Int<u8>
Available on crate feature _int_u8
only.
impl Int<u8>
_int_u8
only.§Integer core methods for u8
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_u8).is_even()];
assert![!Int(3_u8).is_even()];
assert![Int(0_u8).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_u8).is_odd()];
assert![!Int(2_u8).is_odd()];
assert![!Int(0_u8).is_odd()];
Sourcepub const fn gcd(self, b: u8) -> Int<u8>
pub const fn gcd(self, b: u8) -> Int<u8>
Returns the GCD.
Uses Stein’s algorithm which is much more efficient to compute than Euclid’s.
§Examples
assert_eq![Int(4), Int(64_u8).gcd(36)];
assert_eq![Int(36), Int(0_u8).gcd(36)];
assert_eq![Int(64), Int(64_u8).gcd(0)];
Sourcepub const fn gcd_ext(self, b: u8) -> Result<GcdReturn<Int<u8>, Int<i16>>>
Available on crate features _int_i16
and cast
only.
pub const fn gcd_ext(self, b: u8) -> Result<GcdReturn<Int<u8>, Int<i16>>>
_int_i16
and cast
only.Returns the GCD and the Bézout coeficients.
Performs inner operations and returns coefficients as i16
.
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_u8).gcd_ext(36).unwrap().as_tuple();
assert_eq!(gcd.0, 4);
assert_eq![x.0 * 32 + y.0 * 36, gcd.0 as i16];
Sourcepub const fn gcd_ext_euc(self, b: u8) -> Result<GcdReturn<Int<u8>, Int<i16>>>
Available on crate features _int_i16
and cast
only.
pub const fn gcd_ext_euc(self, b: u8) -> Result<GcdReturn<Int<u8>, Int<i16>>>
_int_i16
and cast
only.Returns the GCD and the Bézout coeficients.
Performs inner operations and returns coefficients as i16
.
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_u8).gcd_ext_euc(36).unwrap().as_tuple();
assert_eq!(gcd.0, 4);
assert_eq![x.0 * 32 + y.0 * 36, gcd.0 as i16];
Sourcepub const fn scale(self, min: u8, max: u8, a: u8, b: u8) -> Result<Int<u8>>
Available on crate feature cast
only.
pub const fn scale(self, min: u8, max: u8, a: u8, b: u8) -> Result<Int<u8>>
cast
only.Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as u16
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_u8).scale(0, 120, 30, 50)]; // interpolate
assert_eq![Ok(Int(112)), Int(100_u8).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: u8, max: u8, a: u8, b: u8) -> Int<u8>
pub const fn scale_wrap(self, min: u8, max: u8, a: u8, b: u8) -> Int<u8>
Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as u16
.
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_u8).scale_wrap(0, 120, 30, 50)]; // interpolate
assert_eq![Int(112), Int(100_u8).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<u16>
Available on crate feature _int_u16
only.
impl Int<u16>
_int_u16
only.§Integer core methods for u16
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_u16).is_even()];
assert![!Int(3_u16).is_even()];
assert![Int(0_u16).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_u16).is_odd()];
assert![!Int(2_u16).is_odd()];
assert![!Int(0_u16).is_odd()];
Sourcepub const fn gcd(self, b: u16) -> Int<u16>
pub const fn gcd(self, b: u16) -> Int<u16>
Returns the GCD.
Uses Stein’s algorithm which is much more efficient to compute than Euclid’s.
§Examples
assert_eq![Int(4), Int(64_u16).gcd(36)];
assert_eq![Int(36), Int(0_u16).gcd(36)];
assert_eq![Int(64), Int(64_u16).gcd(0)];
Sourcepub const fn gcd_ext(self, b: u16) -> Result<GcdReturn<Int<u16>, Int<i32>>>
Available on crate features _int_i32
and cast
only.
pub const fn gcd_ext(self, b: u16) -> Result<GcdReturn<Int<u16>, Int<i32>>>
_int_i32
and cast
only.Returns the GCD and the Bézout coeficients.
Performs inner operations and returns coefficients as i32
.
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_u16).gcd_ext(36).unwrap().as_tuple();
assert_eq!(gcd.0, 4);
assert_eq![x.0 * 32 + y.0 * 36, gcd.0 as i32];
Sourcepub const fn gcd_ext_euc(self, b: u16) -> Result<GcdReturn<Int<u16>, Int<i32>>>
Available on crate features _int_i32
and cast
only.
pub const fn gcd_ext_euc(self, b: u16) -> Result<GcdReturn<Int<u16>, Int<i32>>>
_int_i32
and cast
only.Returns the GCD and the Bézout coeficients.
Performs inner operations and returns coefficients as i32
.
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_u16).gcd_ext_euc(36).unwrap().as_tuple();
assert_eq!(gcd.0, 4);
assert_eq![x.0 * 32 + y.0 * 36, gcd.0 as i32];
Sourcepub const fn scale(self, min: u16, max: u16, a: u16, b: u16) -> Result<Int<u16>>
Available on crate feature cast
only.
pub const fn scale(self, min: u16, max: u16, a: u16, b: u16) -> Result<Int<u16>>
cast
only.Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as u32
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_u16).scale(0, 120, 30, 50)]; // interpolate
assert_eq![Ok(Int(112)), Int(100_u16).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: u16, max: u16, a: u16, b: u16) -> Int<u16>
pub const fn scale_wrap(self, min: u16, max: u16, a: u16, b: u16) -> Int<u16>
Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as u32
.
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_u16).scale_wrap(0, 120, 30, 50)]; // interpolate
assert_eq![Int(112), Int(100_u16).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<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<u64>
Available on crate feature _int_u64
only.
impl Int<u64>
_int_u64
only.§Integer core methods for u64
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_u64).is_even()];
assert![!Int(3_u64).is_even()];
assert![Int(0_u64).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_u64).is_odd()];
assert![!Int(2_u64).is_odd()];
assert![!Int(0_u64).is_odd()];
Sourcepub const fn gcd(self, b: u64) -> Int<u64>
pub const fn gcd(self, b: u64) -> Int<u64>
Returns the GCD.
Uses Stein’s algorithm which is much more efficient to compute than Euclid’s.
§Examples
assert_eq![Int(4), Int(64_u64).gcd(36)];
assert_eq![Int(36), Int(0_u64).gcd(36)];
assert_eq![Int(64), Int(64_u64).gcd(0)];
Sourcepub const fn gcd_ext(self, b: u64) -> Result<GcdReturn<Int<u64>, Int<i128>>>
Available on crate features _int_i128
and cast
only.
pub const fn gcd_ext(self, b: u64) -> Result<GcdReturn<Int<u64>, Int<i128>>>
_int_i128
and cast
only.Returns the GCD and the Bézout coeficients.
Performs inner operations and returns coefficients as i128
.
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_u64).gcd_ext(36).unwrap().as_tuple();
assert_eq!(gcd.0, 4);
assert_eq![x.0 * 32 + y.0 * 36, gcd.0 as i128];
Sourcepub const fn gcd_ext_euc(self, b: u64) -> Result<GcdReturn<Int<u64>, Int<i128>>>
Available on crate features _int_i128
and cast
only.
pub const fn gcd_ext_euc(self, b: u64) -> Result<GcdReturn<Int<u64>, Int<i128>>>
_int_i128
and cast
only.Returns the GCD and the Bézout coeficients.
Performs inner operations and returns coefficients as i128
.
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_u64).gcd_ext_euc(36).unwrap().as_tuple();
assert_eq!(gcd.0, 4);
assert_eq![x.0 * 32 + y.0 * 36, gcd.0 as i128];
Sourcepub const fn scale(self, min: u64, max: u64, a: u64, b: u64) -> Result<Int<u64>>
Available on crate feature cast
only.
pub const fn scale(self, min: u64, max: u64, a: u64, b: u64) -> Result<Int<u64>>
cast
only.Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as u128
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_u64).scale(0, 120, 30, 50)]; // interpolate
assert_eq![Ok(Int(112)), Int(100_u64).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: u64, max: u64, a: u64, b: u64) -> Int<u64>
pub const fn scale_wrap(self, min: u64, max: u64, a: u64, b: u64) -> Int<u64>
Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as u128
.
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_u64).scale_wrap(0, 120, 30, 50)]; // interpolate
assert_eq![Int(112), Int(100_u64).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<u128>
Available on crate feature _int_u128
only.
impl Int<u128>
_int_u128
only.§Integer core methods for u128
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_u128).is_even()];
assert![!Int(3_u128).is_even()];
assert![Int(0_u128).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_u128).is_odd()];
assert![!Int(2_u128).is_odd()];
assert![!Int(0_u128).is_odd()];
Sourcepub const fn gcd(self, b: u128) -> Int<u128>
pub const fn gcd(self, b: u128) -> Int<u128>
Returns the GCD.
Uses Stein’s algorithm which is much more efficient to compute than Euclid’s.
§Examples
assert_eq![Int(4), Int(64_u128).gcd(36)];
assert_eq![Int(36), Int(0_u128).gcd(36)];
assert_eq![Int(64), Int(64_u128).gcd(0)];
Sourcepub const fn gcd_ext(self, b: u128) -> Result<GcdReturn<Int<u128>, Int<i128>>>
Available on crate features _int_i128
and cast
only.
pub const fn gcd_ext(self, b: u128) -> Result<GcdReturn<Int<u128>, Int<i128>>>
_int_i128
and cast
only.Returns the GCD and the Bézout coeficients.
Performs inner operations and returns coefficients as i128
.
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_u128).gcd_ext(36).unwrap().as_tuple();
assert_eq!(gcd.0, 4);
assert_eq![x.0 * 32 + y.0 * 36, gcd.0 as i128];
Sourcepub const fn gcd_ext_euc(
self,
b: u128,
) -> Result<GcdReturn<Int<u128>, Int<i128>>>
Available on crate features _int_i128
and cast
only.
pub const fn gcd_ext_euc( self, b: u128, ) -> Result<GcdReturn<Int<u128>, Int<i128>>>
_int_i128
and cast
only.Returns the GCD and the Bézout coeficients.
Performs inner operations and returns coefficients as i128
.
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_u128).gcd_ext_euc(36).unwrap().as_tuple();
assert_eq!(gcd.0, 4);
assert_eq![x.0 * 32 + y.0 * 36, gcd.0 as i128];
Sourcepub const fn scale(
self,
min: u128,
max: u128,
a: u128,
b: u128,
) -> Result<Int<u128>>
Available on crate feature cast
only.
pub const fn scale( self, min: u128, max: u128, a: u128, b: u128, ) -> Result<Int<u128>>
cast
only.Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as u128
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_u128).scale(0, 120, 30, 50)]; // interpolate
assert_eq![Ok(Int(112)), Int(100_u128).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: u128,
max: u128,
a: u128,
b: u128,
) -> Int<u128>
pub const fn scale_wrap( self, min: u128, max: u128, a: u128, b: u128, ) -> Int<u128>
Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as u128
.
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_u128).scale_wrap(0, 120, 30, 50)]; // interpolate
assert_eq![Int(112), Int(100_u128).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<usize>
Available on crate feature _int_usize
only.
impl Int<usize>
_int_usize
only.§Integer core methods for usize
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_usize).is_even()];
assert![!Int(3_usize).is_even()];
assert![Int(0_usize).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_usize).is_odd()];
assert![!Int(2_usize).is_odd()];
assert![!Int(0_usize).is_odd()];
Sourcepub const fn gcd(self, b: usize) -> Int<usize>
pub const fn gcd(self, b: usize) -> Int<usize>
Returns the GCD.
Uses Stein’s algorithm which is much more efficient to compute than Euclid’s.
§Examples
assert_eq![Int(4), Int(64_usize).gcd(36)];
assert_eq![Int(36), Int(0_usize).gcd(36)];
assert_eq![Int(64), Int(64_usize).gcd(0)];
Sourcepub const fn gcd_ext(
self,
b: usize,
) -> Result<GcdReturn<Int<usize>, Int<isize_up>>>
Available on crate features _int_i128
and cast
only.
pub const fn gcd_ext( self, b: usize, ) -> Result<GcdReturn<Int<usize>, Int<isize_up>>>
_int_i128
and cast
only.Returns the GCD and the Bézout coeficients.
Performs inner operations and returns coefficients as isize_up
.
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_usize).gcd_ext(36).unwrap().as_tuple();
assert_eq!(gcd.0, 4);
assert_eq![x.0 * 32 + y.0 * 36, gcd.0 as isize_up];
Sourcepub const fn gcd_ext_euc(
self,
b: usize,
) -> Result<GcdReturn<Int<usize>, Int<isize_up>>>
Available on crate features _int_i128
and cast
only.
pub const fn gcd_ext_euc( self, b: usize, ) -> Result<GcdReturn<Int<usize>, Int<isize_up>>>
_int_i128
and cast
only.Returns the GCD and the Bézout coeficients.
Performs inner operations and returns coefficients as isize_up
.
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_usize).gcd_ext_euc(36).unwrap().as_tuple();
assert_eq!(gcd.0, 4);
assert_eq![x.0 * 32 + y.0 * 36, gcd.0 as isize_up];
Sourcepub const fn scale(
self,
min: usize,
max: usize,
a: usize,
b: usize,
) -> Result<Int<usize>>
Available on crate feature cast
only.
pub const fn scale( self, min: usize, max: usize, a: usize, b: usize, ) -> Result<Int<usize>>
cast
only.Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as usize_up
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_usize).scale(0, 120, 30, 50)]; // interpolate
assert_eq![Ok(Int(112)), Int(100_usize).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: usize,
max: usize,
a: usize,
b: usize,
) -> Int<usize>
pub const fn scale_wrap( self, min: usize, max: usize, a: usize, b: usize, ) -> Int<usize>
Returns a scaled value between [min..=max]
to a new range [a..=b]
.
Performs operations internally as usize_up
.
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_usize).scale_wrap(0, 120, 30, 50)]; // interpolate
assert_eq![Int(112), Int(100_usize).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<i8>
Available on crate feature _int_i8
only.
impl Int<i8>
_int_i8
only.§Integer division related methods for i8
Sourcepub const fn div_rem(self, b: i8) -> [Int<i8>; 2]
Available on crate feature _int_i8
only.
pub const fn div_rem(self, b: i8) -> [Int<i8>; 2]
_int_i8
only.Returns the truncated quotient and the remainder.
Sourcepub const fn div_ceil(self, b: i8) -> Int<i8>
Available on crate feature _int_i8
only.
pub const fn div_ceil(self, b: i8) -> Int<i8>
_int_i8
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_i8).div_ceil(3), Int(3)]; // == 2.33…
assert_eq![Int(7_i8).div_ceil(-3), Int(-2)];
assert_eq![Int(-7_i8).div_ceil(3), Int(-2)];
assert_eq![Int(-7_i8).div_ceil(-3), Int(3)];
assert_eq![Int(7_i8).div_ceil(5), Int(2)]; // == 1.4
assert_eq![Int(6_i8).div_ceil(4), Int(2)]; // == 1.5
assert_eq![Int(8_i8).div_ceil(5), Int(2)]; // == 1.6
assert_eq![Int(5_i8).div_ceil(2), Int(3)]; // == 2.5
assert_eq![Int(-7_i8).div_ceil(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i8).div_ceil(4), Int(-1)]; // == -1.5
assert_eq![Int(-8_i8).div_ceil(5), Int(-1)]; // == -1.6
assert_eq![Int(-5_i8).div_ceil(2), Int(-2)]; // == -2.5
Sourcepub const fn div_floor(self, b: i8) -> Int<i8>
Available on crate feature _int_i8
only.
pub const fn div_floor(self, b: i8) -> Int<i8>
_int_i8
only.Returns the quotient, rounding the result towards negative infinity. $$ \large \left\lfloor \frac{x}{y} \right\rfloor $$
§Examples
assert_eq![Int(7_i8).div_floor(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i8).div_floor(-3), Int(-3)];
assert_eq![Int(-7_i8).div_floor(3), Int(-3)];
assert_eq![Int(-7_i8).div_floor(-3), Int(2)];
assert_eq![Int(7_i8).div_floor(5), Int(1)]; // == 1.4
assert_eq![Int(6_i8).div_floor(4), Int(1)]; // == 1.5
assert_eq![Int(8_i8).div_floor(5), Int(1)]; // == 1.6
assert_eq![Int(5_i8).div_floor(2), Int(2)]; // == 2.5
assert_eq![Int(-7_i8).div_floor(5), Int(-2)]; // == -1.4
assert_eq![Int(-6_i8).div_floor(4), Int(-2)]; // == -1.5
assert_eq![Int(-8_i8).div_floor(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i8).div_floor(2), Int(-3)]; // == -2.5
Sourcepub const fn div_ties_away(self, b: i8) -> Int<i8>
Available on crate feature _int_i8
only.
pub const fn div_ties_away(self, b: i8) -> Int<i8>
_int_i8
only.Returns the quotient, rounding ties away from zero.
§Examples
assert_eq![Int(7_i8).div_ties_away(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i8).div_ties_away(-3), Int(-2)];
assert_eq![Int(-7_i8).div_ties_away(3), Int(-2)];
assert_eq![Int(-7_i8).div_ties_away(-3), Int(2)];
assert_eq![Int(7_i8).div_ties_away(5), Int(1)]; // == 1.4
assert_eq![Int(6_i8).div_ties_away(4), Int(2)]; // == 1.5
assert_eq![Int(8_i8).div_ties_away(5), Int(2)]; // == 1.6
assert_eq![Int(5_i8).div_ties_away(2), Int(3)]; // == 2.5
assert_eq![Int(-7_i8).div_ties_away(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i8).div_ties_away(4), Int(-2)]; // == -1.5
assert_eq![Int(-8_i8).div_ties_away(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i8).div_ties_away(2), Int(-3)]; // == -2.5
Sourcepub const fn div_ties_towards(self, b: i8) -> Int<i8>
Available on crate feature _int_i8
only.
pub const fn div_ties_towards(self, b: i8) -> Int<i8>
_int_i8
only.Returns the quotient, rounding ties towards zero.
§Examples
assert_eq![Int(7_i8).div_ties_towards(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i8).div_ties_towards(-3), Int(-2)];
assert_eq![Int(-7_i8).div_ties_towards(3), Int(-2)];
assert_eq![Int(-7_i8).div_ties_towards(-3), Int(2)];
assert_eq![Int(7_i8).div_ties_towards(5), Int(1)]; // == 1.4
assert_eq![Int(6_i8).div_ties_towards(4), Int(1)]; // == 1.5
assert_eq![Int(8_i8).div_ties_towards(5), Int(2)]; // == 1.6
assert_eq![Int(5_i8).div_ties_towards(2), Int(2)]; // == 2.5
assert_eq![Int(-7_i8).div_ties_towards(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i8).div_ties_towards(4), Int(-1)]; // == -1.5
assert_eq![Int(-8_i8).div_ties_towards(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i8).div_ties_towards(2), Int(-2)]; // == -2.5
Sourcepub const fn div_ties_even(self, b: i8) -> Int<i8>
Available on crate feature _int_i8
only.
pub const fn div_ties_even(self, b: i8) -> Int<i8>
_int_i8
only.Returns the quotient, rounding ties to the nearest even number.
§Examples
assert_eq![Int(7_i8).div_ties_even(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i8).div_ties_even(-3), Int(-2)];
assert_eq![Int(-7_i8).div_ties_even(3), Int(-2)];
assert_eq![Int(-7_i8).div_ties_even(-3), Int(2)];
assert_eq![Int(7_i8).div_ties_even(5), Int(1)]; // == 1.4
assert_eq![Int(6_i8).div_ties_even(4), Int(2)]; // == 1.5
assert_eq![Int(8_i8).div_ties_even(5), Int(2)]; // == 1.6
assert_eq![Int(5_i8).div_ties_even(2), Int(2)]; // == 2.5
assert_eq![Int(-7_i8).div_ties_even(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i8).div_ties_even(4), Int(-2)]; // == -1.5
assert_eq![Int(-8_i8).div_ties_even(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i8).div_ties_even(2), Int(-2)]; // == -2.5
Sourcepub const fn div_ties_odd(self, b: i8) -> Int<i8>
Available on crate feature _int_i8
only.
pub const fn div_ties_odd(self, b: i8) -> Int<i8>
_int_i8
only.Returns the quotient, rounding ties to the nearest odd number.
§Examples
assert_eq![Int(7_i8).div_ties_odd(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i8).div_ties_odd(-3), Int(-2)];
assert_eq![Int(-7_i8).div_ties_odd(3), Int(-2)];
assert_eq![Int(-7_i8).div_ties_odd(-3), Int(2)];
assert_eq![Int(7_i8).div_ties_odd(5), Int(1)]; // == 1.4
assert_eq![Int(6_i8).div_ties_odd(4), Int(1)]; // == 1.5
assert_eq![Int(8_i8).div_ties_odd(5), Int(2)]; // == 1.6
assert_eq![Int(5_i8).div_ties_odd(2), Int(3)]; // == 2.5
assert_eq![Int(-7_i8).div_ties_odd(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i8).div_ties_odd(4), Int(-1)]; // == -1.5
assert_eq![Int(-8_i8).div_ties_odd(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i8).div_ties_odd(2), Int(-3)]; // == -2.5
Source§impl Int<i16>
Available on crate feature _int_i16
only.
impl Int<i16>
_int_i16
only.§Integer division related methods for i16
Sourcepub const fn div_rem(self, b: i16) -> [Int<i16>; 2]
Available on crate feature _int_i16
only.
pub const fn div_rem(self, b: i16) -> [Int<i16>; 2]
_int_i16
only.Returns the truncated quotient and the remainder.
Sourcepub const fn div_ceil(self, b: i16) -> Int<i16>
Available on crate feature _int_i16
only.
pub const fn div_ceil(self, b: i16) -> Int<i16>
_int_i16
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_i16).div_ceil(3), Int(3)]; // == 2.33…
assert_eq![Int(7_i16).div_ceil(-3), Int(-2)];
assert_eq![Int(-7_i16).div_ceil(3), Int(-2)];
assert_eq![Int(-7_i16).div_ceil(-3), Int(3)];
assert_eq![Int(7_i16).div_ceil(5), Int(2)]; // == 1.4
assert_eq![Int(6_i16).div_ceil(4), Int(2)]; // == 1.5
assert_eq![Int(8_i16).div_ceil(5), Int(2)]; // == 1.6
assert_eq![Int(5_i16).div_ceil(2), Int(3)]; // == 2.5
assert_eq![Int(-7_i16).div_ceil(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i16).div_ceil(4), Int(-1)]; // == -1.5
assert_eq![Int(-8_i16).div_ceil(5), Int(-1)]; // == -1.6
assert_eq![Int(-5_i16).div_ceil(2), Int(-2)]; // == -2.5
Sourcepub const fn div_floor(self, b: i16) -> Int<i16>
Available on crate feature _int_i16
only.
pub const fn div_floor(self, b: i16) -> Int<i16>
_int_i16
only.Returns the quotient, rounding the result towards negative infinity. $$ \large \left\lfloor \frac{x}{y} \right\rfloor $$
§Examples
assert_eq![Int(7_i16).div_floor(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i16).div_floor(-3), Int(-3)];
assert_eq![Int(-7_i16).div_floor(3), Int(-3)];
assert_eq![Int(-7_i16).div_floor(-3), Int(2)];
assert_eq![Int(7_i16).div_floor(5), Int(1)]; // == 1.4
assert_eq![Int(6_i16).div_floor(4), Int(1)]; // == 1.5
assert_eq![Int(8_i16).div_floor(5), Int(1)]; // == 1.6
assert_eq![Int(5_i16).div_floor(2), Int(2)]; // == 2.5
assert_eq![Int(-7_i16).div_floor(5), Int(-2)]; // == -1.4
assert_eq![Int(-6_i16).div_floor(4), Int(-2)]; // == -1.5
assert_eq![Int(-8_i16).div_floor(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i16).div_floor(2), Int(-3)]; // == -2.5
Sourcepub const fn div_ties_away(self, b: i16) -> Int<i16>
Available on crate feature _int_i16
only.
pub const fn div_ties_away(self, b: i16) -> Int<i16>
_int_i16
only.Returns the quotient, rounding ties away from zero.
§Examples
assert_eq![Int(7_i16).div_ties_away(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i16).div_ties_away(-3), Int(-2)];
assert_eq![Int(-7_i16).div_ties_away(3), Int(-2)];
assert_eq![Int(-7_i16).div_ties_away(-3), Int(2)];
assert_eq![Int(7_i16).div_ties_away(5), Int(1)]; // == 1.4
assert_eq![Int(6_i16).div_ties_away(4), Int(2)]; // == 1.5
assert_eq![Int(8_i16).div_ties_away(5), Int(2)]; // == 1.6
assert_eq![Int(5_i16).div_ties_away(2), Int(3)]; // == 2.5
assert_eq![Int(-7_i16).div_ties_away(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i16).div_ties_away(4), Int(-2)]; // == -1.5
assert_eq![Int(-8_i16).div_ties_away(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i16).div_ties_away(2), Int(-3)]; // == -2.5
Sourcepub const fn div_ties_towards(self, b: i16) -> Int<i16>
Available on crate feature _int_i16
only.
pub const fn div_ties_towards(self, b: i16) -> Int<i16>
_int_i16
only.Returns the quotient, rounding ties towards zero.
§Examples
assert_eq![Int(7_i16).div_ties_towards(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i16).div_ties_towards(-3), Int(-2)];
assert_eq![Int(-7_i16).div_ties_towards(3), Int(-2)];
assert_eq![Int(-7_i16).div_ties_towards(-3), Int(2)];
assert_eq![Int(7_i16).div_ties_towards(5), Int(1)]; // == 1.4
assert_eq![Int(6_i16).div_ties_towards(4), Int(1)]; // == 1.5
assert_eq![Int(8_i16).div_ties_towards(5), Int(2)]; // == 1.6
assert_eq![Int(5_i16).div_ties_towards(2), Int(2)]; // == 2.5
assert_eq![Int(-7_i16).div_ties_towards(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i16).div_ties_towards(4), Int(-1)]; // == -1.5
assert_eq![Int(-8_i16).div_ties_towards(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i16).div_ties_towards(2), Int(-2)]; // == -2.5
Sourcepub const fn div_ties_even(self, b: i16) -> Int<i16>
Available on crate feature _int_i16
only.
pub const fn div_ties_even(self, b: i16) -> Int<i16>
_int_i16
only.Returns the quotient, rounding ties to the nearest even number.
§Examples
assert_eq![Int(7_i16).div_ties_even(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i16).div_ties_even(-3), Int(-2)];
assert_eq![Int(-7_i16).div_ties_even(3), Int(-2)];
assert_eq![Int(-7_i16).div_ties_even(-3), Int(2)];
assert_eq![Int(7_i16).div_ties_even(5), Int(1)]; // == 1.4
assert_eq![Int(6_i16).div_ties_even(4), Int(2)]; // == 1.5
assert_eq![Int(8_i16).div_ties_even(5), Int(2)]; // == 1.6
assert_eq![Int(5_i16).div_ties_even(2), Int(2)]; // == 2.5
assert_eq![Int(-7_i16).div_ties_even(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i16).div_ties_even(4), Int(-2)]; // == -1.5
assert_eq![Int(-8_i16).div_ties_even(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i16).div_ties_even(2), Int(-2)]; // == -2.5
Sourcepub const fn div_ties_odd(self, b: i16) -> Int<i16>
Available on crate feature _int_i16
only.
pub const fn div_ties_odd(self, b: i16) -> Int<i16>
_int_i16
only.Returns the quotient, rounding ties to the nearest odd number.
§Examples
assert_eq![Int(7_i16).div_ties_odd(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i16).div_ties_odd(-3), Int(-2)];
assert_eq![Int(-7_i16).div_ties_odd(3), Int(-2)];
assert_eq![Int(-7_i16).div_ties_odd(-3), Int(2)];
assert_eq![Int(7_i16).div_ties_odd(5), Int(1)]; // == 1.4
assert_eq![Int(6_i16).div_ties_odd(4), Int(1)]; // == 1.5
assert_eq![Int(8_i16).div_ties_odd(5), Int(2)]; // == 1.6
assert_eq![Int(5_i16).div_ties_odd(2), Int(3)]; // == 2.5
assert_eq![Int(-7_i16).div_ties_odd(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i16).div_ties_odd(4), Int(-1)]; // == -1.5
assert_eq![Int(-8_i16).div_ties_odd(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i16).div_ties_odd(2), Int(-3)]; // == -2.5
Source§impl Int<i32>
Available on crate feature _int_i32
only.
impl Int<i32>
_int_i32
only.§Integer division related methods for i32
Sourcepub const fn div_rem(self, b: i32) -> [Int<i32>; 2]
Available on crate feature _int_i32
only.
pub const fn div_rem(self, b: i32) -> [Int<i32>; 2]
_int_i32
only.Returns the truncated quotient and the remainder.
Sourcepub const fn div_ceil(self, b: i32) -> Int<i32>
Available on crate feature _int_i32
only.
pub const fn div_ceil(self, b: i32) -> Int<i32>
_int_i32
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_i32).div_ceil(3), Int(3)]; // == 2.33…
assert_eq![Int(7_i32).div_ceil(-3), Int(-2)];
assert_eq![Int(-7_i32).div_ceil(3), Int(-2)];
assert_eq![Int(-7_i32).div_ceil(-3), Int(3)];
assert_eq![Int(7_i32).div_ceil(5), Int(2)]; // == 1.4
assert_eq![Int(6_i32).div_ceil(4), Int(2)]; // == 1.5
assert_eq![Int(8_i32).div_ceil(5), Int(2)]; // == 1.6
assert_eq![Int(5_i32).div_ceil(2), Int(3)]; // == 2.5
assert_eq![Int(-7_i32).div_ceil(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i32).div_ceil(4), Int(-1)]; // == -1.5
assert_eq![Int(-8_i32).div_ceil(5), Int(-1)]; // == -1.6
assert_eq![Int(-5_i32).div_ceil(2), Int(-2)]; // == -2.5
Sourcepub const fn div_floor(self, b: i32) -> Int<i32>
Available on crate feature _int_i32
only.
pub const fn div_floor(self, b: i32) -> Int<i32>
_int_i32
only.Returns the quotient, rounding the result towards negative infinity. $$ \large \left\lfloor \frac{x}{y} \right\rfloor $$
§Examples
assert_eq![Int(7_i32).div_floor(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i32).div_floor(-3), Int(-3)];
assert_eq![Int(-7_i32).div_floor(3), Int(-3)];
assert_eq![Int(-7_i32).div_floor(-3), Int(2)];
assert_eq![Int(7_i32).div_floor(5), Int(1)]; // == 1.4
assert_eq![Int(6_i32).div_floor(4), Int(1)]; // == 1.5
assert_eq![Int(8_i32).div_floor(5), Int(1)]; // == 1.6
assert_eq![Int(5_i32).div_floor(2), Int(2)]; // == 2.5
assert_eq![Int(-7_i32).div_floor(5), Int(-2)]; // == -1.4
assert_eq![Int(-6_i32).div_floor(4), Int(-2)]; // == -1.5
assert_eq![Int(-8_i32).div_floor(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i32).div_floor(2), Int(-3)]; // == -2.5
Sourcepub const fn div_ties_away(self, b: i32) -> Int<i32>
Available on crate feature _int_i32
only.
pub const fn div_ties_away(self, b: i32) -> Int<i32>
_int_i32
only.Returns the quotient, rounding ties away from zero.
§Examples
assert_eq![Int(7_i32).div_ties_away(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i32).div_ties_away(-3), Int(-2)];
assert_eq![Int(-7_i32).div_ties_away(3), Int(-2)];
assert_eq![Int(-7_i32).div_ties_away(-3), Int(2)];
assert_eq![Int(7_i32).div_ties_away(5), Int(1)]; // == 1.4
assert_eq![Int(6_i32).div_ties_away(4), Int(2)]; // == 1.5
assert_eq![Int(8_i32).div_ties_away(5), Int(2)]; // == 1.6
assert_eq![Int(5_i32).div_ties_away(2), Int(3)]; // == 2.5
assert_eq![Int(-7_i32).div_ties_away(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i32).div_ties_away(4), Int(-2)]; // == -1.5
assert_eq![Int(-8_i32).div_ties_away(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i32).div_ties_away(2), Int(-3)]; // == -2.5
Sourcepub const fn div_ties_towards(self, b: i32) -> Int<i32>
Available on crate feature _int_i32
only.
pub const fn div_ties_towards(self, b: i32) -> Int<i32>
_int_i32
only.Returns the quotient, rounding ties towards zero.
§Examples
assert_eq![Int(7_i32).div_ties_towards(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i32).div_ties_towards(-3), Int(-2)];
assert_eq![Int(-7_i32).div_ties_towards(3), Int(-2)];
assert_eq![Int(-7_i32).div_ties_towards(-3), Int(2)];
assert_eq![Int(7_i32).div_ties_towards(5), Int(1)]; // == 1.4
assert_eq![Int(6_i32).div_ties_towards(4), Int(1)]; // == 1.5
assert_eq![Int(8_i32).div_ties_towards(5), Int(2)]; // == 1.6
assert_eq![Int(5_i32).div_ties_towards(2), Int(2)]; // == 2.5
assert_eq![Int(-7_i32).div_ties_towards(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i32).div_ties_towards(4), Int(-1)]; // == -1.5
assert_eq![Int(-8_i32).div_ties_towards(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i32).div_ties_towards(2), Int(-2)]; // == -2.5
Sourcepub const fn div_ties_even(self, b: i32) -> Int<i32>
Available on crate feature _int_i32
only.
pub const fn div_ties_even(self, b: i32) -> Int<i32>
_int_i32
only.Returns the quotient, rounding ties to the nearest even number.
§Examples
assert_eq![Int(7_i32).div_ties_even(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i32).div_ties_even(-3), Int(-2)];
assert_eq![Int(-7_i32).div_ties_even(3), Int(-2)];
assert_eq![Int(-7_i32).div_ties_even(-3), Int(2)];
assert_eq![Int(7_i32).div_ties_even(5), Int(1)]; // == 1.4
assert_eq![Int(6_i32).div_ties_even(4), Int(2)]; // == 1.5
assert_eq![Int(8_i32).div_ties_even(5), Int(2)]; // == 1.6
assert_eq![Int(5_i32).div_ties_even(2), Int(2)]; // == 2.5
assert_eq![Int(-7_i32).div_ties_even(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i32).div_ties_even(4), Int(-2)]; // == -1.5
assert_eq![Int(-8_i32).div_ties_even(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i32).div_ties_even(2), Int(-2)]; // == -2.5
Sourcepub const fn div_ties_odd(self, b: i32) -> Int<i32>
Available on crate feature _int_i32
only.
pub const fn div_ties_odd(self, b: i32) -> Int<i32>
_int_i32
only.Returns the quotient, rounding ties to the nearest odd number.
§Examples
assert_eq![Int(7_i32).div_ties_odd(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i32).div_ties_odd(-3), Int(-2)];
assert_eq![Int(-7_i32).div_ties_odd(3), Int(-2)];
assert_eq![Int(-7_i32).div_ties_odd(-3), Int(2)];
assert_eq![Int(7_i32).div_ties_odd(5), Int(1)]; // == 1.4
assert_eq![Int(6_i32).div_ties_odd(4), Int(1)]; // == 1.5
assert_eq![Int(8_i32).div_ties_odd(5), Int(2)]; // == 1.6
assert_eq![Int(5_i32).div_ties_odd(2), Int(3)]; // == 2.5
assert_eq![Int(-7_i32).div_ties_odd(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i32).div_ties_odd(4), Int(-1)]; // == -1.5
assert_eq![Int(-8_i32).div_ties_odd(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i32).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 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<i128>
Available on crate feature _int_i128
only.
impl Int<i128>
_int_i128
only.§Integer division related methods for i128
Sourcepub const fn div_rem(self, b: i128) -> [Int<i128>; 2]
Available on crate feature _int_i128
only.
pub const fn div_rem(self, b: i128) -> [Int<i128>; 2]
_int_i128
only.Returns the truncated quotient and the remainder.
Sourcepub const fn div_ceil(self, b: i128) -> Int<i128>
Available on crate feature _int_i128
only.
pub const fn div_ceil(self, b: i128) -> Int<i128>
_int_i128
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_i128).div_ceil(3), Int(3)]; // == 2.33…
assert_eq![Int(7_i128).div_ceil(-3), Int(-2)];
assert_eq![Int(-7_i128).div_ceil(3), Int(-2)];
assert_eq![Int(-7_i128).div_ceil(-3), Int(3)];
assert_eq![Int(7_i128).div_ceil(5), Int(2)]; // == 1.4
assert_eq![Int(6_i128).div_ceil(4), Int(2)]; // == 1.5
assert_eq![Int(8_i128).div_ceil(5), Int(2)]; // == 1.6
assert_eq![Int(5_i128).div_ceil(2), Int(3)]; // == 2.5
assert_eq![Int(-7_i128).div_ceil(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i128).div_ceil(4), Int(-1)]; // == -1.5
assert_eq![Int(-8_i128).div_ceil(5), Int(-1)]; // == -1.6
assert_eq![Int(-5_i128).div_ceil(2), Int(-2)]; // == -2.5
Sourcepub const fn div_floor(self, b: i128) -> Int<i128>
Available on crate feature _int_i128
only.
pub const fn div_floor(self, b: i128) -> Int<i128>
_int_i128
only.Returns the quotient, rounding the result towards negative infinity. $$ \large \left\lfloor \frac{x}{y} \right\rfloor $$
§Examples
assert_eq![Int(7_i128).div_floor(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i128).div_floor(-3), Int(-3)];
assert_eq![Int(-7_i128).div_floor(3), Int(-3)];
assert_eq![Int(-7_i128).div_floor(-3), Int(2)];
assert_eq![Int(7_i128).div_floor(5), Int(1)]; // == 1.4
assert_eq![Int(6_i128).div_floor(4), Int(1)]; // == 1.5
assert_eq![Int(8_i128).div_floor(5), Int(1)]; // == 1.6
assert_eq![Int(5_i128).div_floor(2), Int(2)]; // == 2.5
assert_eq![Int(-7_i128).div_floor(5), Int(-2)]; // == -1.4
assert_eq![Int(-6_i128).div_floor(4), Int(-2)]; // == -1.5
assert_eq![Int(-8_i128).div_floor(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i128).div_floor(2), Int(-3)]; // == -2.5
Sourcepub const fn div_ties_away(self, b: i128) -> Int<i128>
Available on crate feature _int_i128
only.
pub const fn div_ties_away(self, b: i128) -> Int<i128>
_int_i128
only.Returns the quotient, rounding ties away from zero.
§Examples
assert_eq![Int(7_i128).div_ties_away(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i128).div_ties_away(-3), Int(-2)];
assert_eq![Int(-7_i128).div_ties_away(3), Int(-2)];
assert_eq![Int(-7_i128).div_ties_away(-3), Int(2)];
assert_eq![Int(7_i128).div_ties_away(5), Int(1)]; // == 1.4
assert_eq![Int(6_i128).div_ties_away(4), Int(2)]; // == 1.5
assert_eq![Int(8_i128).div_ties_away(5), Int(2)]; // == 1.6
assert_eq![Int(5_i128).div_ties_away(2), Int(3)]; // == 2.5
assert_eq![Int(-7_i128).div_ties_away(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i128).div_ties_away(4), Int(-2)]; // == -1.5
assert_eq![Int(-8_i128).div_ties_away(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i128).div_ties_away(2), Int(-3)]; // == -2.5
Sourcepub const fn div_ties_towards(self, b: i128) -> Int<i128>
Available on crate feature _int_i128
only.
pub const fn div_ties_towards(self, b: i128) -> Int<i128>
_int_i128
only.Returns the quotient, rounding ties towards zero.
§Examples
assert_eq![Int(7_i128).div_ties_towards(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i128).div_ties_towards(-3), Int(-2)];
assert_eq![Int(-7_i128).div_ties_towards(3), Int(-2)];
assert_eq![Int(-7_i128).div_ties_towards(-3), Int(2)];
assert_eq![Int(7_i128).div_ties_towards(5), Int(1)]; // == 1.4
assert_eq![Int(6_i128).div_ties_towards(4), Int(1)]; // == 1.5
assert_eq![Int(8_i128).div_ties_towards(5), Int(2)]; // == 1.6
assert_eq![Int(5_i128).div_ties_towards(2), Int(2)]; // == 2.5
assert_eq![Int(-7_i128).div_ties_towards(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i128).div_ties_towards(4), Int(-1)]; // == -1.5
assert_eq![Int(-8_i128).div_ties_towards(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i128).div_ties_towards(2), Int(-2)]; // == -2.5
Sourcepub const fn div_ties_even(self, b: i128) -> Int<i128>
Available on crate feature _int_i128
only.
pub const fn div_ties_even(self, b: i128) -> Int<i128>
_int_i128
only.Returns the quotient, rounding ties to the nearest even number.
§Examples
assert_eq![Int(7_i128).div_ties_even(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i128).div_ties_even(-3), Int(-2)];
assert_eq![Int(-7_i128).div_ties_even(3), Int(-2)];
assert_eq![Int(-7_i128).div_ties_even(-3), Int(2)];
assert_eq![Int(7_i128).div_ties_even(5), Int(1)]; // == 1.4
assert_eq![Int(6_i128).div_ties_even(4), Int(2)]; // == 1.5
assert_eq![Int(8_i128).div_ties_even(5), Int(2)]; // == 1.6
assert_eq![Int(5_i128).div_ties_even(2), Int(2)]; // == 2.5
assert_eq![Int(-7_i128).div_ties_even(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i128).div_ties_even(4), Int(-2)]; // == -1.5
assert_eq![Int(-8_i128).div_ties_even(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i128).div_ties_even(2), Int(-2)]; // == -2.5
Sourcepub const fn div_ties_odd(self, b: i128) -> Int<i128>
Available on crate feature _int_i128
only.
pub const fn div_ties_odd(self, b: i128) -> Int<i128>
_int_i128
only.Returns the quotient, rounding ties to the nearest odd number.
§Examples
assert_eq![Int(7_i128).div_ties_odd(3), Int(2)]; // == 2.33…
assert_eq![Int(7_i128).div_ties_odd(-3), Int(-2)];
assert_eq![Int(-7_i128).div_ties_odd(3), Int(-2)];
assert_eq![Int(-7_i128).div_ties_odd(-3), Int(2)];
assert_eq![Int(7_i128).div_ties_odd(5), Int(1)]; // == 1.4
assert_eq![Int(6_i128).div_ties_odd(4), Int(1)]; // == 1.5
assert_eq![Int(8_i128).div_ties_odd(5), Int(2)]; // == 1.6
assert_eq![Int(5_i128).div_ties_odd(2), Int(3)]; // == 2.5
assert_eq![Int(-7_i128).div_ties_odd(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_i128).div_ties_odd(4), Int(-1)]; // == -1.5
assert_eq![Int(-8_i128).div_ties_odd(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_i128).div_ties_odd(2), Int(-3)]; // == -2.5
Source§impl Int<isize>
Available on crate feature _int_isize
only.
impl Int<isize>
_int_isize
only.§Integer division related methods for isize
Sourcepub const fn div_rem(self, b: isize) -> [Int<isize>; 2]
Available on crate feature _int_isize
only.
pub const fn div_rem(self, b: isize) -> [Int<isize>; 2]
_int_isize
only.Returns the truncated quotient and the remainder.
Sourcepub const fn div_ceil(self, b: isize) -> Int<isize>
Available on crate feature _int_isize
only.
pub const fn div_ceil(self, b: isize) -> Int<isize>
_int_isize
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_isize).div_ceil(3), Int(3)]; // == 2.33…
assert_eq![Int(7_isize).div_ceil(-3), Int(-2)];
assert_eq![Int(-7_isize).div_ceil(3), Int(-2)];
assert_eq![Int(-7_isize).div_ceil(-3), Int(3)];
assert_eq![Int(7_isize).div_ceil(5), Int(2)]; // == 1.4
assert_eq![Int(6_isize).div_ceil(4), Int(2)]; // == 1.5
assert_eq![Int(8_isize).div_ceil(5), Int(2)]; // == 1.6
assert_eq![Int(5_isize).div_ceil(2), Int(3)]; // == 2.5
assert_eq![Int(-7_isize).div_ceil(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_isize).div_ceil(4), Int(-1)]; // == -1.5
assert_eq![Int(-8_isize).div_ceil(5), Int(-1)]; // == -1.6
assert_eq![Int(-5_isize).div_ceil(2), Int(-2)]; // == -2.5
Sourcepub const fn div_floor(self, b: isize) -> Int<isize>
Available on crate feature _int_isize
only.
pub const fn div_floor(self, b: isize) -> Int<isize>
_int_isize
only.Returns the quotient, rounding the result towards negative infinity. $$ \large \left\lfloor \frac{x}{y} \right\rfloor $$
§Examples
assert_eq![Int(7_isize).div_floor(3), Int(2)]; // == 2.33…
assert_eq![Int(7_isize).div_floor(-3), Int(-3)];
assert_eq![Int(-7_isize).div_floor(3), Int(-3)];
assert_eq![Int(-7_isize).div_floor(-3), Int(2)];
assert_eq![Int(7_isize).div_floor(5), Int(1)]; // == 1.4
assert_eq![Int(6_isize).div_floor(4), Int(1)]; // == 1.5
assert_eq![Int(8_isize).div_floor(5), Int(1)]; // == 1.6
assert_eq![Int(5_isize).div_floor(2), Int(2)]; // == 2.5
assert_eq![Int(-7_isize).div_floor(5), Int(-2)]; // == -1.4
assert_eq![Int(-6_isize).div_floor(4), Int(-2)]; // == -1.5
assert_eq![Int(-8_isize).div_floor(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_isize).div_floor(2), Int(-3)]; // == -2.5
Sourcepub const fn div_ties_away(self, b: isize) -> Int<isize>
Available on crate feature _int_isize
only.
pub const fn div_ties_away(self, b: isize) -> Int<isize>
_int_isize
only.Returns the quotient, rounding ties away from zero.
§Examples
assert_eq![Int(7_isize).div_ties_away(3), Int(2)]; // == 2.33…
assert_eq![Int(7_isize).div_ties_away(-3), Int(-2)];
assert_eq![Int(-7_isize).div_ties_away(3), Int(-2)];
assert_eq![Int(-7_isize).div_ties_away(-3), Int(2)];
assert_eq![Int(7_isize).div_ties_away(5), Int(1)]; // == 1.4
assert_eq![Int(6_isize).div_ties_away(4), Int(2)]; // == 1.5
assert_eq![Int(8_isize).div_ties_away(5), Int(2)]; // == 1.6
assert_eq![Int(5_isize).div_ties_away(2), Int(3)]; // == 2.5
assert_eq![Int(-7_isize).div_ties_away(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_isize).div_ties_away(4), Int(-2)]; // == -1.5
assert_eq![Int(-8_isize).div_ties_away(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_isize).div_ties_away(2), Int(-3)]; // == -2.5
Sourcepub const fn div_ties_towards(self, b: isize) -> Int<isize>
Available on crate feature _int_isize
only.
pub const fn div_ties_towards(self, b: isize) -> Int<isize>
_int_isize
only.Returns the quotient, rounding ties towards zero.
§Examples
assert_eq![Int(7_isize).div_ties_towards(3), Int(2)]; // == 2.33…
assert_eq![Int(7_isize).div_ties_towards(-3), Int(-2)];
assert_eq![Int(-7_isize).div_ties_towards(3), Int(-2)];
assert_eq![Int(-7_isize).div_ties_towards(-3), Int(2)];
assert_eq![Int(7_isize).div_ties_towards(5), Int(1)]; // == 1.4
assert_eq![Int(6_isize).div_ties_towards(4), Int(1)]; // == 1.5
assert_eq![Int(8_isize).div_ties_towards(5), Int(2)]; // == 1.6
assert_eq![Int(5_isize).div_ties_towards(2), Int(2)]; // == 2.5
assert_eq![Int(-7_isize).div_ties_towards(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_isize).div_ties_towards(4), Int(-1)]; // == -1.5
assert_eq![Int(-8_isize).div_ties_towards(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_isize).div_ties_towards(2), Int(-2)]; // == -2.5
Sourcepub const fn div_ties_even(self, b: isize) -> Int<isize>
Available on crate feature _int_isize
only.
pub const fn div_ties_even(self, b: isize) -> Int<isize>
_int_isize
only.Returns the quotient, rounding ties to the nearest even number.
§Examples
assert_eq![Int(7_isize).div_ties_even(3), Int(2)]; // == 2.33…
assert_eq![Int(7_isize).div_ties_even(-3), Int(-2)];
assert_eq![Int(-7_isize).div_ties_even(3), Int(-2)];
assert_eq![Int(-7_isize).div_ties_even(-3), Int(2)];
assert_eq![Int(7_isize).div_ties_even(5), Int(1)]; // == 1.4
assert_eq![Int(6_isize).div_ties_even(4), Int(2)]; // == 1.5
assert_eq![Int(8_isize).div_ties_even(5), Int(2)]; // == 1.6
assert_eq![Int(5_isize).div_ties_even(2), Int(2)]; // == 2.5
assert_eq![Int(-7_isize).div_ties_even(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_isize).div_ties_even(4), Int(-2)]; // == -1.5
assert_eq![Int(-8_isize).div_ties_even(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_isize).div_ties_even(2), Int(-2)]; // == -2.5
Sourcepub const fn div_ties_odd(self, b: isize) -> Int<isize>
Available on crate feature _int_isize
only.
pub const fn div_ties_odd(self, b: isize) -> Int<isize>
_int_isize
only.Returns the quotient, rounding ties to the nearest odd number.
§Examples
assert_eq![Int(7_isize).div_ties_odd(3), Int(2)]; // == 2.33…
assert_eq![Int(7_isize).div_ties_odd(-3), Int(-2)];
assert_eq![Int(-7_isize).div_ties_odd(3), Int(-2)];
assert_eq![Int(-7_isize).div_ties_odd(-3), Int(2)];
assert_eq![Int(7_isize).div_ties_odd(5), Int(1)]; // == 1.4
assert_eq![Int(6_isize).div_ties_odd(4), Int(1)]; // == 1.5
assert_eq![Int(8_isize).div_ties_odd(5), Int(2)]; // == 1.6
assert_eq![Int(5_isize).div_ties_odd(2), Int(3)]; // == 2.5
assert_eq![Int(-7_isize).div_ties_odd(5), Int(-1)]; // == -1.4
assert_eq![Int(-6_isize).div_ties_odd(4), Int(-1)]; // == -1.5
assert_eq![Int(-8_isize).div_ties_odd(5), Int(-2)]; // == -1.6
assert_eq![Int(-5_isize).div_ties_odd(2), Int(-3)]; // == -2.5
Source§impl Int<u8>
Available on crate feature _int_u8
only.
impl Int<u8>
_int_u8
only.§Integer division related methods for u8
Sourcepub const fn div_rem(self, b: u8) -> [Int<u8>; 2]
pub const fn div_rem(self, b: u8) -> [Int<u8>; 2]
Returns the truncated quotient and the remainder.
Sourcepub const fn div_ceil(self, b: u8) -> Int<u8>
pub const fn div_ceil(self, b: u8) -> Int<u8>
Returns the quotient, rounding the result towards positive infinity. $$ \large \left\lceil \frac{x}{y} \right\rceil $$
§Examples
assert_eq![Int(7_u8).div_ceil(3), Int(3)]; // == 2.33…
assert_eq![Int(7_u8).div_ceil(5), Int(2)]; // == 1.4
assert_eq![Int(6_u8).div_ceil(4), Int(2)]; // == 1.5
assert_eq![Int(8_u8).div_ceil(5), Int(2)]; // == 1.6
assert_eq![Int(5_u8).div_ceil(2), Int(3)]; // == 2.5
Sourcepub const fn div_floor(self, b: u8) -> Int<u8>
pub const fn div_floor(self, b: u8) -> Int<u8>
Returns the quotient, rounding the result towards negative infinity. $$ \large \left\lfloor \frac{x}{y} \right\rfloor $$
§Examples
assert_eq![Int(7_u8).div_floor(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u8).div_floor(5), Int(1)]; // == 1.4
assert_eq![Int(6_u8).div_floor(4), Int(1)]; // == 1.5
assert_eq![Int(8_u8).div_floor(5), Int(1)]; // == 1.6
assert_eq![Int(5_u8).div_floor(2), Int(2)]; // == 2.5
Sourcepub const fn div_ties_away(self, b: u8) -> Int<u8>
pub const fn div_ties_away(self, b: u8) -> Int<u8>
Returns the quotient, rounding ties away from zero.
§Examples
assert_eq![Int(7_u8).div_ties_away(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u8).div_ties_away(5), Int(1)]; // == 1.4
assert_eq![Int(6_u8).div_ties_away(4), Int(2)]; // == 1.5
assert_eq![Int(8_u8).div_ties_away(5), Int(2)]; // == 1.6
assert_eq![Int(5_u8).div_ties_away(2), Int(3)]; // == 2.5
Sourcepub const fn div_ties_towards(self, b: u8) -> Int<u8>
pub const fn div_ties_towards(self, b: u8) -> Int<u8>
Returns the quotient, rounding ties towards zero.
§Examples
assert_eq![Int(7_u8).div_ties_towards(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u8).div_ties_towards(5), Int(1)]; // == 1.4
assert_eq![Int(6_u8).div_ties_towards(4), Int(1)]; // == 1.5
assert_eq![Int(8_u8).div_ties_towards(5), Int(2)]; // == 1.6
assert_eq![Int(5_u8).div_ties_towards(2), Int(2)]; // == 2.5
Sourcepub const fn div_ties_even(self, b: u8) -> Int<u8>
pub const fn div_ties_even(self, b: u8) -> Int<u8>
Returns the quotient, rounding ties to the nearest even number.
§Examples
assert_eq![Int(7_u8).div_ties_even(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u8).div_ties_even(5), Int(1)]; // == 1.4
assert_eq![Int(6_u8).div_ties_even(4), Int(2)]; // == 1.5
assert_eq![Int(8_u8).div_ties_even(5), Int(2)]; // == 1.6
assert_eq![Int(5_u8).div_ties_even(2), Int(2)]; // == 2.5
Sourcepub const fn div_ties_odd(self, b: u8) -> Int<u8>
pub const fn div_ties_odd(self, b: u8) -> Int<u8>
Returns the quotient, rounding ties to the nearest even number.
§Examples
assert_eq![Int(7_u8).div_ties_odd(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u8).div_ties_odd(5), Int(1)]; // == 1.4
assert_eq![Int(6_u8).div_ties_odd(4), Int(1)]; // == 1.5
assert_eq![Int(8_u8).div_ties_odd(5), Int(2)]; // == 1.6
assert_eq![Int(5_u8).div_ties_odd(2), Int(3)]; // == 2.5
Source§impl Int<u16>
Available on crate feature _int_u16
only.
impl Int<u16>
_int_u16
only.§Integer division related methods for u16
Sourcepub const fn div_rem(self, b: u16) -> [Int<u16>; 2]
pub const fn div_rem(self, b: u16) -> [Int<u16>; 2]
Returns the truncated quotient and the remainder.
Sourcepub const fn div_ceil(self, b: u16) -> Int<u16>
pub const fn div_ceil(self, b: u16) -> Int<u16>
Returns the quotient, rounding the result towards positive infinity. $$ \large \left\lceil \frac{x}{y} \right\rceil $$
§Examples
assert_eq![Int(7_u16).div_ceil(3), Int(3)]; // == 2.33…
assert_eq![Int(7_u16).div_ceil(5), Int(2)]; // == 1.4
assert_eq![Int(6_u16).div_ceil(4), Int(2)]; // == 1.5
assert_eq![Int(8_u16).div_ceil(5), Int(2)]; // == 1.6
assert_eq![Int(5_u16).div_ceil(2), Int(3)]; // == 2.5
Sourcepub const fn div_floor(self, b: u16) -> Int<u16>
pub const fn div_floor(self, b: u16) -> Int<u16>
Returns the quotient, rounding the result towards negative infinity. $$ \large \left\lfloor \frac{x}{y} \right\rfloor $$
§Examples
assert_eq![Int(7_u16).div_floor(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u16).div_floor(5), Int(1)]; // == 1.4
assert_eq![Int(6_u16).div_floor(4), Int(1)]; // == 1.5
assert_eq![Int(8_u16).div_floor(5), Int(1)]; // == 1.6
assert_eq![Int(5_u16).div_floor(2), Int(2)]; // == 2.5
Sourcepub const fn div_ties_away(self, b: u16) -> Int<u16>
pub const fn div_ties_away(self, b: u16) -> Int<u16>
Returns the quotient, rounding ties away from zero.
§Examples
assert_eq![Int(7_u16).div_ties_away(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u16).div_ties_away(5), Int(1)]; // == 1.4
assert_eq![Int(6_u16).div_ties_away(4), Int(2)]; // == 1.5
assert_eq![Int(8_u16).div_ties_away(5), Int(2)]; // == 1.6
assert_eq![Int(5_u16).div_ties_away(2), Int(3)]; // == 2.5
Sourcepub const fn div_ties_towards(self, b: u16) -> Int<u16>
pub const fn div_ties_towards(self, b: u16) -> Int<u16>
Returns the quotient, rounding ties towards zero.
§Examples
assert_eq![Int(7_u16).div_ties_towards(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u16).div_ties_towards(5), Int(1)]; // == 1.4
assert_eq![Int(6_u16).div_ties_towards(4), Int(1)]; // == 1.5
assert_eq![Int(8_u16).div_ties_towards(5), Int(2)]; // == 1.6
assert_eq![Int(5_u16).div_ties_towards(2), Int(2)]; // == 2.5
Sourcepub const fn div_ties_even(self, b: u16) -> Int<u16>
pub const fn div_ties_even(self, b: u16) -> Int<u16>
Returns the quotient, rounding ties to the nearest even number.
§Examples
assert_eq![Int(7_u16).div_ties_even(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u16).div_ties_even(5), Int(1)]; // == 1.4
assert_eq![Int(6_u16).div_ties_even(4), Int(2)]; // == 1.5
assert_eq![Int(8_u16).div_ties_even(5), Int(2)]; // == 1.6
assert_eq![Int(5_u16).div_ties_even(2), Int(2)]; // == 2.5
Sourcepub const fn div_ties_odd(self, b: u16) -> Int<u16>
pub const fn div_ties_odd(self, b: u16) -> Int<u16>
Returns the quotient, rounding ties to the nearest even number.
§Examples
assert_eq![Int(7_u16).div_ties_odd(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u16).div_ties_odd(5), Int(1)]; // == 1.4
assert_eq![Int(6_u16).div_ties_odd(4), Int(1)]; // == 1.5
assert_eq![Int(8_u16).div_ties_odd(5), Int(2)]; // == 1.6
assert_eq![Int(5_u16).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<u64>
Available on crate feature _int_u64
only.
impl Int<u64>
_int_u64
only.§Integer division related methods for u64
Sourcepub const fn div_rem(self, b: u64) -> [Int<u64>; 2]
pub const fn div_rem(self, b: u64) -> [Int<u64>; 2]
Returns the truncated quotient and the remainder.
Sourcepub const fn div_ceil(self, b: u64) -> Int<u64>
pub const fn div_ceil(self, b: u64) -> Int<u64>
Returns the quotient, rounding the result towards positive infinity. $$ \large \left\lceil \frac{x}{y} \right\rceil $$
§Examples
assert_eq![Int(7_u64).div_ceil(3), Int(3)]; // == 2.33…
assert_eq![Int(7_u64).div_ceil(5), Int(2)]; // == 1.4
assert_eq![Int(6_u64).div_ceil(4), Int(2)]; // == 1.5
assert_eq![Int(8_u64).div_ceil(5), Int(2)]; // == 1.6
assert_eq![Int(5_u64).div_ceil(2), Int(3)]; // == 2.5
Sourcepub const fn div_floor(self, b: u64) -> Int<u64>
pub const fn div_floor(self, b: u64) -> Int<u64>
Returns the quotient, rounding the result towards negative infinity. $$ \large \left\lfloor \frac{x}{y} \right\rfloor $$
§Examples
assert_eq![Int(7_u64).div_floor(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u64).div_floor(5), Int(1)]; // == 1.4
assert_eq![Int(6_u64).div_floor(4), Int(1)]; // == 1.5
assert_eq![Int(8_u64).div_floor(5), Int(1)]; // == 1.6
assert_eq![Int(5_u64).div_floor(2), Int(2)]; // == 2.5
Sourcepub const fn div_ties_away(self, b: u64) -> Int<u64>
pub const fn div_ties_away(self, b: u64) -> Int<u64>
Returns the quotient, rounding ties away from zero.
§Examples
assert_eq![Int(7_u64).div_ties_away(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u64).div_ties_away(5), Int(1)]; // == 1.4
assert_eq![Int(6_u64).div_ties_away(4), Int(2)]; // == 1.5
assert_eq![Int(8_u64).div_ties_away(5), Int(2)]; // == 1.6
assert_eq![Int(5_u64).div_ties_away(2), Int(3)]; // == 2.5
Sourcepub const fn div_ties_towards(self, b: u64) -> Int<u64>
pub const fn div_ties_towards(self, b: u64) -> Int<u64>
Returns the quotient, rounding ties towards zero.
§Examples
assert_eq![Int(7_u64).div_ties_towards(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u64).div_ties_towards(5), Int(1)]; // == 1.4
assert_eq![Int(6_u64).div_ties_towards(4), Int(1)]; // == 1.5
assert_eq![Int(8_u64).div_ties_towards(5), Int(2)]; // == 1.6
assert_eq![Int(5_u64).div_ties_towards(2), Int(2)]; // == 2.5
Sourcepub const fn div_ties_even(self, b: u64) -> Int<u64>
pub const fn div_ties_even(self, b: u64) -> Int<u64>
Returns the quotient, rounding ties to the nearest even number.
§Examples
assert_eq![Int(7_u64).div_ties_even(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u64).div_ties_even(5), Int(1)]; // == 1.4
assert_eq![Int(6_u64).div_ties_even(4), Int(2)]; // == 1.5
assert_eq![Int(8_u64).div_ties_even(5), Int(2)]; // == 1.6
assert_eq![Int(5_u64).div_ties_even(2), Int(2)]; // == 2.5
Sourcepub const fn div_ties_odd(self, b: u64) -> Int<u64>
pub const fn div_ties_odd(self, b: u64) -> Int<u64>
Returns the quotient, rounding ties to the nearest even number.
§Examples
assert_eq![Int(7_u64).div_ties_odd(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u64).div_ties_odd(5), Int(1)]; // == 1.4
assert_eq![Int(6_u64).div_ties_odd(4), Int(1)]; // == 1.5
assert_eq![Int(8_u64).div_ties_odd(5), Int(2)]; // == 1.6
assert_eq![Int(5_u64).div_ties_odd(2), Int(3)]; // == 2.5
Source§impl Int<u128>
Available on crate feature _int_u128
only.
impl Int<u128>
_int_u128
only.§Integer division related methods for u128
Sourcepub const fn div_rem(self, b: u128) -> [Int<u128>; 2]
pub const fn div_rem(self, b: u128) -> [Int<u128>; 2]
Returns the truncated quotient and the remainder.
Sourcepub const fn div_ceil(self, b: u128) -> Int<u128>
pub const fn div_ceil(self, b: u128) -> Int<u128>
Returns the quotient, rounding the result towards positive infinity. $$ \large \left\lceil \frac{x}{y} \right\rceil $$
§Examples
assert_eq![Int(7_u128).div_ceil(3), Int(3)]; // == 2.33…
assert_eq![Int(7_u128).div_ceil(5), Int(2)]; // == 1.4
assert_eq![Int(6_u128).div_ceil(4), Int(2)]; // == 1.5
assert_eq![Int(8_u128).div_ceil(5), Int(2)]; // == 1.6
assert_eq![Int(5_u128).div_ceil(2), Int(3)]; // == 2.5
Sourcepub const fn div_floor(self, b: u128) -> Int<u128>
pub const fn div_floor(self, b: u128) -> Int<u128>
Returns the quotient, rounding the result towards negative infinity. $$ \large \left\lfloor \frac{x}{y} \right\rfloor $$
§Examples
assert_eq![Int(7_u128).div_floor(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u128).div_floor(5), Int(1)]; // == 1.4
assert_eq![Int(6_u128).div_floor(4), Int(1)]; // == 1.5
assert_eq![Int(8_u128).div_floor(5), Int(1)]; // == 1.6
assert_eq![Int(5_u128).div_floor(2), Int(2)]; // == 2.5
Sourcepub const fn div_ties_away(self, b: u128) -> Int<u128>
pub const fn div_ties_away(self, b: u128) -> Int<u128>
Returns the quotient, rounding ties away from zero.
§Examples
assert_eq![Int(7_u128).div_ties_away(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u128).div_ties_away(5), Int(1)]; // == 1.4
assert_eq![Int(6_u128).div_ties_away(4), Int(2)]; // == 1.5
assert_eq![Int(8_u128).div_ties_away(5), Int(2)]; // == 1.6
assert_eq![Int(5_u128).div_ties_away(2), Int(3)]; // == 2.5
Sourcepub const fn div_ties_towards(self, b: u128) -> Int<u128>
pub const fn div_ties_towards(self, b: u128) -> Int<u128>
Returns the quotient, rounding ties towards zero.
§Examples
assert_eq![Int(7_u128).div_ties_towards(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u128).div_ties_towards(5), Int(1)]; // == 1.4
assert_eq![Int(6_u128).div_ties_towards(4), Int(1)]; // == 1.5
assert_eq![Int(8_u128).div_ties_towards(5), Int(2)]; // == 1.6
assert_eq![Int(5_u128).div_ties_towards(2), Int(2)]; // == 2.5
Sourcepub const fn div_ties_even(self, b: u128) -> Int<u128>
pub const fn div_ties_even(self, b: u128) -> Int<u128>
Returns the quotient, rounding ties to the nearest even number.
§Examples
assert_eq![Int(7_u128).div_ties_even(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u128).div_ties_even(5), Int(1)]; // == 1.4
assert_eq![Int(6_u128).div_ties_even(4), Int(2)]; // == 1.5
assert_eq![Int(8_u128).div_ties_even(5), Int(2)]; // == 1.6
assert_eq![Int(5_u128).div_ties_even(2), Int(2)]; // == 2.5
Sourcepub const fn div_ties_odd(self, b: u128) -> Int<u128>
pub const fn div_ties_odd(self, b: u128) -> Int<u128>
Returns the quotient, rounding ties to the nearest even number.
§Examples
assert_eq![Int(7_u128).div_ties_odd(3), Int(2)]; // == 2.33…
assert_eq![Int(7_u128).div_ties_odd(5), Int(1)]; // == 1.4
assert_eq![Int(6_u128).div_ties_odd(4), Int(1)]; // == 1.5
assert_eq![Int(8_u128).div_ties_odd(5), Int(2)]; // == 1.6
assert_eq![Int(5_u128).div_ties_odd(2), Int(3)]; // == 2.5
Source§impl Int<usize>
Available on crate feature _int_usize
only.
impl Int<usize>
_int_usize
only.§Integer division related methods for usize
Sourcepub const fn div_rem(self, b: usize) -> [Int<usize>; 2]
pub const fn div_rem(self, b: usize) -> [Int<usize>; 2]
Returns the truncated quotient and the remainder.
Sourcepub const fn div_ceil(self, b: usize) -> Int<usize>
pub const fn div_ceil(self, b: usize) -> Int<usize>
Returns the quotient, rounding the result towards positive infinity. $$ \large \left\lceil \frac{x}{y} \right\rceil $$
§Examples
assert_eq![Int(7_usize).div_ceil(3), Int(3)]; // == 2.33…
assert_eq![Int(7_usize).div_ceil(5), Int(2)]; // == 1.4
assert_eq![Int(6_usize).div_ceil(4), Int(2)]; // == 1.5
assert_eq![Int(8_usize).div_ceil(5), Int(2)]; // == 1.6
assert_eq![Int(5_usize).div_ceil(2), Int(3)]; // == 2.5
Sourcepub const fn div_floor(self, b: usize) -> Int<usize>
pub const fn div_floor(self, b: usize) -> Int<usize>
Returns the quotient, rounding the result towards negative infinity. $$ \large \left\lfloor \frac{x}{y} \right\rfloor $$
§Examples
assert_eq![Int(7_usize).div_floor(3), Int(2)]; // == 2.33…
assert_eq![Int(7_usize).div_floor(5), Int(1)]; // == 1.4
assert_eq![Int(6_usize).div_floor(4), Int(1)]; // == 1.5
assert_eq![Int(8_usize).div_floor(5), Int(1)]; // == 1.6
assert_eq![Int(5_usize).div_floor(2), Int(2)]; // == 2.5
Sourcepub const fn div_ties_away(self, b: usize) -> Int<usize>
pub const fn div_ties_away(self, b: usize) -> Int<usize>
Returns the quotient, rounding ties away from zero.
§Examples
assert_eq![Int(7_usize).div_ties_away(3), Int(2)]; // == 2.33…
assert_eq![Int(7_usize).div_ties_away(5), Int(1)]; // == 1.4
assert_eq![Int(6_usize).div_ties_away(4), Int(2)]; // == 1.5
assert_eq![Int(8_usize).div_ties_away(5), Int(2)]; // == 1.6
assert_eq![Int(5_usize).div_ties_away(2), Int(3)]; // == 2.5
Sourcepub const fn div_ties_towards(self, b: usize) -> Int<usize>
pub const fn div_ties_towards(self, b: usize) -> Int<usize>
Returns the quotient, rounding ties towards zero.
§Examples
assert_eq![Int(7_usize).div_ties_towards(3), Int(2)]; // == 2.33…
assert_eq![Int(7_usize).div_ties_towards(5), Int(1)]; // == 1.4
assert_eq![Int(6_usize).div_ties_towards(4), Int(1)]; // == 1.5
assert_eq![Int(8_usize).div_ties_towards(5), Int(2)]; // == 1.6
assert_eq![Int(5_usize).div_ties_towards(2), Int(2)]; // == 2.5
Sourcepub const fn div_ties_even(self, b: usize) -> Int<usize>
pub const fn div_ties_even(self, b: usize) -> Int<usize>
Returns the quotient, rounding ties to the nearest even number.
§Examples
assert_eq![Int(7_usize).div_ties_even(3), Int(2)]; // == 2.33…
assert_eq![Int(7_usize).div_ties_even(5), Int(1)]; // == 1.4
assert_eq![Int(6_usize).div_ties_even(4), Int(2)]; // == 1.5
assert_eq![Int(8_usize).div_ties_even(5), Int(2)]; // == 1.6
assert_eq![Int(5_usize).div_ties_even(2), Int(2)]; // == 2.5
Sourcepub const fn div_ties_odd(self, b: usize) -> Int<usize>
pub const fn div_ties_odd(self, b: usize) -> Int<usize>
Returns the quotient, rounding ties to the nearest even number.
§Examples
assert_eq![Int(7_usize).div_ties_odd(3), Int(2)]; // == 2.33…
assert_eq![Int(7_usize).div_ties_odd(5), Int(1)]; // == 1.4
assert_eq![Int(6_usize).div_ties_odd(4), Int(1)]; // == 1.5
assert_eq![Int(8_usize).div_ties_odd(5), Int(2)]; // == 1.6
assert_eq![Int(5_usize).div_ties_odd(2), Int(3)]; // == 2.5
Source§impl Int<i8>
Available on crate feature _int_i8
only.
impl Int<i8>
_int_i8
only.§Integer factors related methods for i8
- Allocating:
- Not allocating:
Sourcepub fn factors(self) -> Vec<i8> ⓘ
Available on crate feature alloc
only.
pub fn factors(self) -> Vec<i8> ⓘ
alloc
only.Returns the factors (including 1 and self).
§Examples
assert_eq![Int(24_i8).factors(), vec![1, 2, 3, 4, 6, 8, 12, 24]];
assert_eq![Int(-24_i8).factors(), vec![1, 2, 3, 4, 6, 8, 12, 24]];
assert![Int(0_i8).factors().is_empty()];
assert_eq![Int(1_i8).factors(), vec![1]];
assert_eq![Int(7_i8).factors(), vec![1, 7]];
Sourcepub fn factors_proper(self) -> Vec<i8> ⓘ
Available on crate feature alloc
only.
pub fn factors_proper(self) -> Vec<i8> ⓘ
alloc
only.Returns the proper factors.
§Examples
assert_eq![Int(24_i8).factors_proper(), vec![2, 3, 4, 6, 8, 12]];
assert_eq![Int(-24_i8).factors_proper(), vec![2, 3, 4, 6, 8, 12]];
assert![Int(0_i8).factors_proper().is_empty()];
assert![Int(1_i8).factors_proper().is_empty()];
assert![Int(7_i8).factors_proper().is_empty()];
Sourcepub fn factors_prime(self) -> Vec<i8> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime(self) -> Vec<i8> ⓘ
alloc
only.Returns the prime factors.
§Examples
assert_eq![Int(24_i8).factors_prime(), vec![2, 2, 2, 3]];
assert_eq![Int(-24_i8).factors_prime(), vec![2, 2, 2, 3]];
assert![Int(0_i8).factors_prime().is_empty()];
assert![Int(1_i8).factors_prime().is_empty()];
assert_eq![Int(7_i8).factors_prime(), vec![7]];
Sourcepub fn factors_prime_unique(self) -> Vec<i8> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique(self) -> Vec<i8> ⓘ
alloc
only.Returns the unique prime factors.
§Examples
assert_eq![Int(24_i8).factors_prime_unique(), vec![2, 3]];
assert_eq![Int(-24_i8).factors_prime_unique(), vec![2, 3]];
Sourcepub fn factors_prime_unique_exp(self) -> Vec<(i8, u32)> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique_exp(self) -> Vec<(i8, u32)> ⓘ
alloc
only.Returns the unique prime factors with its exponent.
§Examples
assert_eq![Int(24_i8).factors_prime_unique_exp(), vec![(2, 3), (3, 1)]];
assert_eq![Int(-24_i8).factors_prime_unique_exp(), vec![(2, 3), (3, 1)]];
assert![Int(0_i8).factors_prime_unique_exp().is_empty()];
assert![Int(1_i8).factors_prime_unique_exp().is_empty()];
assert_eq![Int(7_i8).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_i8).factors_prime_count(), 4];
assert_eq![Int(-24_i8).factors_prime_count(), 4];
assert_eq![Int(0_i8).factors_prime_count(), 0];
assert_eq![Int(1_i8).factors_prime_count(), 0];
assert_eq![Int(7_i8).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_i8).factors_prime_unique_count(), 2];
assert_eq![Int(-24_i8).factors_prime_unique_count(), 2];
Sourcepub fn factors_buf(
self,
fbuf: &mut [i8],
upfbuf: &mut [i8],
) -> Result<(usize, usize)>
pub fn factors_buf( self, fbuf: &mut [i8], upfbuf: &mut [i8], ) -> 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_i8).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 [i8],
upfbuf: &mut [i8],
) -> Result<(usize, usize)>
pub fn factors_proper_buf( self, fbuf: &mut [i8], upfbuf: &mut [i8], ) -> 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_i8).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 [i8]) -> Result<usize>
pub fn factors_prime_buf(self, buffer: &mut [i8]) -> 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_i8).factors_prime_buf(&mut buf), Ok(4)];
assert_eq![buf[..4], [2, 2, 2, 3]];
assert![Int(24_i8 * 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_i8).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(1_i8).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(7_i8).factors_prime_buf(&mut buf), Ok(1)];
assert_eq![buf[..1], [7]];
Sourcepub fn factors_prime_unique_buf(self, buffer: &mut [i8]) -> Result<usize>
pub fn factors_prime_unique_buf(self, buffer: &mut [i8]) -> 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_i8).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 [i8],
ebuffer: &mut [u32],
) -> Result<usize>
pub fn factors_prime_unique_exp_buf( self, fbuffer: &mut [i8], 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_i8).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_i8).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(1_i8).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(7_i8).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_i8).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_i8).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 [i8],
upfbuf: &mut [i8],
) -> Result<(usize, usize)>
pub fn factors_prime_unique_plus_buf( self, pfbuf: &mut [i8], upfbuf: &mut [i8], ) -> 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_i8).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<i16>
Available on crate feature _int_i16
only.
impl Int<i16>
_int_i16
only.§Integer factors related methods for i16
- Allocating:
- Not allocating:
Sourcepub fn factors(self) -> Vec<i16> ⓘ
Available on crate feature alloc
only.
pub fn factors(self) -> Vec<i16> ⓘ
alloc
only.Returns the factors (including 1 and self).
§Examples
assert_eq![Int(24_i16).factors(), vec![1, 2, 3, 4, 6, 8, 12, 24]];
assert_eq![Int(-24_i16).factors(), vec![1, 2, 3, 4, 6, 8, 12, 24]];
assert![Int(0_i16).factors().is_empty()];
assert_eq![Int(1_i16).factors(), vec![1]];
assert_eq![Int(7_i16).factors(), vec![1, 7]];
Sourcepub fn factors_proper(self) -> Vec<i16> ⓘ
Available on crate feature alloc
only.
pub fn factors_proper(self) -> Vec<i16> ⓘ
alloc
only.Returns the proper factors.
§Examples
assert_eq![Int(24_i16).factors_proper(), vec![2, 3, 4, 6, 8, 12]];
assert_eq![Int(-24_i16).factors_proper(), vec![2, 3, 4, 6, 8, 12]];
assert![Int(0_i16).factors_proper().is_empty()];
assert![Int(1_i16).factors_proper().is_empty()];
assert![Int(7_i16).factors_proper().is_empty()];
Sourcepub fn factors_prime(self) -> Vec<i16> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime(self) -> Vec<i16> ⓘ
alloc
only.Returns the prime factors.
§Examples
assert_eq![Int(24_i16).factors_prime(), vec![2, 2, 2, 3]];
assert_eq![Int(-24_i16).factors_prime(), vec![2, 2, 2, 3]];
assert![Int(0_i16).factors_prime().is_empty()];
assert![Int(1_i16).factors_prime().is_empty()];
assert_eq![Int(7_i16).factors_prime(), vec![7]];
Sourcepub fn factors_prime_unique(self) -> Vec<i16> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique(self) -> Vec<i16> ⓘ
alloc
only.Returns the unique prime factors.
§Examples
assert_eq![Int(24_i16).factors_prime_unique(), vec![2, 3]];
assert_eq![Int(-24_i16).factors_prime_unique(), vec![2, 3]];
Sourcepub fn factors_prime_unique_exp(self) -> Vec<(i16, u32)> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique_exp(self) -> Vec<(i16, u32)> ⓘ
alloc
only.Returns the unique prime factors with its exponent.
§Examples
assert_eq![Int(24_i16).factors_prime_unique_exp(), vec![(2, 3), (3, 1)]];
assert_eq![Int(-24_i16).factors_prime_unique_exp(), vec![(2, 3), (3, 1)]];
assert![Int(0_i16).factors_prime_unique_exp().is_empty()];
assert![Int(1_i16).factors_prime_unique_exp().is_empty()];
assert_eq![Int(7_i16).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_i16).factors_prime_count(), 4];
assert_eq![Int(-24_i16).factors_prime_count(), 4];
assert_eq![Int(0_i16).factors_prime_count(), 0];
assert_eq![Int(1_i16).factors_prime_count(), 0];
assert_eq![Int(7_i16).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_i16).factors_prime_unique_count(), 2];
assert_eq![Int(-24_i16).factors_prime_unique_count(), 2];
Sourcepub fn factors_buf(
self,
fbuf: &mut [i16],
upfbuf: &mut [i16],
) -> Result<(usize, usize)>
pub fn factors_buf( self, fbuf: &mut [i16], upfbuf: &mut [i16], ) -> 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_i16).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 [i16],
upfbuf: &mut [i16],
) -> Result<(usize, usize)>
pub fn factors_proper_buf( self, fbuf: &mut [i16], upfbuf: &mut [i16], ) -> 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_i16).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 [i16]) -> Result<usize>
pub fn factors_prime_buf(self, buffer: &mut [i16]) -> 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_i16).factors_prime_buf(&mut buf), Ok(4)];
assert_eq![buf[..4], [2, 2, 2, 3]];
assert![Int(24_i16 * 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_i16).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(1_i16).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(7_i16).factors_prime_buf(&mut buf), Ok(1)];
assert_eq![buf[..1], [7]];
Sourcepub fn factors_prime_unique_buf(self, buffer: &mut [i16]) -> Result<usize>
pub fn factors_prime_unique_buf(self, buffer: &mut [i16]) -> 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_i16).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 [i16],
ebuffer: &mut [u32],
) -> Result<usize>
pub fn factors_prime_unique_exp_buf( self, fbuffer: &mut [i16], 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_i16).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_i16).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(1_i16).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(7_i16).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_i16).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_i16).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 [i16],
upfbuf: &mut [i16],
) -> Result<(usize, usize)>
pub fn factors_prime_unique_plus_buf( self, pfbuf: &mut [i16], upfbuf: &mut [i16], ) -> 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_i16).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<i32>
Available on crate feature _int_i32
only.
impl Int<i32>
_int_i32
only.§Integer factors related methods for i32
- Allocating:
- Not allocating:
Sourcepub fn factors(self) -> Vec<i32> ⓘ
Available on crate feature alloc
only.
pub fn factors(self) -> Vec<i32> ⓘ
alloc
only.Returns the factors (including 1 and self).
§Examples
assert_eq![Int(24_i32).factors(), vec![1, 2, 3, 4, 6, 8, 12, 24]];
assert_eq![Int(-24_i32).factors(), vec![1, 2, 3, 4, 6, 8, 12, 24]];
assert![Int(0_i32).factors().is_empty()];
assert_eq![Int(1_i32).factors(), vec![1]];
assert_eq![Int(7_i32).factors(), vec![1, 7]];
Sourcepub fn factors_proper(self) -> Vec<i32> ⓘ
Available on crate feature alloc
only.
pub fn factors_proper(self) -> Vec<i32> ⓘ
alloc
only.Returns the proper factors.
§Examples
assert_eq![Int(24_i32).factors_proper(), vec![2, 3, 4, 6, 8, 12]];
assert_eq![Int(-24_i32).factors_proper(), vec![2, 3, 4, 6, 8, 12]];
assert![Int(0_i32).factors_proper().is_empty()];
assert![Int(1_i32).factors_proper().is_empty()];
assert![Int(7_i32).factors_proper().is_empty()];
Sourcepub fn factors_prime(self) -> Vec<i32> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime(self) -> Vec<i32> ⓘ
alloc
only.Returns the prime factors.
§Examples
assert_eq![Int(24_i32).factors_prime(), vec![2, 2, 2, 3]];
assert_eq![Int(-24_i32).factors_prime(), vec![2, 2, 2, 3]];
assert![Int(0_i32).factors_prime().is_empty()];
assert![Int(1_i32).factors_prime().is_empty()];
assert_eq![Int(7_i32).factors_prime(), vec![7]];
Sourcepub fn factors_prime_unique(self) -> Vec<i32> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique(self) -> Vec<i32> ⓘ
alloc
only.Returns the unique prime factors.
§Examples
assert_eq![Int(24_i32).factors_prime_unique(), vec![2, 3]];
assert_eq![Int(-24_i32).factors_prime_unique(), vec![2, 3]];
Sourcepub fn factors_prime_unique_exp(self) -> Vec<(i32, u32)> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique_exp(self) -> Vec<(i32, u32)> ⓘ
alloc
only.Returns the unique prime factors with its exponent.
§Examples
assert_eq![Int(24_i32).factors_prime_unique_exp(), vec![(2, 3), (3, 1)]];
assert_eq![Int(-24_i32).factors_prime_unique_exp(), vec![(2, 3), (3, 1)]];
assert![Int(0_i32).factors_prime_unique_exp().is_empty()];
assert![Int(1_i32).factors_prime_unique_exp().is_empty()];
assert_eq![Int(7_i32).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_i32).factors_prime_count(), 4];
assert_eq![Int(-24_i32).factors_prime_count(), 4];
assert_eq![Int(0_i32).factors_prime_count(), 0];
assert_eq![Int(1_i32).factors_prime_count(), 0];
assert_eq![Int(7_i32).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_i32).factors_prime_unique_count(), 2];
assert_eq![Int(-24_i32).factors_prime_unique_count(), 2];
Sourcepub fn factors_buf(
self,
fbuf: &mut [i32],
upfbuf: &mut [i32],
) -> Result<(usize, usize)>
pub fn factors_buf( self, fbuf: &mut [i32], upfbuf: &mut [i32], ) -> 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_i32).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 [i32],
upfbuf: &mut [i32],
) -> Result<(usize, usize)>
pub fn factors_proper_buf( self, fbuf: &mut [i32], upfbuf: &mut [i32], ) -> 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_i32).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 [i32]) -> Result<usize>
pub fn factors_prime_buf(self, buffer: &mut [i32]) -> 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_i32).factors_prime_buf(&mut buf), Ok(4)];
assert_eq![buf[..4], [2, 2, 2, 3]];
assert![Int(24_i32 * 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_i32).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(1_i32).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(7_i32).factors_prime_buf(&mut buf), Ok(1)];
assert_eq![buf[..1], [7]];
Sourcepub fn factors_prime_unique_buf(self, buffer: &mut [i32]) -> Result<usize>
pub fn factors_prime_unique_buf(self, buffer: &mut [i32]) -> 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_i32).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 [i32],
ebuffer: &mut [u32],
) -> Result<usize>
pub fn factors_prime_unique_exp_buf( self, fbuffer: &mut [i32], 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_i32).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_i32).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(1_i32).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(7_i32).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_i32).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_i32).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 [i32],
upfbuf: &mut [i32],
) -> Result<(usize, usize)>
pub fn factors_prime_unique_plus_buf( self, pfbuf: &mut [i32], upfbuf: &mut [i32], ) -> 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_i32).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 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<i128>
Available on crate feature _int_i128
only.
impl Int<i128>
_int_i128
only.§Integer factors related methods for i128
- Allocating:
- Not allocating:
Sourcepub fn factors(self) -> Vec<i128> ⓘ
Available on crate feature alloc
only.
pub fn factors(self) -> Vec<i128> ⓘ
alloc
only.Returns the factors (including 1 and self).
§Examples
assert_eq![Int(24_i128).factors(), vec![1, 2, 3, 4, 6, 8, 12, 24]];
assert_eq![Int(-24_i128).factors(), vec![1, 2, 3, 4, 6, 8, 12, 24]];
assert![Int(0_i128).factors().is_empty()];
assert_eq![Int(1_i128).factors(), vec![1]];
assert_eq![Int(7_i128).factors(), vec![1, 7]];
Sourcepub fn factors_proper(self) -> Vec<i128> ⓘ
Available on crate feature alloc
only.
pub fn factors_proper(self) -> Vec<i128> ⓘ
alloc
only.Returns the proper factors.
§Examples
assert_eq![Int(24_i128).factors_proper(), vec![2, 3, 4, 6, 8, 12]];
assert_eq![Int(-24_i128).factors_proper(), vec![2, 3, 4, 6, 8, 12]];
assert![Int(0_i128).factors_proper().is_empty()];
assert![Int(1_i128).factors_proper().is_empty()];
assert![Int(7_i128).factors_proper().is_empty()];
Sourcepub fn factors_prime(self) -> Vec<i128> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime(self) -> Vec<i128> ⓘ
alloc
only.Returns the prime factors.
§Examples
assert_eq![Int(24_i128).factors_prime(), vec![2, 2, 2, 3]];
assert_eq![Int(-24_i128).factors_prime(), vec![2, 2, 2, 3]];
assert![Int(0_i128).factors_prime().is_empty()];
assert![Int(1_i128).factors_prime().is_empty()];
assert_eq![Int(7_i128).factors_prime(), vec![7]];
Sourcepub fn factors_prime_unique(self) -> Vec<i128> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique(self) -> Vec<i128> ⓘ
alloc
only.Returns the unique prime factors.
§Examples
assert_eq![Int(24_i128).factors_prime_unique(), vec![2, 3]];
assert_eq![Int(-24_i128).factors_prime_unique(), vec![2, 3]];
Sourcepub fn factors_prime_unique_exp(self) -> Vec<(i128, u32)> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique_exp(self) -> Vec<(i128, u32)> ⓘ
alloc
only.Returns the unique prime factors with its exponent.
§Examples
assert_eq![Int(24_i128).factors_prime_unique_exp(), vec![(2, 3), (3, 1)]];
assert_eq![Int(-24_i128).factors_prime_unique_exp(), vec![(2, 3), (3, 1)]];
assert![Int(0_i128).factors_prime_unique_exp().is_empty()];
assert![Int(1_i128).factors_prime_unique_exp().is_empty()];
assert_eq![Int(7_i128).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_i128).factors_prime_count(), 4];
assert_eq![Int(-24_i128).factors_prime_count(), 4];
assert_eq![Int(0_i128).factors_prime_count(), 0];
assert_eq![Int(1_i128).factors_prime_count(), 0];
assert_eq![Int(7_i128).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_i128).factors_prime_unique_count(), 2];
assert_eq![Int(-24_i128).factors_prime_unique_count(), 2];
Sourcepub fn factors_buf(
self,
fbuf: &mut [i128],
upfbuf: &mut [i128],
) -> Result<(usize, usize)>
pub fn factors_buf( self, fbuf: &mut [i128], upfbuf: &mut [i128], ) -> 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_i128).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 [i128],
upfbuf: &mut [i128],
) -> Result<(usize, usize)>
pub fn factors_proper_buf( self, fbuf: &mut [i128], upfbuf: &mut [i128], ) -> 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_i128).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 [i128]) -> Result<usize>
pub fn factors_prime_buf(self, buffer: &mut [i128]) -> 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_i128).factors_prime_buf(&mut buf), Ok(4)];
assert_eq![buf[..4], [2, 2, 2, 3]];
assert![Int(24_i128 * 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_i128).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(1_i128).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(7_i128).factors_prime_buf(&mut buf), Ok(1)];
assert_eq![buf[..1], [7]];
Sourcepub fn factors_prime_unique_buf(self, buffer: &mut [i128]) -> Result<usize>
pub fn factors_prime_unique_buf(self, buffer: &mut [i128]) -> 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_i128).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 [i128],
ebuffer: &mut [u32],
) -> Result<usize>
pub fn factors_prime_unique_exp_buf( self, fbuffer: &mut [i128], 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_i128).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_i128).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(1_i128).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(7_i128).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_i128).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_i128).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 [i128],
upfbuf: &mut [i128],
) -> Result<(usize, usize)>
pub fn factors_prime_unique_plus_buf( self, pfbuf: &mut [i128], upfbuf: &mut [i128], ) -> 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_i128).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<isize>
Available on crate feature _int_isize
only.
impl Int<isize>
_int_isize
only.§Integer factors related methods for isize
- Allocating:
- Not allocating:
Sourcepub fn factors(self) -> Vec<isize> ⓘ
Available on crate feature alloc
only.
pub fn factors(self) -> Vec<isize> ⓘ
alloc
only.Returns the factors (including 1 and self).
§Examples
assert_eq![Int(24_isize).factors(), vec![1, 2, 3, 4, 6, 8, 12, 24]];
assert_eq![Int(-24_isize).factors(), vec![1, 2, 3, 4, 6, 8, 12, 24]];
assert![Int(0_isize).factors().is_empty()];
assert_eq![Int(1_isize).factors(), vec![1]];
assert_eq![Int(7_isize).factors(), vec![1, 7]];
Sourcepub fn factors_proper(self) -> Vec<isize> ⓘ
Available on crate feature alloc
only.
pub fn factors_proper(self) -> Vec<isize> ⓘ
alloc
only.Returns the proper factors.
§Examples
assert_eq![Int(24_isize).factors_proper(), vec![2, 3, 4, 6, 8, 12]];
assert_eq![Int(-24_isize).factors_proper(), vec![2, 3, 4, 6, 8, 12]];
assert![Int(0_isize).factors_proper().is_empty()];
assert![Int(1_isize).factors_proper().is_empty()];
assert![Int(7_isize).factors_proper().is_empty()];
Sourcepub fn factors_prime(self) -> Vec<isize> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime(self) -> Vec<isize> ⓘ
alloc
only.Returns the prime factors.
§Examples
assert_eq![Int(24_isize).factors_prime(), vec![2, 2, 2, 3]];
assert_eq![Int(-24_isize).factors_prime(), vec![2, 2, 2, 3]];
assert![Int(0_isize).factors_prime().is_empty()];
assert![Int(1_isize).factors_prime().is_empty()];
assert_eq![Int(7_isize).factors_prime(), vec![7]];
Sourcepub fn factors_prime_unique(self) -> Vec<isize> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique(self) -> Vec<isize> ⓘ
alloc
only.Returns the unique prime factors.
§Examples
assert_eq![Int(24_isize).factors_prime_unique(), vec![2, 3]];
assert_eq![Int(-24_isize).factors_prime_unique(), vec![2, 3]];
Sourcepub fn factors_prime_unique_exp(self) -> Vec<(isize, u32)> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique_exp(self) -> Vec<(isize, u32)> ⓘ
alloc
only.Returns the unique prime factors with its exponent.
§Examples
assert_eq![Int(24_isize).factors_prime_unique_exp(), vec![(2, 3), (3, 1)]];
assert_eq![Int(-24_isize).factors_prime_unique_exp(), vec![(2, 3), (3, 1)]];
assert![Int(0_isize).factors_prime_unique_exp().is_empty()];
assert![Int(1_isize).factors_prime_unique_exp().is_empty()];
assert_eq![Int(7_isize).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_isize).factors_prime_count(), 4];
assert_eq![Int(-24_isize).factors_prime_count(), 4];
assert_eq![Int(0_isize).factors_prime_count(), 0];
assert_eq![Int(1_isize).factors_prime_count(), 0];
assert_eq![Int(7_isize).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_isize).factors_prime_unique_count(), 2];
assert_eq![Int(-24_isize).factors_prime_unique_count(), 2];
Sourcepub fn factors_buf(
self,
fbuf: &mut [isize],
upfbuf: &mut [isize],
) -> Result<(usize, usize)>
pub fn factors_buf( self, fbuf: &mut [isize], upfbuf: &mut [isize], ) -> 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_isize).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 [isize],
upfbuf: &mut [isize],
) -> Result<(usize, usize)>
pub fn factors_proper_buf( self, fbuf: &mut [isize], upfbuf: &mut [isize], ) -> 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_isize).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 [isize]) -> Result<usize>
pub fn factors_prime_buf(self, buffer: &mut [isize]) -> 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_isize).factors_prime_buf(&mut buf), Ok(4)];
assert_eq![buf[..4], [2, 2, 2, 3]];
assert![Int(24_isize * 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_isize).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(1_isize).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(7_isize).factors_prime_buf(&mut buf), Ok(1)];
assert_eq![buf[..1], [7]];
Sourcepub fn factors_prime_unique_buf(self, buffer: &mut [isize]) -> Result<usize>
pub fn factors_prime_unique_buf(self, buffer: &mut [isize]) -> 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_isize).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 [isize],
ebuffer: &mut [u32],
) -> Result<usize>
pub fn factors_prime_unique_exp_buf( self, fbuffer: &mut [isize], 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_isize).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_isize).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(1_isize).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(7_isize).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_isize).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_isize).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 [isize],
upfbuf: &mut [isize],
) -> Result<(usize, usize)>
pub fn factors_prime_unique_plus_buf( self, pfbuf: &mut [isize], upfbuf: &mut [isize], ) -> 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_isize).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<u8>
Available on crate feature _int_u8
only.
impl Int<u8>
_int_u8
only.§Integer factors related methods for u8
- Allocating:
- Not allocating:
Sourcepub fn factors(self) -> Vec<u8> ⓘ
Available on crate feature alloc
only.
pub fn factors(self) -> Vec<u8> ⓘ
alloc
only.Returns the factors (including 1 and self).
§Examples
assert_eq![Int(24_u8).factors(), vec![1, 2, 3, 4, 6, 8, 12, 24]];
assert![Int(0_u8).factors().is_empty()];
assert_eq![Int(1_u8).factors(), vec![1]];
assert_eq![Int(7_u8).factors(), vec![1, 7]];
Sourcepub fn factors_proper(self) -> Vec<u8> ⓘ
Available on crate feature alloc
only.
pub fn factors_proper(self) -> Vec<u8> ⓘ
alloc
only.Returns the proper factors.
§Examples
assert_eq![Int(24_u8).factors_proper(), vec![2, 3, 4, 6, 8, 12]];
assert![Int(0_u8).factors_proper().is_empty()];
assert![Int(1_u8).factors_proper().is_empty()];
assert![Int(7_u8).factors_proper().is_empty()];
Sourcepub fn factors_prime(self) -> Vec<u8> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime(self) -> Vec<u8> ⓘ
alloc
only.Returns the prime factors.
§Examples
assert_eq![Int(24_u8).factors_prime(), vec![2, 2, 2, 3]];
assert![Int(0_u8).factors_prime().is_empty()];
assert![Int(1_u8).factors_prime().is_empty()];
assert_eq![Int(7_u8).factors_prime(), vec![7]];
Sourcepub fn factors_prime_unique(self) -> Vec<u8> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique(self) -> Vec<u8> ⓘ
alloc
only.Returns the unique prime factors.
§Examples
assert_eq![Int(24_u8).factors_prime_unique(), vec![2, 3]];
Sourcepub fn factors_prime_unique_exp(self) -> Vec<(u8, u32)> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique_exp(self) -> Vec<(u8, u32)> ⓘ
alloc
only.Returns the unique prime factors with its exponent.
§Examples
assert_eq![Int(24_u8).factors_prime_unique_exp(), vec![(2, 3), (3, 1)]];
assert![Int(0_u8).factors_prime_unique_exp().is_empty()];
assert![Int(1_u8).factors_prime_unique_exp().is_empty()];
assert_eq![Int(7_u8).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_u8).factors_prime_count(), 4];
assert_eq![Int(0_u8).factors_prime_count(), 0];
assert_eq![Int(1_u8).factors_prime_count(), 0];
assert_eq![Int(7_u8).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_u8).factors_prime_unique_count(), 2];
Sourcepub fn factors_buf(
self,
fbuf: &mut [u8],
upfbuf: &mut [u8],
) -> Result<(usize, usize)>
pub fn factors_buf( self, fbuf: &mut [u8], upfbuf: &mut [u8], ) -> 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_u8).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 [u8],
upfbuf: &mut [u8],
) -> Result<(usize, usize)>
pub fn factors_proper_buf( self, fbuf: &mut [u8], upfbuf: &mut [u8], ) -> 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_u8).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 [u8]) -> Result<usize>
pub fn factors_prime_buf(self, buffer: &mut [u8]) -> 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_u8).factors_prime_buf(&mut buf), Ok(4)];
assert_eq![buf[..4], [2, 2, 2, 3]];
assert![Int(24_u8 * 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_u8).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(1_u8).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(7_u8).factors_prime_buf(&mut buf), Ok(1)];
assert_eq![buf[..1], [7]];
Sourcepub fn factors_prime_unique_buf(self, buffer: &mut [u8]) -> Result<usize>
pub fn factors_prime_unique_buf(self, buffer: &mut [u8]) -> 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_u8).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 [u8],
ebuffer: &mut [u32],
) -> Result<usize>
pub fn factors_prime_unique_exp_buf( self, fbuffer: &mut [u8], 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_u8).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_u8).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(1_u8).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(7_u8).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_u8).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_u8).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 [u8],
upfbuf: &mut [u8],
) -> Result<(usize, usize)>
pub fn factors_prime_unique_plus_buf( self, pfbuf: &mut [u8], upfbuf: &mut [u8], ) -> 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_u8).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<u16>
Available on crate feature _int_u16
only.
impl Int<u16>
_int_u16
only.§Integer factors related methods for u16
- Allocating:
- Not allocating:
Sourcepub fn factors(self) -> Vec<u16> ⓘ
Available on crate feature alloc
only.
pub fn factors(self) -> Vec<u16> ⓘ
alloc
only.Returns the factors (including 1 and self).
§Examples
assert_eq![Int(24_u16).factors(), vec![1, 2, 3, 4, 6, 8, 12, 24]];
assert![Int(0_u16).factors().is_empty()];
assert_eq![Int(1_u16).factors(), vec![1]];
assert_eq![Int(7_u16).factors(), vec![1, 7]];
Sourcepub fn factors_proper(self) -> Vec<u16> ⓘ
Available on crate feature alloc
only.
pub fn factors_proper(self) -> Vec<u16> ⓘ
alloc
only.Returns the proper factors.
§Examples
assert_eq![Int(24_u16).factors_proper(), vec![2, 3, 4, 6, 8, 12]];
assert![Int(0_u16).factors_proper().is_empty()];
assert![Int(1_u16).factors_proper().is_empty()];
assert![Int(7_u16).factors_proper().is_empty()];
Sourcepub fn factors_prime(self) -> Vec<u16> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime(self) -> Vec<u16> ⓘ
alloc
only.Returns the prime factors.
§Examples
assert_eq![Int(24_u16).factors_prime(), vec![2, 2, 2, 3]];
assert![Int(0_u16).factors_prime().is_empty()];
assert![Int(1_u16).factors_prime().is_empty()];
assert_eq![Int(7_u16).factors_prime(), vec![7]];
Sourcepub fn factors_prime_unique(self) -> Vec<u16> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique(self) -> Vec<u16> ⓘ
alloc
only.Returns the unique prime factors.
§Examples
assert_eq![Int(24_u16).factors_prime_unique(), vec![2, 3]];
Sourcepub fn factors_prime_unique_exp(self) -> Vec<(u16, u32)> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique_exp(self) -> Vec<(u16, u32)> ⓘ
alloc
only.Returns the unique prime factors with its exponent.
§Examples
assert_eq![Int(24_u16).factors_prime_unique_exp(), vec![(2, 3), (3, 1)]];
assert![Int(0_u16).factors_prime_unique_exp().is_empty()];
assert![Int(1_u16).factors_prime_unique_exp().is_empty()];
assert_eq![Int(7_u16).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_u16).factors_prime_count(), 4];
assert_eq![Int(0_u16).factors_prime_count(), 0];
assert_eq![Int(1_u16).factors_prime_count(), 0];
assert_eq![Int(7_u16).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_u16).factors_prime_unique_count(), 2];
Sourcepub fn factors_buf(
self,
fbuf: &mut [u16],
upfbuf: &mut [u16],
) -> Result<(usize, usize)>
pub fn factors_buf( self, fbuf: &mut [u16], upfbuf: &mut [u16], ) -> 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_u16).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 [u16],
upfbuf: &mut [u16],
) -> Result<(usize, usize)>
pub fn factors_proper_buf( self, fbuf: &mut [u16], upfbuf: &mut [u16], ) -> 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_u16).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 [u16]) -> Result<usize>
pub fn factors_prime_buf(self, buffer: &mut [u16]) -> 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_u16).factors_prime_buf(&mut buf), Ok(4)];
assert_eq![buf[..4], [2, 2, 2, 3]];
assert![Int(24_u16 * 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_u16).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(1_u16).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(7_u16).factors_prime_buf(&mut buf), Ok(1)];
assert_eq![buf[..1], [7]];
Sourcepub fn factors_prime_unique_buf(self, buffer: &mut [u16]) -> Result<usize>
pub fn factors_prime_unique_buf(self, buffer: &mut [u16]) -> 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_u16).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 [u16],
ebuffer: &mut [u32],
) -> Result<usize>
pub fn factors_prime_unique_exp_buf( self, fbuffer: &mut [u16], 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_u16).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_u16).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(1_u16).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(7_u16).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_u16).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_u16).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 [u16],
upfbuf: &mut [u16],
) -> Result<(usize, usize)>
pub fn factors_prime_unique_plus_buf( self, pfbuf: &mut [u16], upfbuf: &mut [u16], ) -> 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_u16).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<u64>
Available on crate feature _int_u64
only.
impl Int<u64>
_int_u64
only.§Integer factors related methods for u64
- Allocating:
- Not allocating:
Sourcepub fn factors(self) -> Vec<u64> ⓘ
Available on crate feature alloc
only.
pub fn factors(self) -> Vec<u64> ⓘ
alloc
only.Returns the factors (including 1 and self).
§Examples
assert_eq![Int(24_u64).factors(), vec![1, 2, 3, 4, 6, 8, 12, 24]];
assert![Int(0_u64).factors().is_empty()];
assert_eq![Int(1_u64).factors(), vec![1]];
assert_eq![Int(7_u64).factors(), vec![1, 7]];
Sourcepub fn factors_proper(self) -> Vec<u64> ⓘ
Available on crate feature alloc
only.
pub fn factors_proper(self) -> Vec<u64> ⓘ
alloc
only.Returns the proper factors.
§Examples
assert_eq![Int(24_u64).factors_proper(), vec![2, 3, 4, 6, 8, 12]];
assert![Int(0_u64).factors_proper().is_empty()];
assert![Int(1_u64).factors_proper().is_empty()];
assert![Int(7_u64).factors_proper().is_empty()];
Sourcepub fn factors_prime(self) -> Vec<u64> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime(self) -> Vec<u64> ⓘ
alloc
only.Returns the prime factors.
§Examples
assert_eq![Int(24_u64).factors_prime(), vec![2, 2, 2, 3]];
assert![Int(0_u64).factors_prime().is_empty()];
assert![Int(1_u64).factors_prime().is_empty()];
assert_eq![Int(7_u64).factors_prime(), vec![7]];
Sourcepub fn factors_prime_unique(self) -> Vec<u64> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique(self) -> Vec<u64> ⓘ
alloc
only.Returns the unique prime factors.
§Examples
assert_eq![Int(24_u64).factors_prime_unique(), vec![2, 3]];
Sourcepub fn factors_prime_unique_exp(self) -> Vec<(u64, u32)> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique_exp(self) -> Vec<(u64, u32)> ⓘ
alloc
only.Returns the unique prime factors with its exponent.
§Examples
assert_eq![Int(24_u64).factors_prime_unique_exp(), vec![(2, 3), (3, 1)]];
assert![Int(0_u64).factors_prime_unique_exp().is_empty()];
assert![Int(1_u64).factors_prime_unique_exp().is_empty()];
assert_eq![Int(7_u64).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_u64).factors_prime_count(), 4];
assert_eq![Int(0_u64).factors_prime_count(), 0];
assert_eq![Int(1_u64).factors_prime_count(), 0];
assert_eq![Int(7_u64).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_u64).factors_prime_unique_count(), 2];
Sourcepub fn factors_buf(
self,
fbuf: &mut [u64],
upfbuf: &mut [u64],
) -> Result<(usize, usize)>
pub fn factors_buf( self, fbuf: &mut [u64], upfbuf: &mut [u64], ) -> 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_u64).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 [u64],
upfbuf: &mut [u64],
) -> Result<(usize, usize)>
pub fn factors_proper_buf( self, fbuf: &mut [u64], upfbuf: &mut [u64], ) -> 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_u64).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 [u64]) -> Result<usize>
pub fn factors_prime_buf(self, buffer: &mut [u64]) -> 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_u64).factors_prime_buf(&mut buf), Ok(4)];
assert_eq![buf[..4], [2, 2, 2, 3]];
assert![Int(24_u64 * 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_u64).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(1_u64).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(7_u64).factors_prime_buf(&mut buf), Ok(1)];
assert_eq![buf[..1], [7]];
Sourcepub fn factors_prime_unique_buf(self, buffer: &mut [u64]) -> Result<usize>
pub fn factors_prime_unique_buf(self, buffer: &mut [u64]) -> 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_u64).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 [u64],
ebuffer: &mut [u32],
) -> Result<usize>
pub fn factors_prime_unique_exp_buf( self, fbuffer: &mut [u64], 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_u64).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_u64).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(1_u64).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(7_u64).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_u64).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_u64).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 [u64],
upfbuf: &mut [u64],
) -> Result<(usize, usize)>
pub fn factors_prime_unique_plus_buf( self, pfbuf: &mut [u64], upfbuf: &mut [u64], ) -> 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_u64).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<u128>
Available on crate feature _int_u128
only.
impl Int<u128>
_int_u128
only.§Integer factors related methods for u128
- Allocating:
- Not allocating:
Sourcepub fn factors(self) -> Vec<u128> ⓘ
Available on crate feature alloc
only.
pub fn factors(self) -> Vec<u128> ⓘ
alloc
only.Returns the factors (including 1 and self).
§Examples
assert_eq![Int(24_u128).factors(), vec![1, 2, 3, 4, 6, 8, 12, 24]];
assert![Int(0_u128).factors().is_empty()];
assert_eq![Int(1_u128).factors(), vec![1]];
assert_eq![Int(7_u128).factors(), vec![1, 7]];
Sourcepub fn factors_proper(self) -> Vec<u128> ⓘ
Available on crate feature alloc
only.
pub fn factors_proper(self) -> Vec<u128> ⓘ
alloc
only.Returns the proper factors.
§Examples
assert_eq![Int(24_u128).factors_proper(), vec![2, 3, 4, 6, 8, 12]];
assert![Int(0_u128).factors_proper().is_empty()];
assert![Int(1_u128).factors_proper().is_empty()];
assert![Int(7_u128).factors_proper().is_empty()];
Sourcepub fn factors_prime(self) -> Vec<u128> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime(self) -> Vec<u128> ⓘ
alloc
only.Returns the prime factors.
§Examples
assert_eq![Int(24_u128).factors_prime(), vec![2, 2, 2, 3]];
assert![Int(0_u128).factors_prime().is_empty()];
assert![Int(1_u128).factors_prime().is_empty()];
assert_eq![Int(7_u128).factors_prime(), vec![7]];
Sourcepub fn factors_prime_unique(self) -> Vec<u128> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique(self) -> Vec<u128> ⓘ
alloc
only.Returns the unique prime factors.
§Examples
assert_eq![Int(24_u128).factors_prime_unique(), vec![2, 3]];
Sourcepub fn factors_prime_unique_exp(self) -> Vec<(u128, u32)> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique_exp(self) -> Vec<(u128, u32)> ⓘ
alloc
only.Returns the unique prime factors with its exponent.
§Examples
assert_eq![Int(24_u128).factors_prime_unique_exp(), vec![(2, 3), (3, 1)]];
assert![Int(0_u128).factors_prime_unique_exp().is_empty()];
assert![Int(1_u128).factors_prime_unique_exp().is_empty()];
assert_eq![Int(7_u128).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_u128).factors_prime_count(), 4];
assert_eq![Int(0_u128).factors_prime_count(), 0];
assert_eq![Int(1_u128).factors_prime_count(), 0];
assert_eq![Int(7_u128).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_u128).factors_prime_unique_count(), 2];
Sourcepub fn factors_buf(
self,
fbuf: &mut [u128],
upfbuf: &mut [u128],
) -> Result<(usize, usize)>
pub fn factors_buf( self, fbuf: &mut [u128], upfbuf: &mut [u128], ) -> 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_u128).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 [u128],
upfbuf: &mut [u128],
) -> Result<(usize, usize)>
pub fn factors_proper_buf( self, fbuf: &mut [u128], upfbuf: &mut [u128], ) -> 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_u128).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 [u128]) -> Result<usize>
pub fn factors_prime_buf(self, buffer: &mut [u128]) -> 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_u128).factors_prime_buf(&mut buf), Ok(4)];
assert_eq![buf[..4], [2, 2, 2, 3]];
assert![Int(24_u128 * 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_u128).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(1_u128).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(7_u128).factors_prime_buf(&mut buf), Ok(1)];
assert_eq![buf[..1], [7]];
Sourcepub fn factors_prime_unique_buf(self, buffer: &mut [u128]) -> Result<usize>
pub fn factors_prime_unique_buf(self, buffer: &mut [u128]) -> 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_u128).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 [u128],
ebuffer: &mut [u32],
) -> Result<usize>
pub fn factors_prime_unique_exp_buf( self, fbuffer: &mut [u128], 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_u128).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_u128).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(1_u128).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(7_u128).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_u128).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_u128).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 [u128],
upfbuf: &mut [u128],
) -> Result<(usize, usize)>
pub fn factors_prime_unique_plus_buf( self, pfbuf: &mut [u128], upfbuf: &mut [u128], ) -> 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_u128).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<usize>
Available on crate feature _int_usize
only.
impl Int<usize>
_int_usize
only.§Integer factors related methods for usize
- Allocating:
- Not allocating:
Sourcepub fn factors(self) -> Vec<usize> ⓘ
Available on crate feature alloc
only.
pub fn factors(self) -> Vec<usize> ⓘ
alloc
only.Returns the factors (including 1 and self).
§Examples
assert_eq![Int(24_usize).factors(), vec![1, 2, 3, 4, 6, 8, 12, 24]];
assert![Int(0_usize).factors().is_empty()];
assert_eq![Int(1_usize).factors(), vec![1]];
assert_eq![Int(7_usize).factors(), vec![1, 7]];
Sourcepub fn factors_proper(self) -> Vec<usize> ⓘ
Available on crate feature alloc
only.
pub fn factors_proper(self) -> Vec<usize> ⓘ
alloc
only.Returns the proper factors.
§Examples
assert_eq![Int(24_usize).factors_proper(), vec![2, 3, 4, 6, 8, 12]];
assert![Int(0_usize).factors_proper().is_empty()];
assert![Int(1_usize).factors_proper().is_empty()];
assert![Int(7_usize).factors_proper().is_empty()];
Sourcepub fn factors_prime(self) -> Vec<usize> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime(self) -> Vec<usize> ⓘ
alloc
only.Returns the prime factors.
§Examples
assert_eq![Int(24_usize).factors_prime(), vec![2, 2, 2, 3]];
assert![Int(0_usize).factors_prime().is_empty()];
assert![Int(1_usize).factors_prime().is_empty()];
assert_eq![Int(7_usize).factors_prime(), vec![7]];
Sourcepub fn factors_prime_unique(self) -> Vec<usize> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique(self) -> Vec<usize> ⓘ
alloc
only.Returns the unique prime factors.
§Examples
assert_eq![Int(24_usize).factors_prime_unique(), vec![2, 3]];
Sourcepub fn factors_prime_unique_exp(self) -> Vec<(usize, u32)> ⓘ
Available on crate feature alloc
only.
pub fn factors_prime_unique_exp(self) -> Vec<(usize, u32)> ⓘ
alloc
only.Returns the unique prime factors with its exponent.
§Examples
assert_eq![Int(24_usize).factors_prime_unique_exp(), vec![(2, 3), (3, 1)]];
assert![Int(0_usize).factors_prime_unique_exp().is_empty()];
assert![Int(1_usize).factors_prime_unique_exp().is_empty()];
assert_eq![Int(7_usize).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_usize).factors_prime_count(), 4];
assert_eq![Int(0_usize).factors_prime_count(), 0];
assert_eq![Int(1_usize).factors_prime_count(), 0];
assert_eq![Int(7_usize).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_usize).factors_prime_unique_count(), 2];
Sourcepub fn factors_buf(
self,
fbuf: &mut [usize],
upfbuf: &mut [usize],
) -> Result<(usize, usize)>
pub fn factors_buf( self, fbuf: &mut [usize], upfbuf: &mut [usize], ) -> 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_usize).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 [usize],
upfbuf: &mut [usize],
) -> Result<(usize, usize)>
pub fn factors_proper_buf( self, fbuf: &mut [usize], upfbuf: &mut [usize], ) -> 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_usize).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 [usize]) -> Result<usize>
pub fn factors_prime_buf(self, buffer: &mut [usize]) -> 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_usize).factors_prime_buf(&mut buf), Ok(4)];
assert_eq![buf[..4], [2, 2, 2, 3]];
assert![Int(24_usize * 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_usize).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(1_usize).factors_prime_buf(&mut buf), Ok(0)];
assert_eq![Int(7_usize).factors_prime_buf(&mut buf), Ok(1)];
assert_eq![buf[..1], [7]];
Sourcepub fn factors_prime_unique_buf(self, buffer: &mut [usize]) -> Result<usize>
pub fn factors_prime_unique_buf(self, buffer: &mut [usize]) -> 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_usize).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 [usize],
ebuffer: &mut [u32],
) -> Result<usize>
pub fn factors_prime_unique_exp_buf( self, fbuffer: &mut [usize], 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_usize).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_usize).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(1_usize).factors_prime_unique_exp_buf(&mut fbuf, &mut ebuf), Ok(0)];
assert_eq![Int(7_usize).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_usize).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_usize).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 [usize],
upfbuf: &mut [usize],
) -> Result<(usize, usize)>
pub fn factors_prime_unique_plus_buf( self, pfbuf: &mut [usize], upfbuf: &mut [usize], ) -> 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_usize).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<i8>
Available on crate feature _int_i8
only.
impl Int<i8>
_int_i8
only.§Integer modulo related methods for i8
- 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: i8) -> Result<Int<i8>>
pub const fn modulo(self, modulus: i8) -> Result<Int<i8>>
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 i16
.
§Errors
Returns NonZeroRequired
if modulus == 0
, and for i128
it could also return Overflow
.
§Examples
let m = 3;
assert_eq![Int(-4_i8).modulo(m)?, 2];
assert_eq![Int(-3_i8).modulo(m)?, 0];
assert_eq![Int(-2_i8).modulo(m)?, 1];
assert_eq![Int(-1_i8).modulo(m)?, 2];
assert_eq![Int( 0_i8).modulo(m)?, 0];
assert_eq![Int( 1_i8).modulo(m)?, 1];
assert_eq![Int( 2_i8).modulo(m)?, 2];
assert_eq![Int( 3_i8).modulo(m)?, 0];
assert_eq![Int( 4_i8).modulo(m)?, 1];
assert_eq![Int(i8::MAX).modulo(i8::MIN)?, i8::MAX];
assert_eq![Int(i8::MIN).modulo(i8::MAX)?, i8::MAX - 1];
assert_eq![Int(i8::MIN).modulo(i8::MIN)?, 0];
assert![Int(i64::MIN).modulo(-1).is_ok()];
assert_eq![Int(1_i8).modulo(0), Err(NumError::NonZeroRequired)];
assert_eq![Int(i128::MIN).modulo(-1), Err(NumError::Overflow(None))];
Sourcepub const fn modulo_unchecked(self, modulus: i8) -> Int<i8>
pub const fn modulo_unchecked(self, modulus: i8) -> Int<i8>
Computes the non-negative modulo of self
over |modulus
|, unchecked version.
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 i16
.
§Panics
Panics if modulus == 0
, and for i128
it could also panic on overflow.
§Examples
let m = 3;
assert_eq![Int(-4_i8).modulo_unchecked(m), 2];
assert_eq![Int(-3_i8).modulo_unchecked(m), 0];
assert_eq![Int(-2_i8).modulo_unchecked(m), 1];
assert_eq![Int(-1_i8).modulo_unchecked(m), 2];
assert_eq![Int( 0_i8).modulo_unchecked(m), 0];
assert_eq![Int( 1_i8).modulo_unchecked(m), 1];
assert_eq![Int( 2_i8).modulo_unchecked(m), 2];
assert_eq![Int( 3_i8).modulo_unchecked(m), 0];
assert_eq![Int( 4_i8).modulo_unchecked(m), 1];
assert_eq![Int( 4_i8).modulo_unchecked(-m), 1];
assert_eq![Int(i8::MAX).modulo_unchecked(i8::MAX - 1), 1];
assert_eq![Int(i8::MAX).modulo_unchecked(i8::MAX), 0];
assert_eq![Int(i8::MAX).modulo_unchecked(i8::MIN), i8::MAX];
assert_eq![Int(i8::MIN).modulo_unchecked(i8::MAX), i8::MAX - 1];
assert_eq![Int(i8::MIN).modulo_unchecked(i8::MIN), 0];
assert_eq![Int(i64::MIN).modulo_unchecked(-1), 0];
let _ = Int(i128::MIN).modulo_unchecked(-1); // i128 could overflow
let _ = Int(1_i8).modulo_unchecked(0); // panics if modulus == 0
Sourcepub const fn modulo_cycles(
self,
modulus: i8,
) -> Result<ValueQuant<Int<i8>, Int<i8>>>
pub const fn modulo_cycles( self, modulus: i8, ) -> Result<ValueQuant<Int<i8>, Int<i8>>>
Computes the non-negative modulo of self
over |modulus
|,
and the number of cycles the result is reduced.
Performs operations internally as i16
.
§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_i8).modulo_cycles(m)?, (0, 1)];
assert_eq![Int(-2_i8).modulo_cycles(m)?, (1, 0)];
assert_eq![Int(-1_i8).modulo_cycles(m)?, (2, 0)];
assert_eq![Int( 0_i8).modulo_cycles(m)?, (0, 0)];
assert_eq![Int( 1_i8).modulo_cycles(m)?, (1, 0)];
assert_eq![Int( 2_i8).modulo_cycles(m)?, (2, 0)];
assert_eq![Int( 3_i8).modulo_cycles(m)?, (0, 1)];
Sourcepub const fn modulo_cycles_unchecked(
self,
modulus: i8,
) -> ValueQuant<Int<i8>, Int<i8>>
pub const fn modulo_cycles_unchecked( self, modulus: i8, ) -> ValueQuant<Int<i8>, Int<i8>>
Sourcepub const fn modulo_add(self, other: i8, modulus: i8) -> Result<Int<i8>>
pub const fn modulo_add(self, other: i8, modulus: i8) -> Result<Int<i8>>
Computes the non-negative modulo of self + other
over |modulus
|.
Performs operations internally as i16
.
§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_i8).modulo_add(-4, m)?, 0];
assert_eq![Int(4_i8).modulo_add(-3, m)?, 1];
assert_eq![Int(4_i8).modulo_add(-2, m)?, 2];
assert_eq![Int(4_i8).modulo_add(-1, m)?, 0];
assert_eq![Int(4_i8).modulo_add( 0, m)?, 1];
assert_eq![Int(4_i8).modulo_add( 1, m)?, 2];
assert_eq![Int(4_i8).modulo_add( 2, m)?, 0];
assert_eq![Int(4_i8).modulo_add( 3, m)?, 1];
assert_eq![Int(4_i8).modulo_add( 4, m)?, 2];
Sourcepub const fn modulo_add_cycles(
self,
other: i8,
modulus: i8,
) -> Result<ValueQuant<Int<i8>, Int<i8>>>
pub const fn modulo_add_cycles( self, other: i8, modulus: i8, ) -> Result<ValueQuant<Int<i8>, Int<i8>>>
Computes the non-negative modulo of self + other
over |modulus
|,
and the number of cycles the result is reduced.
Performs operations internally as i16
.
§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_i8).modulo_add_cycles(-4, m)?, (0, 0)];
assert_eq![Int(4_i8).modulo_add_cycles(-3, m)?, (1, 0)];
assert_eq![Int(4_i8).modulo_add_cycles(-2, m)?, (2, 0)];
assert_eq![Int(4_i8).modulo_add_cycles(-1, m)?, (0, 1)];
assert_eq![Int(4_i8).modulo_add_cycles( 0, m)?, (1, 1)];
assert_eq![Int(4_i8).modulo_add_cycles( 1, m)?, (2, 1)];
assert_eq![Int(4_i8).modulo_add_cycles( 2, m)?, (0, 2)];
assert_eq![Int(4_i8).modulo_add_cycles( 3, m)?, (1, 2)];
assert_eq![Int(4_i8).modulo_add_cycles( 4, m)?, (2, 2)];
Sourcepub const fn modulo_add_cycles_unchecked(
self,
other: i8,
modulus: i8,
) -> ValueQuant<Int<i8>, Int<i8>>
pub const fn modulo_add_cycles_unchecked( self, other: i8, modulus: i8, ) -> ValueQuant<Int<i8>, Int<i8>>
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 i16
.
§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: i8) -> Result<Int<i8>>
pub const fn modulo_add_inv(self, modulus: i8) -> Result<Int<i8>>
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_i8).modulo_add_inv(m)?, 1];
assert_eq![Int(-3_i8).modulo_add_inv(m)?, 0];
assert_eq![Int(-2_i8).modulo_add_inv(m)?, 2];
assert_eq![Int(-1_i8).modulo_add_inv(m)?, 1];
assert_eq![Int( 0_i8).modulo_add_inv(m)?, 0];
assert_eq![Int( 1_i8).modulo_add_inv(m)?, 2];
assert_eq![Int( 2_i8).modulo_add_inv(m)?, 1];
assert_eq![Int( 3_i8).modulo_add_inv(m)?, 0];
Sourcepub const fn modulo_add_inv_unchecked(self, modulus: i8) -> Int<i8>
pub const fn modulo_add_inv_unchecked(self, modulus: i8) -> Int<i8>
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: i8, modulus: i8) -> Result<Int<i8>>
pub const fn modulo_sub(self, other: i8, modulus: i8) -> Result<Int<i8>>
Computes the modulo of self - other
over |modulus
|.
Performs operations internally as i16
.
§Features
Uses unsafe_hint
for performance optimizations with upcasted arithmetic.
§Errors
Returns NonZeroRequired
if modulus == 0
.
§Examples
let m = 3;
assert_eq![Int(4_i8).modulo_sub(-4, m)?, 2];
assert_eq![Int(4_i8).modulo_sub(-3, m)?, 1];
assert_eq![Int(4_i8).modulo_sub(-2, m)?, 0];
assert_eq![Int(4_i8).modulo_sub(-1, m)?, 2];
assert_eq![Int(4_i8).modulo_sub( 0, m)?, 1];
assert_eq![Int(4_i8).modulo_sub( 1, m)?, 0];
assert_eq![Int(4_i8).modulo_sub( 2, m)?, 2];
assert_eq![Int(4_i8).modulo_sub( 3, m)?, 1];
assert_eq![Int(4_i8).modulo_sub( 4, m)?, 0];
Sourcepub const fn modulo_sub_cycles(
self,
other: i8,
modulus: i8,
) -> Result<ValueQuant<Int<i8>, Int<i8>>>
pub const fn modulo_sub_cycles( self, other: i8, modulus: i8, ) -> Result<ValueQuant<Int<i8>, Int<i8>>>
Computes the non-negative modulo of self - other
over |modulus
|,
and the number of cycles the result is reduced.
Performs operations internally as i16
.
§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_i8).modulo_sub_cycles(-4, m)?, (2, 2)];
assert_eq![Int(4_i8).modulo_sub_cycles(-3, m)?, (1, 2)];
assert_eq![Int(4_i8).modulo_sub_cycles(-2, m)?, (0, 2)];
assert_eq![Int(4_i8).modulo_sub_cycles(-1, m)?, (2, 1)];
assert_eq![Int(4_i8).modulo_sub_cycles( 0, m)?, (1, 1)];
assert_eq![Int(4_i8).modulo_sub_cycles( 1, m)?, (0, 1)];
assert_eq![Int(4_i8).modulo_sub_cycles( 2, m)?, (2, 0)];
assert_eq![Int(4_i8).modulo_sub_cycles( 3, m)?, (1, 0)];
assert_eq![Int(4_i8).modulo_sub_cycles( 4, m)?, (0, 0)];
Sourcepub const fn modulo_sub_cycles_unchecked(
self,
other: i8,
modulus: i8,
) -> (Int<i8>, Int<i8>) ⓘ
pub const fn modulo_sub_cycles_unchecked( self, other: i8, modulus: i8, ) -> (Int<i8>, Int<i8>) ⓘ
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 i16
.
§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: i8, modulus: i8) -> Result<Int<i8>>
pub const fn modulo_mul(self, other: i8, modulus: i8) -> Result<Int<i8>>
Computes the non-negative modulo of self + other
over |modulus
|.
Performs operations internally as i16
.
§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_i8).modulo_mul(-4, m)?, 2];
assert_eq![Int(4_i8).modulo_mul(-3, m)?, 0];
assert_eq![Int(4_i8).modulo_mul(-2, m)?, 1];
assert_eq![Int(4_i8).modulo_mul(-1, m)?, 2];
assert_eq![Int(4_i8).modulo_mul( 0, m)?, 0];
assert_eq![Int(4_i8).modulo_mul( 1, m)?, 1];
assert_eq![Int(4_i8).modulo_mul( 2, m)?, 2];
assert_eq![Int(4_i8).modulo_mul( 3, m)?, 0];
assert_eq![Int(4_i8).modulo_mul( 4, m)?, 1];
Sourcepub const fn modulo_mul_cycles(
self,
other: i8,
modulus: i8,
) -> Result<ValueQuant<Int<i8>, Int<i8>>>
pub const fn modulo_mul_cycles( self, other: i8, modulus: i8, ) -> Result<ValueQuant<Int<i8>, Int<i8>>>
Computes the non-negative modulo of self + other
over |modulus
|,
and the number of cycles the result is reduced.
Performs operations internally as i16
.
§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_i8).modulo_mul_cycles(-4, m)?, (2, 5)];
assert_eq![Int(4_i8).modulo_mul_cycles(-3, m)?, (0, 4)];
assert_eq![Int(4_i8).modulo_mul_cycles(-2, m)?, (1, 2)];
assert_eq![Int(4_i8).modulo_mul_cycles(-1, m)?, (2, 1)];
assert_eq![Int(4_i8).modulo_mul_cycles( 0, m)?, (0, 0)];
assert_eq![Int(4_i8).modulo_mul_cycles( 1, m)?, (1, 1)];
assert_eq![Int(4_i8).modulo_mul_cycles( 2, m)?, (2, 2)];
assert_eq![Int(4_i8).modulo_mul_cycles( 3, m)?, (0, 4)];
assert_eq![Int(4_i8).modulo_mul_cycles( 4, m)?, (1, 5)];
Sourcepub const fn modulo_mul_cycles_unchecked(
self,
other: i8,
modulus: i8,
) -> ValueQuant<Int<i8>, Int<i8>>
pub const fn modulo_mul_cycles_unchecked( self, other: i8, modulus: i8, ) -> ValueQuant<Int<i8>, Int<i8>>
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 i16
.
§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: i8) -> Result<Int<i8>>
pub const fn modulo_mul_inv(self, modulus: i8) -> Result<Int<i8>>
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_i8).modulo_mul_inv(m)?, 4];
assert_eq![Int(-3_i8).modulo_mul_inv(m)?, 2];
assert_eq![Int(-2_i8).modulo_mul_inv(m)?, 3];
assert_eq![Int(-1_i8).modulo_mul_inv(m)?, 1];
assert_eq![Int( 0_i8).modulo_mul_inv(m), Err(NumError::NoInverse)];
assert_eq![Int( 1_i8).modulo_mul_inv(m)?, 1];
assert_eq![Int( 2_i8).modulo_mul_inv(m)?, 3];
assert_eq![Int( 3_i8).modulo_mul_inv(m)?, 2];
assert_eq![Int( 4_i8).modulo_mul_inv(m)?, 4];
Sourcepub const fn modulo_mul_inv_unchecked(self, modulus: i8) -> Int<i8>
pub const fn modulo_mul_inv_unchecked(self, modulus: i8) -> Int<i8>
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: i8, modulus: i8) -> Result<Int<i8>>
pub const fn modulo_div(self, other: i8, modulus: i8) -> Result<Int<i8>>
Computes self / other
over |modulus
|.
Performs operations internally as i16
.
$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_i8).modulo_div(2, m)?, 1];
assert_eq![Int(-3_i8).modulo_div(2, m)?, 0];
assert_eq![Int(-2_i8).modulo_div(2, m)?, 2];
assert_eq![Int(-1_i8).modulo_div(2, m)?, 1];
assert_eq![Int( 0_i8).modulo_div(2, m)?, 0];
assert_eq![Int( 1_i8).modulo_div(2, m)?, 2];
assert_eq![Int( 2_i8).modulo_div(2, m)?, 1];
assert_eq![Int( 3_i8).modulo_div(2, m)?, 0];
assert_eq![Int( 4_i8).modulo_div(2, m)?, 2];
Source§impl Int<i16>
Available on crate feature _int_i16
only.
impl Int<i16>
_int_i16
only.§Integer modulo related methods for i16
- 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: i16) -> Result<Int<i16>>
pub const fn modulo(self, modulus: i16) -> Result<Int<i16>>
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 i32
.
§Errors
Returns NonZeroRequired
if modulus == 0
, and for i128
it could also return Overflow
.
§Examples
let m = 3;
assert_eq![Int(-4_i16).modulo(m)?, 2];
assert_eq![Int(-3_i16).modulo(m)?, 0];
assert_eq![Int(-2_i16).modulo(m)?, 1];
assert_eq![Int(-1_i16).modulo(m)?, 2];
assert_eq![Int( 0_i16).modulo(m)?, 0];
assert_eq![Int( 1_i16).modulo(m)?, 1];
assert_eq![Int( 2_i16).modulo(m)?, 2];
assert_eq![Int( 3_i16).modulo(m)?, 0];
assert_eq![Int( 4_i16).modulo(m)?, 1];
assert_eq![Int(i16::MAX).modulo(i16::MIN)?, i16::MAX];
assert_eq![Int(i16::MIN).modulo(i16::MAX)?, i16::MAX - 1];
assert_eq![Int(i16::MIN).modulo(i16::MIN)?, 0];
assert![Int(i64::MIN).modulo(-1).is_ok()];
assert_eq![Int(1_i16).modulo(0), Err(NumError::NonZeroRequired)];
assert_eq![Int(i128::MIN).modulo(-1), Err(NumError::Overflow(None))];
Sourcepub const fn modulo_unchecked(self, modulus: i16) -> Int<i16>
pub const fn modulo_unchecked(self, modulus: i16) -> Int<i16>
Computes the non-negative modulo of self
over |modulus
|, unchecked version.
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 i32
.
§Panics
Panics if modulus == 0
, and for i128
it could also panic on overflow.
§Examples
let m = 3;
assert_eq![Int(-4_i16).modulo_unchecked(m), 2];
assert_eq![Int(-3_i16).modulo_unchecked(m), 0];
assert_eq![Int(-2_i16).modulo_unchecked(m), 1];
assert_eq![Int(-1_i16).modulo_unchecked(m), 2];
assert_eq![Int( 0_i16).modulo_unchecked(m), 0];
assert_eq![Int( 1_i16).modulo_unchecked(m), 1];
assert_eq![Int( 2_i16).modulo_unchecked(m), 2];
assert_eq![Int( 3_i16).modulo_unchecked(m), 0];
assert_eq![Int( 4_i16).modulo_unchecked(m), 1];
assert_eq![Int( 4_i16).modulo_unchecked(-m), 1];
assert_eq![Int(i16::MAX).modulo_unchecked(i16::MAX - 1), 1];
assert_eq![Int(i16::MAX).modulo_unchecked(i16::MAX), 0];
assert_eq![Int(i16::MAX).modulo_unchecked(i16::MIN), i16::MAX];
assert_eq![Int(i16::MIN).modulo_unchecked(i16::MAX), i16::MAX - 1];
assert_eq![Int(i16::MIN).modulo_unchecked(i16::MIN), 0];
assert_eq![Int(i64::MIN).modulo_unchecked(-1), 0];
let _ = Int(i128::MIN).modulo_unchecked(-1); // i128 could overflow
let _ = Int(1_i16).modulo_unchecked(0); // panics if modulus == 0
Sourcepub const fn modulo_cycles(
self,
modulus: i16,
) -> Result<ValueQuant<Int<i16>, Int<i16>>>
pub const fn modulo_cycles( self, modulus: i16, ) -> Result<ValueQuant<Int<i16>, Int<i16>>>
Computes the non-negative modulo of self
over |modulus
|,
and the number of cycles the result is reduced.
Performs operations internally as i32
.
§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_i16).modulo_cycles(m)?, (0, 1)];
assert_eq![Int(-2_i16).modulo_cycles(m)?, (1, 0)];
assert_eq![Int(-1_i16).modulo_cycles(m)?, (2, 0)];
assert_eq![Int( 0_i16).modulo_cycles(m)?, (0, 0)];
assert_eq![Int( 1_i16).modulo_cycles(m)?, (1, 0)];
assert_eq![Int( 2_i16).modulo_cycles(m)?, (2, 0)];
assert_eq![Int( 3_i16).modulo_cycles(m)?, (0, 1)];
Sourcepub const fn modulo_cycles_unchecked(
self,
modulus: i16,
) -> ValueQuant<Int<i16>, Int<i16>>
pub const fn modulo_cycles_unchecked( self, modulus: i16, ) -> ValueQuant<Int<i16>, Int<i16>>
Sourcepub const fn modulo_add(self, other: i16, modulus: i16) -> Result<Int<i16>>
pub const fn modulo_add(self, other: i16, modulus: i16) -> Result<Int<i16>>
Computes the non-negative modulo of self + other
over |modulus
|.
Performs operations internally as i32
.
§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_i16).modulo_add(-4, m)?, 0];
assert_eq![Int(4_i16).modulo_add(-3, m)?, 1];
assert_eq![Int(4_i16).modulo_add(-2, m)?, 2];
assert_eq![Int(4_i16).modulo_add(-1, m)?, 0];
assert_eq![Int(4_i16).modulo_add( 0, m)?, 1];
assert_eq![Int(4_i16).modulo_add( 1, m)?, 2];
assert_eq![Int(4_i16).modulo_add( 2, m)?, 0];
assert_eq![Int(4_i16).modulo_add( 3, m)?, 1];
assert_eq![Int(4_i16).modulo_add( 4, m)?, 2];
Sourcepub const fn modulo_add_cycles(
self,
other: i16,
modulus: i16,
) -> Result<ValueQuant<Int<i16>, Int<i16>>>
pub const fn modulo_add_cycles( self, other: i16, modulus: i16, ) -> Result<ValueQuant<Int<i16>, Int<i16>>>
Computes the non-negative modulo of self + other
over |modulus
|,
and the number of cycles the result is reduced.
Performs operations internally as i32
.
§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_i16).modulo_add_cycles(-4, m)?, (0, 0)];
assert_eq![Int(4_i16).modulo_add_cycles(-3, m)?, (1, 0)];
assert_eq![Int(4_i16).modulo_add_cycles(-2, m)?, (2, 0)];
assert_eq![Int(4_i16).modulo_add_cycles(-1, m)?, (0, 1)];
assert_eq![Int(4_i16).modulo_add_cycles( 0, m)?, (1, 1)];
assert_eq![Int(4_i16).modulo_add_cycles( 1, m)?, (2, 1)];
assert_eq![Int(4_i16).modulo_add_cycles( 2, m)?, (0, 2)];
assert_eq![Int(4_i16).modulo_add_cycles( 3, m)?, (1, 2)];
assert_eq![Int(4_i16).modulo_add_cycles( 4, m)?, (2, 2)];
Sourcepub const fn modulo_add_cycles_unchecked(
self,
other: i16,
modulus: i16,
) -> ValueQuant<Int<i16>, Int<i16>>
pub const fn modulo_add_cycles_unchecked( self, other: i16, modulus: i16, ) -> ValueQuant<Int<i16>, Int<i16>>
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 i32
.
§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: i16) -> Result<Int<i16>>
pub const fn modulo_add_inv(self, modulus: i16) -> Result<Int<i16>>
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_i16).modulo_add_inv(m)?, 1];
assert_eq![Int(-3_i16).modulo_add_inv(m)?, 0];
assert_eq![Int(-2_i16).modulo_add_inv(m)?, 2];
assert_eq![Int(-1_i16).modulo_add_inv(m)?, 1];
assert_eq![Int( 0_i16).modulo_add_inv(m)?, 0];
assert_eq![Int( 1_i16).modulo_add_inv(m)?, 2];
assert_eq![Int( 2_i16).modulo_add_inv(m)?, 1];
assert_eq![Int( 3_i16).modulo_add_inv(m)?, 0];
Sourcepub const fn modulo_add_inv_unchecked(self, modulus: i16) -> Int<i16>
pub const fn modulo_add_inv_unchecked(self, modulus: i16) -> Int<i16>
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: i16, modulus: i16) -> Result<Int<i16>>
pub const fn modulo_sub(self, other: i16, modulus: i16) -> Result<Int<i16>>
Computes the modulo of self - other
over |modulus
|.
Performs operations internally as i32
.
§Features
Uses unsafe_hint
for performance optimizations with upcasted arithmetic.
§Errors
Returns NonZeroRequired
if modulus == 0
.
§Examples
let m = 3;
assert_eq![Int(4_i16).modulo_sub(-4, m)?, 2];
assert_eq![Int(4_i16).modulo_sub(-3, m)?, 1];
assert_eq![Int(4_i16).modulo_sub(-2, m)?, 0];
assert_eq![Int(4_i16).modulo_sub(-1, m)?, 2];
assert_eq![Int(4_i16).modulo_sub( 0, m)?, 1];
assert_eq![Int(4_i16).modulo_sub( 1, m)?, 0];
assert_eq![Int(4_i16).modulo_sub( 2, m)?, 2];
assert_eq![Int(4_i16).modulo_sub( 3, m)?, 1];
assert_eq![Int(4_i16).modulo_sub( 4, m)?, 0];
Sourcepub const fn modulo_sub_cycles(
self,
other: i16,
modulus: i16,
) -> Result<ValueQuant<Int<i16>, Int<i16>>>
pub const fn modulo_sub_cycles( self, other: i16, modulus: i16, ) -> Result<ValueQuant<Int<i16>, Int<i16>>>
Computes the non-negative modulo of self - other
over |modulus
|,
and the number of cycles the result is reduced.
Performs operations internally as i32
.
§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_i16).modulo_sub_cycles(-4, m)?, (2, 2)];
assert_eq![Int(4_i16).modulo_sub_cycles(-3, m)?, (1, 2)];
assert_eq![Int(4_i16).modulo_sub_cycles(-2, m)?, (0, 2)];
assert_eq![Int(4_i16).modulo_sub_cycles(-1, m)?, (2, 1)];
assert_eq![Int(4_i16).modulo_sub_cycles( 0, m)?, (1, 1)];
assert_eq![Int(4_i16).modulo_sub_cycles( 1, m)?, (0, 1)];
assert_eq![Int(4_i16).modulo_sub_cycles( 2, m)?, (2, 0)];
assert_eq![Int(4_i16).modulo_sub_cycles( 3, m)?, (1, 0)];
assert_eq![Int(4_i16).modulo_sub_cycles( 4, m)?, (0, 0)];
Sourcepub const fn modulo_sub_cycles_unchecked(
self,
other: i16,
modulus: i16,
) -> (Int<i16>, Int<i16>) ⓘ
pub const fn modulo_sub_cycles_unchecked( self, other: i16, modulus: i16, ) -> (Int<i16>, Int<i16>) ⓘ
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 i32
.
§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: i16, modulus: i16) -> Result<Int<i16>>
pub const fn modulo_mul(self, other: i16, modulus: i16) -> Result<Int<i16>>
Computes the non-negative modulo of self + other
over |modulus
|.
Performs operations internally as i32
.
§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_i16).modulo_mul(-4, m)?, 2];
assert_eq![Int(4_i16).modulo_mul(-3, m)?, 0];
assert_eq![Int(4_i16).modulo_mul(-2, m)?, 1];
assert_eq![Int(4_i16).modulo_mul(-1, m)?, 2];
assert_eq![Int(4_i16).modulo_mul( 0, m)?, 0];
assert_eq![Int(4_i16).modulo_mul( 1, m)?, 1];
assert_eq![Int(4_i16).modulo_mul( 2, m)?, 2];
assert_eq![Int(4_i16).modulo_mul( 3, m)?, 0];
assert_eq![Int(4_i16).modulo_mul( 4, m)?, 1];
Sourcepub const fn modulo_mul_cycles(
self,
other: i16,
modulus: i16,
) -> Result<ValueQuant<Int<i16>, Int<i16>>>
pub const fn modulo_mul_cycles( self, other: i16, modulus: i16, ) -> Result<ValueQuant<Int<i16>, Int<i16>>>
Computes the non-negative modulo of self + other
over |modulus
|,
and the number of cycles the result is reduced.
Performs operations internally as i32
.
§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_i16).modulo_mul_cycles(-4, m)?, (2, 5)];
assert_eq![Int(4_i16).modulo_mul_cycles(-3, m)?, (0, 4)];
assert_eq![Int(4_i16).modulo_mul_cycles(-2, m)?, (1, 2)];
assert_eq![Int(4_i16).modulo_mul_cycles(-1, m)?, (2, 1)];
assert_eq![Int(4_i16).modulo_mul_cycles( 0, m)?, (0, 0)];
assert_eq![Int(4_i16).modulo_mul_cycles( 1, m)?, (1, 1)];
assert_eq![Int(4_i16).modulo_mul_cycles( 2, m)?, (2, 2)];
assert_eq![Int(4_i16).modulo_mul_cycles( 3, m)?, (0, 4)];
assert_eq![Int(4_i16).modulo_mul_cycles( 4, m)?, (1, 5)];
Sourcepub const fn modulo_mul_cycles_unchecked(
self,
other: i16,
modulus: i16,
) -> ValueQuant<Int<i16>, Int<i16>>
pub const fn modulo_mul_cycles_unchecked( self, other: i16, modulus: i16, ) -> ValueQuant<Int<i16>, Int<i16>>
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 i32
.
§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: i16) -> Result<Int<i16>>
pub const fn modulo_mul_inv(self, modulus: i16) -> Result<Int<i16>>
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_i16).modulo_mul_inv(m)?, 4];
assert_eq![Int(-3_i16).modulo_mul_inv(m)?, 2];
assert_eq![Int(-2_i16).modulo_mul_inv(m)?, 3];
assert_eq![Int(-1_i16).modulo_mul_inv(m)?, 1];
assert_eq![Int( 0_i16).modulo_mul_inv(m), Err(NumError::NoInverse)];
assert_eq![Int( 1_i16).modulo_mul_inv(m)?, 1];
assert_eq![Int( 2_i16).modulo_mul_inv(m)?, 3];
assert_eq![Int( 3_i16).modulo_mul_inv(m)?, 2];
assert_eq![Int( 4_i16).modulo_mul_inv(m)?, 4];
Sourcepub const fn modulo_mul_inv_unchecked(self, modulus: i16) -> Int<i16>
pub const fn modulo_mul_inv_unchecked(self, modulus: i16) -> Int<i16>
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: i16, modulus: i16) -> Result<Int<i16>>
pub const fn modulo_div(self, other: i16, modulus: i16) -> Result<Int<i16>>
Computes self / other
over |modulus
|.
Performs operations internally as i32
.
$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_i16).modulo_div(2, m)?, 1];
assert_eq![Int(-3_i16).modulo_div(2, m)?, 0];
assert_eq![Int(-2_i16).modulo_div(2, m)?, 2];
assert_eq![Int(-1_i16).modulo_div(2, m)?, 1];
assert_eq![Int( 0_i16).modulo_div(2, m)?, 0];
assert_eq![Int( 1_i16).modulo_div(2, m)?, 2];
assert_eq![Int( 2_i16).modulo_div(2, m)?, 1];
assert_eq![Int( 3_i16).modulo_div(2, m)?, 0];
assert_eq![Int( 4_i16).modulo_div(2, m)?, 2];
Source§impl Int<i32>
Available on crate feature _int_i32
only.
impl Int<i32>
_int_i32
only.§Integer modulo related methods for i32
- 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: i32) -> Result<Int<i32>>
pub const fn modulo(self, modulus: i32) -> Result<Int<i32>>
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 i64
.
§Errors
Returns NonZeroRequired
if modulus == 0
, and for i128
it could also return Overflow
.
§Examples
let m = 3;
assert_eq![Int(-4_i32).modulo(m)?, 2];
assert_eq![Int(-3_i32).modulo(m)?, 0];
assert_eq![Int(-2_i32).modulo(m)?, 1];
assert_eq![Int(-1_i32).modulo(m)?, 2];
assert_eq![Int( 0_i32).modulo(m)?, 0];
assert_eq![Int( 1_i32).modulo(m)?, 1];
assert_eq![Int( 2_i32).modulo(m)?, 2];
assert_eq![Int( 3_i32).modulo(m)?, 0];
assert_eq![Int( 4_i32).modulo(m)?, 1];
assert_eq![Int(i32::MAX).modulo(i32::MIN)?, i32::MAX];
assert_eq![Int(i32::MIN).modulo(i32::MAX)?, i32::MAX - 1];
assert_eq![Int(i32::MIN).modulo(i32::MIN)?, 0];
assert![Int(i64::MIN).modulo(-1).is_ok()];
assert_eq![Int(1_i32).modulo(0), Err(NumError::NonZeroRequired)];
assert_eq![Int(i128::MIN).modulo(-1), Err(NumError::Overflow(None))];
Sourcepub const fn modulo_unchecked(self, modulus: i32) -> Int<i32>
pub const fn modulo_unchecked(self, modulus: i32) -> Int<i32>
Computes the non-negative modulo of self
over |modulus
|, unchecked version.
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 i64
.
§Panics
Panics if modulus == 0
, and for i128
it could also panic on overflow.
§Examples
let m = 3;
assert_eq![Int(-4_i32).modulo_unchecked(m), 2];
assert_eq![Int(-3_i32).modulo_unchecked(m), 0];
assert_eq![Int(-2_i32).modulo_unchecked(m), 1];
assert_eq![Int(-1_i32).modulo_unchecked(m), 2];
assert_eq![Int( 0_i32).modulo_unchecked(m), 0];
assert_eq![Int( 1_i32).modulo_unchecked(m), 1];
assert_eq![Int( 2_i32).modulo_unchecked(m), 2];
assert_eq![Int( 3_i32).modulo_unchecked(m), 0];
assert_eq![Int( 4_i32).modulo_unchecked(m), 1];
assert_eq![Int( 4_i32).modulo_unchecked(-m), 1];
assert_eq![Int(i32::MAX).modulo_unchecked(i32::MAX - 1), 1];
assert_eq![Int(i32::MAX).modulo_unchecked(i32::MAX), 0];
assert_eq![Int(i32::MAX).modulo_unchecked(i32::MIN), i32::MAX];
assert_eq![Int(i32::MIN).modulo_unchecked(i32::MAX), i32::MAX - 1];
assert_eq![Int(i32::MIN).modulo_unchecked(i32::MIN), 0];
assert_eq![Int(i64::MIN).modulo_unchecked(-1), 0];
let _ = Int(i128::MIN).modulo_unchecked(-1); // i128 could overflow
let _ = Int(1_i32).modulo_unchecked(0); // panics if modulus == 0
Sourcepub const fn modulo_cycles(
self,
modulus: i32,
) -> Result<ValueQuant<Int<i32>, Int<i32>>>
pub const fn modulo_cycles( self, modulus: i32, ) -> Result<ValueQuant<Int<i32>, Int<i32>>>
Computes the non-negative modulo of self
over |modulus
|,
and the number of cycles the result is reduced.
Performs operations internally as i64
.
§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_i32).modulo_cycles(m)?, (0, 1)];
assert_eq![Int(-2_i32).modulo_cycles(m)?, (1, 0)];
assert_eq![Int(-1_i32).modulo_cycles(m)?, (2, 0)];
assert_eq![Int( 0_i32).modulo_cycles(m)?, (0, 0)];
assert_eq![Int( 1_i32).modulo_cycles(m)?, (1, 0)];
assert_eq![Int( 2_i32).modulo_cycles(m)?, (2, 0)];
assert_eq![Int( 3_i32).modulo_cycles(m)?, (0, 1)];
Sourcepub const fn modulo_cycles_unchecked(
self,
modulus: i32,
) -> ValueQuant<Int<i32>, Int<i32>>
pub const fn modulo_cycles_unchecked( self, modulus: i32, ) -> ValueQuant<Int<i32>, Int<i32>>
Sourcepub const fn modulo_add(self, other: i32, modulus: i32) -> Result<Int<i32>>
pub const fn modulo_add(self, other: i32, modulus: i32) -> Result<Int<i32>>
Computes the non-negative modulo of self + other
over |modulus
|.
Performs operations internally as i64
.
§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_i32).modulo_add(-4, m)?, 0];
assert_eq![Int(4_i32).modulo_add(-3, m)?, 1];
assert_eq![Int(4_i32).modulo_add(-2, m)?, 2];
assert_eq![Int(4_i32).modulo_add(-1, m)?, 0];
assert_eq![Int(4_i32).modulo_add( 0, m)?, 1];
assert_eq![Int(4_i32).modulo_add( 1, m)?, 2];
assert_eq![Int(4_i32).modulo_add( 2, m)?, 0];
assert_eq![Int(4_i32).modulo_add( 3, m)?, 1];
assert_eq![Int(4_i32).modulo_add( 4, m)?, 2];
Sourcepub const fn modulo_add_cycles(
self,
other: i32,
modulus: i32,
) -> Result<ValueQuant<Int<i32>, Int<i32>>>
pub const fn modulo_add_cycles( self, other: i32, modulus: i32, ) -> Result<ValueQuant<Int<i32>, Int<i32>>>
Computes the non-negative modulo of self + other
over |modulus
|,
and the number of cycles the result is reduced.
Performs operations internally as i64
.
§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_i32).modulo_add_cycles(-4, m)?, (0, 0)];
assert_eq![Int(4_i32).modulo_add_cycles(-3, m)?, (1, 0)];
assert_eq![Int(4_i32).modulo_add_cycles(-2, m)?, (2, 0)];
assert_eq![Int(4_i32).modulo_add_cycles(-1, m)?, (0, 1)];
assert_eq![Int(4_i32).modulo_add_cycles( 0, m)?, (1, 1)];
assert_eq![Int(4_i32).modulo_add_cycles( 1, m)?, (2, 1)];
assert_eq![Int(4_i32).modulo_add_cycles( 2, m)?, (0, 2)];
assert_eq![Int(4_i32).modulo_add_cycles( 3, m)?, (1, 2)];
assert_eq![Int(4_i32).modulo_add_cycles( 4, m)?, (2, 2)];
Sourcepub const fn modulo_add_cycles_unchecked(
self,
other: i32,
modulus: i32,
) -> ValueQuant<Int<i32>, Int<i32>>
pub const fn modulo_add_cycles_unchecked( self, other: i32, modulus: i32, ) -> ValueQuant<Int<i32>, Int<i32>>
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 i64
.
§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: i32) -> Result<Int<i32>>
pub const fn modulo_add_inv(self, modulus: i32) -> Result<Int<i32>>
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_i32).modulo_add_inv(m)?, 1];
assert_eq![Int(-3_i32).modulo_add_inv(m)?, 0];
assert_eq![Int(-2_i32).modulo_add_inv(m)?, 2];
assert_eq![Int(-1_i32).modulo_add_inv(m)?, 1];
assert_eq![Int( 0_i32).modulo_add_inv(m)?, 0];
assert_eq![Int( 1_i32).modulo_add_inv(m)?, 2];
assert_eq![Int( 2_i32).modulo_add_inv(m)?, 1];
assert_eq![Int( 3_i32).modulo_add_inv(m)?, 0];
Sourcepub const fn modulo_add_inv_unchecked(self, modulus: i32) -> Int<i32>
pub const fn modulo_add_inv_unchecked(self, modulus: i32) -> Int<i32>
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: i32, modulus: i32) -> Result<Int<i32>>
pub const fn modulo_sub(self, other: i32, modulus: i32) -> Result<Int<i32>>
Computes the modulo of self - other
over |modulus
|.
Performs operations internally as i64
.
§Features
Uses unsafe_hint
for performance optimizations with upcasted arithmetic.
§Errors
Returns NonZeroRequired
if modulus == 0
.
§Examples
let m = 3;
assert_eq![Int(4_i32).modulo_sub(-4, m)?, 2];
assert_eq![Int(4_i32).modulo_sub(-3, m)?, 1];
assert_eq![Int(4_i32).modulo_sub(-2, m)?, 0];
assert_eq![Int(4_i32).modulo_sub(-1, m)?, 2];
assert_eq![Int(4_i32).modulo_sub( 0, m)?, 1];
assert_eq![Int(4_i32).modulo_sub( 1, m)?, 0];
assert_eq![Int(4_i32).modulo_sub( 2, m)?, 2];
assert_eq![Int(4_i32).modulo_sub( 3, m)?, 1];
assert_eq![Int(4_i32).modulo_sub( 4, m)?, 0];
Sourcepub const fn modulo_sub_cycles(
self,
other: i32,
modulus: i32,
) -> Result<ValueQuant<Int<i32>, Int<i32>>>
pub const fn modulo_sub_cycles( self, other: i32, modulus: i32, ) -> Result<ValueQuant<Int<i32>, Int<i32>>>
Computes the non-negative modulo of self - other
over |modulus
|,
and the number of cycles the result is reduced.
Performs operations internally as i64
.
§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_i32).modulo_sub_cycles(-4, m)?, (2, 2)];
assert_eq![Int(4_i32).modulo_sub_cycles(-3, m)?, (1, 2)];
assert_eq![Int(4_i32).modulo_sub_cycles(-2, m)?, (0, 2)];
assert_eq![Int(4_i32).modulo_sub_cycles(-1, m)?, (2, 1)];
assert_eq![Int(4_i32).modulo_sub_cycles( 0, m)?, (1, 1)];
assert_eq![Int(4_i32).modulo_sub_cycles( 1, m)?, (0, 1)];
assert_eq![Int(4_i32).modulo_sub_cycles( 2, m)?, (2, 0)];
assert_eq![Int(4_i32).modulo_sub_cycles( 3, m)?, (1, 0)];
assert_eq![Int(4_i32).modulo_sub_cycles( 4, m)?, (0, 0)];
Sourcepub const fn modulo_sub_cycles_unchecked(
self,
other: i32,
modulus: i32,
) -> (Int<i32>, Int<i32>) ⓘ
pub const fn modulo_sub_cycles_unchecked( self, other: i32, modulus: i32, ) -> (Int<i32>, Int<i32>) ⓘ
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 i64
.
§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: i32, modulus: i32) -> Result<Int<i32>>
pub const fn modulo_mul(self, other: i32, modulus: i32) -> Result<Int<i32>>
Computes the non-negative modulo of self + other
over |modulus
|.
Performs operations internally as i64
.
§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_i32).modulo_mul(-4, m)?, 2];
assert_eq![Int(4_i32).modulo_mul(-3, m)?, 0];
assert_eq![Int(4_i32).modulo_mul(-2, m)?, 1];
assert_eq![Int(4_i32).modulo_mul(-1, m)?, 2];
assert_eq![Int(4_i32).modulo_mul( 0, m)?, 0];
assert_eq![Int(4_i32).modulo_mul( 1, m)?, 1];
assert_eq![Int(4_i32).modulo_mul( 2, m)?, 2];
assert_eq![Int(4_i32).modulo_mul( 3, m)?, 0];
assert_eq![Int(4_i32).modulo_mul( 4, m)?, 1];
Sourcepub const fn modulo_mul_cycles(
self,
other: i32,
modulus: i32,
) -> Result<ValueQuant<Int<i32>, Int<i32>>>
pub const fn modulo_mul_cycles( self, other: i32, modulus: i32, ) -> Result<ValueQuant<Int<i32>, Int<i32>>>
Computes the non-negative modulo of self + other
over |modulus
|,
and the number of cycles the result is reduced.
Performs operations internally as i64
.
§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_i32).modulo_mul_cycles(-4, m)?, (2, 5)];
assert_eq![Int(4_i32).modulo_mul_cycles(-3, m)?, (0, 4)];
assert_eq![Int(4_i32).modulo_mul_cycles(-2, m)?, (1, 2)];
assert_eq![Int(4_i32).modulo_mul_cycles(-1, m)?, (2, 1)];
assert_eq![Int(4_i32).modulo_mul_cycles( 0, m)?, (0, 0)];
assert_eq![Int(4_i32).modulo_mul_cycles( 1, m)?, (1, 1)];
assert_eq![Int(4_i32).modulo_mul_cycles( 2, m)?, (2, 2)];
assert_eq![Int(4_i32).modulo_mul_cycles( 3, m)?, (0, 4)];
assert_eq![Int(4_i32).modulo_mul_cycles( 4, m)?, (1, 5)];
Sourcepub const fn modulo_mul_cycles_unchecked(
self,
other: i32,
modulus: i32,
) -> ValueQuant<Int<i32>, Int<i32>>
pub const fn modulo_mul_cycles_unchecked( self, other: i32, modulus: i32, ) -> ValueQuant<Int<i32>, Int<i32>>
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 i64
.
§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: i32) -> Result<Int<i32>>
pub const fn modulo_mul_inv(self, modulus: i32) -> Result<Int<i32>>
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_i32).modulo_mul_inv(m)?, 4];
assert_eq![Int(-3_i32).modulo_mul_inv(m)?, 2];
assert_eq![Int(-2_i32).modulo_mul_inv(m)?, 3];
assert_eq![Int(-1_i32).modulo_mul_inv(m)?, 1];
assert_eq![Int( 0_i32).modulo_mul_inv(m), Err(NumError::NoInverse)];
assert_eq![Int( 1_i32).modulo_mul_inv(m)?, 1];
assert_eq![Int( 2_i32).modulo_mul_inv(m)?, 3];
assert_eq![Int( 3_i32).modulo_mul_inv(m)?, 2];
assert_eq![Int( 4_i32).modulo_mul_inv(m)?, 4];
Sourcepub const fn modulo_mul_inv_unchecked(self, modulus: i32) -> Int<i32>
pub const fn modulo_mul_inv_unchecked(self, modulus: i32) -> Int<i32>
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: i32, modulus: i32) -> Result<Int<i32>>
pub const fn modulo_div(self, other: i32, modulus: i32) -> Result<Int<i32>>
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
,
and NoInverse
if there’s no multiplicative inverse of other
.
§Examples
let m = 3;
assert_eq![Int(-4_i32).modulo_div(2, m)?, 1];
assert_eq![Int(-3_i32).modulo_div(2, m)?, 0];
assert_eq![Int(-2_i32).modulo_div(2, m)?, 2];
assert_eq![Int(-1_i32).modulo_div(2, m)?, 1];
assert_eq![Int( 0_i32).modulo_div(2, m)?, 0];
assert_eq![Int( 1_i32).modulo_div(2, m)?, 2];
assert_eq![Int( 2_i32).modulo_div(2, m)?, 1];
assert_eq![Int( 3_i32).modulo_div(2, m)?, 0];
assert_eq![Int( 4_i32).modulo_div(2, m)?, 2];
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_unchecked(self, modulus: i64) -> Int<i64>
pub const fn modulo_unchecked(self, modulus: i64) -> Int<i64>
Computes the non-negative modulo of self
over |modulus
|, unchecked version.
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
.
§Panics
Panics if modulus == 0
, and for i128
it could also panic on overflow.
§Examples
let m = 3;
assert_eq![Int(-4_i64).modulo_unchecked(m), 2];
assert_eq![Int(-3_i64).modulo_unchecked(m), 0];
assert_eq![Int(-2_i64).modulo_unchecked(m), 1];
assert_eq![Int(-1_i64).modulo_unchecked(m), 2];
assert_eq![Int( 0_i64).modulo_unchecked(m), 0];
assert_eq![Int( 1_i64).modulo_unchecked(m), 1];
assert_eq![Int( 2_i64).modulo_unchecked(m), 2];
assert_eq![Int( 3_i64).modulo_unchecked(m), 0];
assert_eq![Int( 4_i64).modulo_unchecked(m), 1];
assert_eq![Int( 4_i64).modulo_unchecked(-m), 1];
assert_eq![Int(i64::MAX).modulo_unchecked(i64::MAX - 1), 1];
assert_eq![Int(i64::MAX).modulo_unchecked(i64::MAX), 0];
assert_eq![Int(i64::MAX).modulo_unchecked(i64::MIN), i64::MAX];
assert_eq![Int(i64::MIN).modulo_unchecked(i64::MAX), i64::MAX - 1];
assert_eq![Int(i64::MIN).modulo_unchecked(i64::MIN), 0];
assert_eq![Int(i64::MIN).modulo_unchecked(-1), 0];
let _ = Int(i128::MIN).modulo_unchecked(-1); // i128 could overflow
let _ = Int(1_i64).modulo_unchecked(0); // panics if modulus == 0
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<i128>
Available on crate feature _int_i128
only.
impl Int<i128>
_int_i128
only.§Integer modulo related methods for i128
- 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: i128) -> Result<Int<i128>>
pub const fn modulo(self, modulus: i128) -> Result<Int<i128>>
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_i128).modulo(m)?, 2];
assert_eq![Int(-3_i128).modulo(m)?, 0];
assert_eq![Int(-2_i128).modulo(m)?, 1];
assert_eq![Int(-1_i128).modulo(m)?, 2];
assert_eq![Int( 0_i128).modulo(m)?, 0];
assert_eq![Int( 1_i128).modulo(m)?, 1];
assert_eq![Int( 2_i128).modulo(m)?, 2];
assert_eq![Int( 3_i128).modulo(m)?, 0];
assert_eq![Int( 4_i128).modulo(m)?, 1];
assert_eq![Int(i128::MAX).modulo(i128::MIN)?, i128::MAX];
assert_eq![Int(i128::MIN).modulo(i128::MAX)?, i128::MAX - 1];
assert_eq![Int(i128::MIN).modulo(i128::MIN)?, 0];
assert![Int(i64::MIN).modulo(-1).is_ok()];
assert_eq![Int(1_i128).modulo(0), Err(NumError::NonZeroRequired)];
assert_eq![Int(i128::MIN).modulo(-1), Err(NumError::Overflow(None))];
Sourcepub const fn modulo_unchecked(self, modulus: i128) -> Int<i128>
pub const fn modulo_unchecked(self, modulus: i128) -> Int<i128>
Computes the non-negative modulo of self
over |modulus
|, unchecked version.
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
.
§Panics
Panics if modulus == 0
, and for i128
it could also panic on overflow.
§Examples
let m = 3;
assert_eq![Int(-4_i128).modulo_unchecked(m), 2];
assert_eq![Int(-3_i128).modulo_unchecked(m), 0];
assert_eq![Int(-2_i128).modulo_unchecked(m), 1];
assert_eq![Int(-1_i128).modulo_unchecked(m), 2];
assert_eq![Int( 0_i128).modulo_unchecked(m), 0];
assert_eq![Int( 1_i128).modulo_unchecked(m), 1];
assert_eq![Int( 2_i128).modulo_unchecked(m), 2];
assert_eq![Int( 3_i128).modulo_unchecked(m), 0];
assert_eq![Int( 4_i128).modulo_unchecked(m), 1];
assert_eq![Int( 4_i128).modulo_unchecked(-m), 1];
assert_eq![Int(i128::MAX).modulo_unchecked(i128::MAX - 1), 1];
assert_eq![Int(i128::MAX).modulo_unchecked(i128::MAX), 0];
assert_eq![Int(i128::MAX).modulo_unchecked(i128::MIN), i128::MAX];
assert_eq![Int(i128::MIN).modulo_unchecked(i128::MAX), i128::MAX - 1];
assert_eq![Int(i128::MIN).modulo_unchecked(i128::MIN), 0];
assert_eq![Int(i64::MIN).modulo_unchecked(-1), 0];
let _ = Int(i128::MIN).modulo_unchecked(-1); // i128 could overflow
let _ = Int(1_i128).modulo_unchecked(0); // panics if modulus == 0
Sourcepub const fn modulo_cycles(
self,
modulus: i128,
) -> Result<ValueQuant<Int<i128>, Int<i128>>>
pub const fn modulo_cycles( self, modulus: i128, ) -> Result<ValueQuant<Int<i128>, Int<i128>>>
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_i128).modulo_cycles(m)?, (0, 1)];
assert_eq![Int(-2_i128).modulo_cycles(m)?, (1, 0)];
assert_eq![Int(-1_i128).modulo_cycles(m)?, (2, 0)];
assert_eq![Int( 0_i128).modulo_cycles(m)?, (0, 0)];
assert_eq![Int( 1_i128).modulo_cycles(m)?, (1, 0)];
assert_eq![Int( 2_i128).modulo_cycles(m)?, (2, 0)];
assert_eq![Int( 3_i128).modulo_cycles(m)?, (0, 1)];
Sourcepub const fn modulo_cycles_unchecked(
self,
modulus: i128,
) -> ValueQuant<Int<i128>, Int<i128>>
pub const fn modulo_cycles_unchecked( self, modulus: i128, ) -> ValueQuant<Int<i128>, Int<i128>>
Sourcepub const fn modulo_add(self, other: i128, modulus: i128) -> Result<Int<i128>>
pub const fn modulo_add(self, other: i128, modulus: i128) -> Result<Int<i128>>
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_i128).modulo_add(-4, m)?, 0];
assert_eq![Int(4_i128).modulo_add(-3, m)?, 1];
assert_eq![Int(4_i128).modulo_add(-2, m)?, 2];
assert_eq![Int(4_i128).modulo_add(-1, m)?, 0];
assert_eq![Int(4_i128).modulo_add( 0, m)?, 1];
assert_eq![Int(4_i128).modulo_add( 1, m)?, 2];
assert_eq![Int(4_i128).modulo_add( 2, m)?, 0];
assert_eq![Int(4_i128).modulo_add( 3, m)?, 1];
assert_eq![Int(4_i128).modulo_add( 4, m)?, 2];
Sourcepub const fn modulo_add_cycles(
self,
other: i128,
modulus: i128,
) -> Result<ValueQuant<Int<i128>, Int<i128>>>
pub const fn modulo_add_cycles( self, other: i128, modulus: i128, ) -> Result<ValueQuant<Int<i128>, Int<i128>>>
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_i128).modulo_add_cycles(-4, m)?, (0, 0)];
assert_eq![Int(4_i128).modulo_add_cycles(-3, m)?, (1, 0)];
assert_eq![Int(4_i128).modulo_add_cycles(-2, m)?, (2, 0)];
assert_eq![Int(4_i128).modulo_add_cycles(-1, m)?, (0, 1)];
assert_eq![Int(4_i128).modulo_add_cycles( 0, m)?, (1, 1)];
assert_eq![Int(4_i128).modulo_add_cycles( 1, m)?, (2, 1)];
assert_eq![Int(4_i128).modulo_add_cycles( 2, m)?, (0, 2)];
assert_eq![Int(4_i128).modulo_add_cycles( 3, m)?, (1, 2)];
assert_eq![Int(4_i128).modulo_add_cycles( 4, m)?, (2, 2)];
Sourcepub const fn modulo_add_cycles_unchecked(
self,
other: i128,
modulus: i128,
) -> ValueQuant<Int<i128>, Int<i128>>
pub const fn modulo_add_cycles_unchecked( self, other: i128, modulus: i128, ) -> ValueQuant<Int<i128>, Int<i128>>
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: i128) -> Result<Int<i128>>
pub const fn modulo_add_inv(self, modulus: i128) -> Result<Int<i128>>
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_i128).modulo_add_inv(m)?, 1];
assert_eq![Int(-3_i128).modulo_add_inv(m)?, 0];
assert_eq![Int(-2_i128).modulo_add_inv(m)?, 2];
assert_eq![Int(-1_i128).modulo_add_inv(m)?, 1];
assert_eq![Int( 0_i128).modulo_add_inv(m)?, 0];
assert_eq![Int( 1_i128).modulo_add_inv(m)?, 2];
assert_eq![Int( 2_i128).modulo_add_inv(m)?, 1];
assert_eq![Int( 3_i128).modulo_add_inv(m)?, 0];
Sourcepub const fn modulo_add_inv_unchecked(self, modulus: i128) -> Int<i128>
pub const fn modulo_add_inv_unchecked(self, modulus: i128) -> Int<i128>
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: i128, modulus: i128) -> Result<Int<i128>>
pub const fn modulo_sub(self, other: i128, modulus: i128) -> Result<Int<i128>>
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_i128).modulo_sub(-4, m)?, 2];
assert_eq![Int(4_i128).modulo_sub(-3, m)?, 1];
assert_eq![Int(4_i128).modulo_sub(-2, m)?, 0];
assert_eq![Int(4_i128).modulo_sub(-1, m)?, 2];
assert_eq![Int(4_i128).modulo_sub( 0, m)?, 1];
assert_eq![Int(4_i128).modulo_sub( 1, m)?, 0];
assert_eq![Int(4_i128).modulo_sub( 2, m)?, 2];
assert_eq![Int(4_i128).modulo_sub( 3, m)?, 1];
assert_eq![Int(4_i128).modulo_sub( 4, m)?, 0];
Sourcepub const fn modulo_sub_cycles(
self,
other: i128,
modulus: i128,
) -> Result<ValueQuant<Int<i128>, Int<i128>>>
pub const fn modulo_sub_cycles( self, other: i128, modulus: i128, ) -> Result<ValueQuant<Int<i128>, Int<i128>>>
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_i128).modulo_sub_cycles(-4, m)?, (2, 2)];
assert_eq![Int(4_i128).modulo_sub_cycles(-3, m)?, (1, 2)];
assert_eq![Int(4_i128).modulo_sub_cycles(-2, m)?, (0, 2)];
assert_eq![Int(4_i128).modulo_sub_cycles(-1, m)?, (2, 1)];
assert_eq![Int(4_i128).modulo_sub_cycles( 0, m)?, (1, 1)];
assert_eq![Int(4_i128).modulo_sub_cycles( 1, m)?, (0, 1)];
assert_eq![Int(4_i128).modulo_sub_cycles( 2, m)?, (2, 0)];
assert_eq![Int(4_i128).modulo_sub_cycles( 3, m)?, (1, 0)];
assert_eq![Int(4_i128).modulo_sub_cycles( 4, m)?, (0, 0)];
Sourcepub const fn modulo_sub_cycles_unchecked(
self,
other: i128,
modulus: i128,
) -> (Int<i128>, Int<i128>) ⓘ
pub const fn modulo_sub_cycles_unchecked( self, other: i128, modulus: i128, ) -> (Int<i128>, Int<i128>) ⓘ
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: i128, modulus: i128) -> Result<Int<i128>>
pub const fn modulo_mul(self, other: i128, modulus: i128) -> Result<Int<i128>>
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_i128).modulo_mul(-4, m)?, 2];
assert_eq![Int(4_i128).modulo_mul(-3, m)?, 0];
assert_eq![Int(4_i128).modulo_mul(-2, m)?, 1];
assert_eq![Int(4_i128).modulo_mul(-1, m)?, 2];
assert_eq![Int(4_i128).modulo_mul( 0, m)?, 0];
assert_eq![Int(4_i128).modulo_mul( 1, m)?, 1];
assert_eq![Int(4_i128).modulo_mul( 2, m)?, 2];
assert_eq![Int(4_i128).modulo_mul( 3, m)?, 0];
assert_eq![Int(4_i128).modulo_mul( 4, m)?, 1];
Sourcepub const fn modulo_mul_cycles(
self,
other: i128,
modulus: i128,
) -> Result<ValueQuant<Int<i128>, Int<i128>>>
pub const fn modulo_mul_cycles( self, other: i128, modulus: i128, ) -> Result<ValueQuant<Int<i128>, Int<i128>>>
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_i128).modulo_mul_cycles(-4, m)?, (2, 5)];
assert_eq![Int(4_i128).modulo_mul_cycles(-3, m)?, (0, 4)];
assert_eq![Int(4_i128).modulo_mul_cycles(-2, m)?, (1, 2)];
assert_eq![Int(4_i128).modulo_mul_cycles(-1, m)?, (2, 1)];
assert_eq![Int(4_i128).modulo_mul_cycles( 0, m)?, (0, 0)];
assert_eq![Int(4_i128).modulo_mul_cycles( 1, m)?, (1, 1)];
assert_eq![Int(4_i128).modulo_mul_cycles( 2, m)?, (2, 2)];
assert_eq![Int(4_i128).modulo_mul_cycles( 3, m)?, (0, 4)];
assert_eq![Int(4_i128).modulo_mul_cycles( 4, m)?, (1, 5)];
Sourcepub const fn modulo_mul_cycles_unchecked(
self,
other: i128,
modulus: i128,
) -> ValueQuant<Int<i128>, Int<i128>>
pub const fn modulo_mul_cycles_unchecked( self, other: i128, modulus: i128, ) -> ValueQuant<Int<i128>, Int<i128>>
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: i128) -> Result<Int<i128>>
pub const fn modulo_mul_inv(self, modulus: i128) -> Result<Int<i128>>
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_i128).modulo_mul_inv(m)?, 4];
assert_eq![Int(-3_i128).modulo_mul_inv(m)?, 2];
assert_eq![Int(-2_i128).modulo_mul_inv(m)?, 3];
assert_eq![Int(-1_i128).modulo_mul_inv(m)?, 1];
assert_eq![Int( 0_i128).modulo_mul_inv(m), Err(NumError::NoInverse)];
assert_eq![Int( 1_i128).modulo_mul_inv(m)?, 1];
assert_eq![Int( 2_i128).modulo_mul_inv(m)?, 3];
assert_eq![Int( 3_i128).modulo_mul_inv(m)?, 2];
assert_eq![Int( 4_i128).modulo_mul_inv(m)?, 4];
Sourcepub const fn modulo_mul_inv_unchecked(self, modulus: i128) -> Int<i128>
pub const fn modulo_mul_inv_unchecked(self, modulus: i128) -> Int<i128>
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: i128, modulus: i128) -> Result<Int<i128>>
pub const fn modulo_div(self, other: i128, modulus: i128) -> Result<Int<i128>>
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_i128).modulo_div(2, m)?, 1];
assert_eq![Int(-3_i128).modulo_div(2, m)?, 0];
assert_eq![Int(-2_i128).modulo_div(2, m)?, 2];
assert_eq![Int(-1_i128).modulo_div(2, m)?, 1];
assert_eq![Int( 0_i128).modulo_div(2, m)?, 0];
assert_eq![Int( 1_i128).modulo_div(2, m)?, 2];
assert_eq![Int( 2_i128).modulo_div(2, m)?, 1];
assert_eq![Int( 3_i128).modulo_div(2, m)?, 0];
assert_eq![Int( 4_i128).modulo_div(2, m)?, 2];
Source§impl Int<isize>
Available on crate feature _int_isize
only.
impl Int<isize>
_int_isize
only.§Integer modulo related methods for isize
- 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: isize) -> Result<Int<isize>>
pub const fn modulo(self, modulus: isize) -> Result<Int<isize>>
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 isize_up
.
§Errors
Returns NonZeroRequired
if modulus == 0
, and for i128
it could also return Overflow
.
§Examples
let m = 3;
assert_eq![Int(-4_isize).modulo(m)?, 2];
assert_eq![Int(-3_isize).modulo(m)?, 0];
assert_eq![Int(-2_isize).modulo(m)?, 1];
assert_eq![Int(-1_isize).modulo(m)?, 2];
assert_eq![Int( 0_isize).modulo(m)?, 0];
assert_eq![Int( 1_isize).modulo(m)?, 1];
assert_eq![Int( 2_isize).modulo(m)?, 2];
assert_eq![Int( 3_isize).modulo(m)?, 0];
assert_eq![Int( 4_isize).modulo(m)?, 1];
assert_eq![Int(isize::MAX).modulo(isize::MIN)?, isize::MAX];
assert_eq![Int(isize::MIN).modulo(isize::MAX)?, isize::MAX - 1];
assert_eq![Int(isize::MIN).modulo(isize::MIN)?, 0];
assert![Int(i64::MIN).modulo(-1).is_ok()];
assert_eq![Int(1_isize).modulo(0), Err(NumError::NonZeroRequired)];
assert_eq![Int(i128::MIN).modulo(-1), Err(NumError::Overflow(None))];
Sourcepub const fn modulo_unchecked(self, modulus: isize) -> Int<isize>
pub const fn modulo_unchecked(self, modulus: isize) -> Int<isize>
Computes the non-negative modulo of self
over |modulus
|, unchecked version.
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 isize_up
.
§Panics
Panics if modulus == 0
, and for i128
it could also panic on overflow.
§Examples
let m = 3;
assert_eq![Int(-4_isize).modulo_unchecked(m), 2];
assert_eq![Int(-3_isize).modulo_unchecked(m), 0];
assert_eq![Int(-2_isize).modulo_unchecked(m), 1];
assert_eq![Int(-1_isize).modulo_unchecked(m), 2];
assert_eq![Int( 0_isize).modulo_unchecked(m), 0];
assert_eq![Int( 1_isize).modulo_unchecked(m), 1];
assert_eq![Int( 2_isize).modulo_unchecked(m), 2];
assert_eq![Int( 3_isize).modulo_unchecked(m), 0];
assert_eq![Int( 4_isize).modulo_unchecked(m), 1];
assert_eq![Int( 4_isize).modulo_unchecked(-m), 1];
assert_eq![Int(isize::MAX).modulo_unchecked(isize::MAX - 1), 1];
assert_eq![Int(isize::MAX).modulo_unchecked(isize::MAX), 0];
assert_eq![Int(isize::MAX).modulo_unchecked(isize::MIN), isize::MAX];
assert_eq![Int(isize::MIN).modulo_unchecked(isize::MAX), isize::MAX - 1];
assert_eq![Int(isize::MIN).modulo_unchecked(isize::MIN), 0];
assert_eq![Int(i64::MIN).modulo_unchecked(-1), 0];
let _ = Int(i128::MIN).modulo_unchecked(-1); // i128 could overflow
let _ = Int(1_isize).modulo_unchecked(0); // panics if modulus == 0
Sourcepub const fn modulo_cycles(
self,
modulus: isize,
) -> Result<ValueQuant<Int<isize>, Int<isize>>>
pub const fn modulo_cycles( self, modulus: isize, ) -> Result<ValueQuant<Int<isize>, Int<isize>>>
Computes the non-negative modulo of self
over |modulus
|,
and the number of cycles the result is reduced.
Performs operations internally as isize_up
.
§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_isize).modulo_cycles(m)?, (0, 1)];
assert_eq![Int(-2_isize).modulo_cycles(m)?, (1, 0)];
assert_eq![Int(-1_isize).modulo_cycles(m)?, (2, 0)];
assert_eq![Int( 0_isize).modulo_cycles(m)?, (0, 0)];
assert_eq![Int( 1_isize).modulo_cycles(m)?, (1, 0)];
assert_eq![Int( 2_isize).modulo_cycles(m)?, (2, 0)];
assert_eq![Int( 3_isize).modulo_cycles(m)?, (0, 1)];
Sourcepub const fn modulo_cycles_unchecked(
self,
modulus: isize,
) -> ValueQuant<Int<isize>, Int<isize>>
pub const fn modulo_cycles_unchecked( self, modulus: isize, ) -> ValueQuant<Int<isize>, Int<isize>>
Sourcepub const fn modulo_add(
self,
other: isize,
modulus: isize,
) -> Result<Int<isize>>
pub const fn modulo_add( self, other: isize, modulus: isize, ) -> Result<Int<isize>>
Computes the non-negative modulo of self + other
over |modulus
|.
Performs operations internally as isize_up
.
§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_isize).modulo_add(-4, m)?, 0];
assert_eq![Int(4_isize).modulo_add(-3, m)?, 1];
assert_eq![Int(4_isize).modulo_add(-2, m)?, 2];
assert_eq![Int(4_isize).modulo_add(-1, m)?, 0];
assert_eq![Int(4_isize).modulo_add( 0, m)?, 1];
assert_eq![Int(4_isize).modulo_add( 1, m)?, 2];
assert_eq![Int(4_isize).modulo_add( 2, m)?, 0];
assert_eq![Int(4_isize).modulo_add( 3, m)?, 1];
assert_eq![Int(4_isize).modulo_add( 4, m)?, 2];
Sourcepub const fn modulo_add_cycles(
self,
other: isize,
modulus: isize,
) -> Result<ValueQuant<Int<isize>, Int<isize>>>
pub const fn modulo_add_cycles( self, other: isize, modulus: isize, ) -> Result<ValueQuant<Int<isize>, Int<isize>>>
Computes the non-negative modulo of self + other
over |modulus
|,
and the number of cycles the result is reduced.
Performs operations internally as isize_up
.
§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_isize).modulo_add_cycles(-4, m)?, (0, 0)];
assert_eq![Int(4_isize).modulo_add_cycles(-3, m)?, (1, 0)];
assert_eq![Int(4_isize).modulo_add_cycles(-2, m)?, (2, 0)];
assert_eq![Int(4_isize).modulo_add_cycles(-1, m)?, (0, 1)];
assert_eq![Int(4_isize).modulo_add_cycles( 0, m)?, (1, 1)];
assert_eq![Int(4_isize).modulo_add_cycles( 1, m)?, (2, 1)];
assert_eq![Int(4_isize).modulo_add_cycles( 2, m)?, (0, 2)];
assert_eq![Int(4_isize).modulo_add_cycles( 3, m)?, (1, 2)];
assert_eq![Int(4_isize).modulo_add_cycles( 4, m)?, (2, 2)];
Sourcepub const fn modulo_add_cycles_unchecked(
self,
other: isize,
modulus: isize,
) -> ValueQuant<Int<isize>, Int<isize>>
pub const fn modulo_add_cycles_unchecked( self, other: isize, modulus: isize, ) -> ValueQuant<Int<isize>, Int<isize>>
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 isize_up
.
§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: isize) -> Result<Int<isize>>
pub const fn modulo_add_inv(self, modulus: isize) -> Result<Int<isize>>
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_isize).modulo_add_inv(m)?, 1];
assert_eq![Int(-3_isize).modulo_add_inv(m)?, 0];
assert_eq![Int(-2_isize).modulo_add_inv(m)?, 2];
assert_eq![Int(-1_isize).modulo_add_inv(m)?, 1];
assert_eq![Int( 0_isize).modulo_add_inv(m)?, 0];
assert_eq![Int( 1_isize).modulo_add_inv(m)?, 2];
assert_eq![Int( 2_isize).modulo_add_inv(m)?, 1];
assert_eq![Int( 3_isize).modulo_add_inv(m)?, 0];
Sourcepub const fn modulo_add_inv_unchecked(self, modulus: isize) -> Int<isize>
pub const fn modulo_add_inv_unchecked(self, modulus: isize) -> Int<isize>
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: isize,
modulus: isize,
) -> Result<Int<isize>>
pub const fn modulo_sub( self, other: isize, modulus: isize, ) -> Result<Int<isize>>
Computes the modulo of self - other
over |modulus
|.
Performs operations internally as isize_up
.
§Features
Uses unsafe_hint
for performance optimizations with upcasted arithmetic.
§Errors
Returns NonZeroRequired
if modulus == 0
.
§Examples
let m = 3;
assert_eq![Int(4_isize).modulo_sub(-4, m)?, 2];
assert_eq![Int(4_isize).modulo_sub(-3, m)?, 1];
assert_eq![Int(4_isize).modulo_sub(-2, m)?, 0];
assert_eq![Int(4_isize).modulo_sub(-1, m)?, 2];
assert_eq![Int(4_isize).modulo_sub( 0, m)?, 1];
assert_eq![Int(4_isize).modulo_sub( 1, m)?, 0];
assert_eq![Int(4_isize).modulo_sub( 2, m)?, 2];
assert_eq![Int(4_isize).modulo_sub( 3, m)?, 1];
assert_eq![Int(4_isize).modulo_sub( 4, m)?, 0];
Sourcepub const fn modulo_sub_cycles(
self,
other: isize,
modulus: isize,
) -> Result<ValueQuant<Int<isize>, Int<isize>>>
pub const fn modulo_sub_cycles( self, other: isize, modulus: isize, ) -> Result<ValueQuant<Int<isize>, Int<isize>>>
Computes the non-negative modulo of self - other
over |modulus
|,
and the number of cycles the result is reduced.
Performs operations internally as isize_up
.
§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_isize).modulo_sub_cycles(-4, m)?, (2, 2)];
assert_eq![Int(4_isize).modulo_sub_cycles(-3, m)?, (1, 2)];
assert_eq![Int(4_isize).modulo_sub_cycles(-2, m)?, (0, 2)];
assert_eq![Int(4_isize).modulo_sub_cycles(-1, m)?, (2, 1)];
assert_eq![Int(4_isize).modulo_sub_cycles( 0, m)?, (1, 1)];
assert_eq![Int(4_isize).modulo_sub_cycles( 1, m)?, (0, 1)];
assert_eq![Int(4_isize).modulo_sub_cycles( 2, m)?, (2, 0)];
assert_eq![Int(4_isize).modulo_sub_cycles( 3, m)?, (1, 0)];
assert_eq![Int(4_isize).modulo_sub_cycles( 4, m)?, (0, 0)];
Sourcepub const fn modulo_sub_cycles_unchecked(
self,
other: isize,
modulus: isize,
) -> (Int<isize>, Int<isize>) ⓘ
pub const fn modulo_sub_cycles_unchecked( self, other: isize, modulus: isize, ) -> (Int<isize>, Int<isize>) ⓘ
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 isize_up
.
§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: isize,
modulus: isize,
) -> Result<Int<isize>>
pub const fn modulo_mul( self, other: isize, modulus: isize, ) -> Result<Int<isize>>
Computes the non-negative modulo of self + other
over |modulus
|.
Performs operations internally as isize_up
.
§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_isize).modulo_mul(-4, m)?, 2];
assert_eq![Int(4_isize).modulo_mul(-3, m)?, 0];
assert_eq![Int(4_isize).modulo_mul(-2, m)?, 1];
assert_eq![Int(4_isize).modulo_mul(-1, m)?, 2];
assert_eq![Int(4_isize).modulo_mul( 0, m)?, 0];
assert_eq![Int(4_isize).modulo_mul( 1, m)?, 1];
assert_eq![Int(4_isize).modulo_mul( 2, m)?, 2];
assert_eq![Int(4_isize).modulo_mul( 3, m)?, 0];
assert_eq![Int(4_isize).modulo_mul( 4, m)?, 1];
Sourcepub const fn modulo_mul_cycles(
self,
other: isize,
modulus: isize,
) -> Result<ValueQuant<Int<isize>, Int<isize>>>
pub const fn modulo_mul_cycles( self, other: isize, modulus: isize, ) -> Result<ValueQuant<Int<isize>, Int<isize>>>
Computes the non-negative modulo of self + other
over |modulus
|,
and the number of cycles the result is reduced.
Performs operations internally as isize_up
.
§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_isize).modulo_mul_cycles(-4, m)?, (2, 5)];
assert_eq![Int(4_isize).modulo_mul_cycles(-3, m)?, (0, 4)];
assert_eq![Int(4_isize).modulo_mul_cycles(-2, m)?, (1, 2)];
assert_eq![Int(4_isize).modulo_mul_cycles(-1, m)?, (2, 1)];
assert_eq![Int(4_isize).modulo_mul_cycles( 0, m)?, (0, 0)];
assert_eq![Int(4_isize).modulo_mul_cycles( 1, m)?, (1, 1)];
assert_eq![Int(4_isize).modulo_mul_cycles( 2, m)?, (2, 2)];
assert_eq![Int(4_isize).modulo_mul_cycles( 3, m)?, (0, 4)];
assert_eq![Int(4_isize).modulo_mul_cycles( 4, m)?, (1, 5)];
Sourcepub const fn modulo_mul_cycles_unchecked(
self,
other: isize,
modulus: isize,
) -> ValueQuant<Int<isize>, Int<isize>>
pub const fn modulo_mul_cycles_unchecked( self, other: isize, modulus: isize, ) -> ValueQuant<Int<isize>, Int<isize>>
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 isize_up
.
§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: isize) -> Result<Int<isize>>
pub const fn modulo_mul_inv(self, modulus: isize) -> Result<Int<isize>>
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_isize).modulo_mul_inv(m)?, 4];
assert_eq![Int(-3_isize).modulo_mul_inv(m)?, 2];
assert_eq![Int(-2_isize).modulo_mul_inv(m)?, 3];
assert_eq![Int(-1_isize).modulo_mul_inv(m)?, 1];
assert_eq![Int( 0_isize).modulo_mul_inv(m), Err(NumError::NoInverse)];
assert_eq![Int( 1_isize).modulo_mul_inv(m)?, 1];
assert_eq![Int( 2_isize).modulo_mul_inv(m)?, 3];
assert_eq![Int( 3_isize).modulo_mul_inv(m)?, 2];
assert_eq![Int( 4_isize).modulo_mul_inv(m)?, 4];
Sourcepub const fn modulo_mul_inv_unchecked(self, modulus: isize) -> Int<isize>
pub const fn modulo_mul_inv_unchecked(self, modulus: isize) -> Int<isize>
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: isize,
modulus: isize,
) -> Result<Int<isize>>
pub const fn modulo_div( self, other: isize, modulus: isize, ) -> Result<Int<isize>>
Computes self / other
over |modulus
|.
Performs operations internally as isize_up
.
$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_isize).modulo_div(2, m)?, 1];
assert_eq![Int(-3_isize).modulo_div(2, m)?, 0];
assert_eq![Int(-2_isize).modulo_div(2, m)?, 2];
assert_eq![Int(-1_isize).modulo_div(2, m)?, 1];
assert_eq![Int( 0_isize).modulo_div(2, m)?, 0];
assert_eq![Int( 1_isize).modulo_div(2, m)?, 2];
assert_eq![Int( 2_isize).modulo_div(2, m)?, 1];
assert_eq![Int( 3_isize).modulo_div(2, m)?, 0];
assert_eq![Int( 4_isize).modulo_div(2, m)?, 2];
Source§impl Int<u8>
Available on crate feature _int_u8
only.
impl Int<u8>
_int_u8
only.§Integer modulo related methods for u8
- 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: u8) -> Result<Int<u8>>
pub const fn modulo(self, modulus: u8) -> Result<Int<u8>>
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_u8).modulo(m)?, 0];
assert_eq![Int(1_u8).modulo(m)?, 1];
assert_eq![Int(2_u8).modulo(m)?, 2];
assert_eq![Int(3_u8).modulo(m)?, 0];
assert_eq![Int(4_u8).modulo(m)?, 1];
Sourcepub const fn modulo_unchecked(self, modulus: u8) -> Int<u8>
pub const fn modulo_unchecked(self, modulus: u8) -> Int<u8>
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_u8).modulo_unchecked(m), 0];
assert_eq![Int(1_u8).modulo_unchecked(m), 1];
assert_eq![Int(2_u8).modulo_unchecked(m), 2];
assert_eq![Int(3_u8).modulo_unchecked(m), 0];
assert_eq![Int(4_u8).modulo_unchecked(m), 1];
Sourcepub const fn modulo_cycles(
self,
modulus: u8,
) -> Result<ValueQuant<Int<u8>, Int<u8>>>
pub const fn modulo_cycles( self, modulus: u8, ) -> Result<ValueQuant<Int<u8>, Int<u8>>>
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_u8).modulo_cycles(m)?, (0, 0)];
assert_eq![Int(1_u8).modulo_cycles(m)?, (1, 0)];
assert_eq![Int(2_u8).modulo_cycles(m)?, (2, 0)];
assert_eq![Int(3_u8).modulo_cycles(m)?, (0, 1)];
Sourcepub const fn modulo_cycles_unchecked(
self,
modulus: u8,
) -> ValueQuant<Int<u8>, Int<u8>>
pub const fn modulo_cycles_unchecked( self, modulus: u8, ) -> ValueQuant<Int<u8>, Int<u8>>
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_u8).modulo_cycles_unchecked(m), (0, 0)];
assert_eq![Int(1_u8).modulo_cycles_unchecked(m), (1, 0)];
assert_eq![Int(2_u8).modulo_cycles_unchecked(m), (2, 0)];
assert_eq![Int(3_u8).modulo_cycles_unchecked(m), (0, 1)];
Sourcepub const fn modulo_add(self, other: u8, modulus: u8) -> Result<Int<u8>>
pub const fn modulo_add(self, other: u8, modulus: u8) -> Result<Int<u8>>
Computes the modulo of self + other
over modulus
.
Performs operations internally as u16
.
§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_u8).modulo_add(0, m)?, 1];
assert_eq![Int(4_u8).modulo_add(1, m)?, 2];
assert_eq![Int(4_u8).modulo_add(2, m)?, 0];
assert_eq![Int(4_u8).modulo_add(3, m)?, 1];
assert_eq![Int(4_u8).modulo_add(4, m)?, 2];
Sourcepub const fn modulo_add_cycles(
self,
other: u8,
modulus: u8,
) -> Result<ValueQuant<Int<u8>, Int<u8>>>
pub const fn modulo_add_cycles( self, other: u8, modulus: u8, ) -> Result<ValueQuant<Int<u8>, Int<u8>>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced.
Performs operations internally as u16
.
§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_u8).modulo_add_cycles(0, m)?, (1, 1)];
assert_eq![Int(4_u8).modulo_add_cycles(1, m)?, (2, 1)];
assert_eq![Int(4_u8).modulo_add_cycles(2, m)?, (0, 2)];
assert_eq![Int(4_u8).modulo_add_cycles(3, m)?, (1, 2)];
assert_eq![Int(4_u8).modulo_add_cycles(4, m)?, (2, 2)];
Sourcepub const fn modulo_add_cycles_unchecked(
self,
other: u8,
modulus: u8,
) -> ValueQuant<Int<u8>, Int<u8>>
pub const fn modulo_add_cycles_unchecked( self, other: u8, modulus: u8, ) -> ValueQuant<Int<u8>, Int<u8>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced,
unchecked version.
Performs operations internally as u16
.
§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: u8) -> Result<Int<u8>>
pub const fn modulo_add_inv(self, modulus: u8) -> Result<Int<u8>>
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_u8).modulo_add_inv(m)?, 0];
assert_eq![Int(1_u8).modulo_add_inv(m)?, 2];
assert_eq![Int(2_u8).modulo_add_inv(m)?, 1];
assert_eq![Int(3_u8).modulo_add_inv(m)?, 0];
Sourcepub const fn modulo_add_inv_unchecked(self, modulus: u8) -> Int<u8>
pub const fn modulo_add_inv_unchecked(self, modulus: u8) -> Int<u8>
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: u8, modulus: u8) -> Result<Int<u8>>
pub const fn modulo_sub(self, other: u8, modulus: u8) -> Result<Int<u8>>
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_u8).modulo_sub(0, m)?, 1];
assert_eq![Int(4_u8).modulo_sub(1, m)?, 0];
assert_eq![Int(4_u8).modulo_sub(2, m)?, 2];
assert_eq![Int(4_u8).modulo_sub(3, m)?, 1];
assert_eq![Int(4_u8).modulo_sub(4, m)?, 0];
Sourcepub const fn modulo_sub_unchecked(self, other: u8, modulus: u8) -> Int<u8>
pub const fn modulo_sub_unchecked(self, other: u8, modulus: u8) -> Int<u8>
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: u8,
modulus: u8,
) -> Result<ValueQuant<Int<u8>, Int<u8>>>
pub const fn modulo_sub_cycles( self, other: u8, modulus: u8, ) -> Result<ValueQuant<Int<u8>, Int<u8>>>
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_u8).modulo_sub_cycles(0, m)?, (1, 1)];
assert_eq![Int(4_u8).modulo_sub_cycles(1, m)?, (0, 1)];
assert_eq![Int(4_u8).modulo_sub_cycles(2, m)?, (2, 0)];
assert_eq![Int(4_u8).modulo_sub_cycles(3, m)?, (1, 0)];
assert_eq![Int(4_u8).modulo_sub_cycles(4, m)?, (0, 0)];
Sourcepub const fn modulo_sub_cycles_unchecked(
self,
other: u8,
modulus: u8,
) -> ValueQuant<Int<u8>, Int<u8>>
pub const fn modulo_sub_cycles_unchecked( self, other: u8, modulus: u8, ) -> ValueQuant<Int<u8>, Int<u8>>
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: u8, modulus: u8) -> Result<Int<u8>>
pub const fn modulo_mul(self, other: u8, modulus: u8) -> Result<Int<u8>>
Computes the modulo of self + other
over modulus
.
Performs operations internally as u16
.
§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_u8).modulo_mul(0, m)?, 0];
assert_eq![Int(4_u8).modulo_mul(1, m)?, 1];
assert_eq![Int(4_u8).modulo_mul(2, m)?, 2];
assert_eq![Int(4_u8).modulo_mul(3, m)?, 0];
assert_eq![Int(4_u8).modulo_mul(4, m)?, 1];
Sourcepub const fn modulo_mul_cycles(
self,
other: u8,
modulus: u8,
) -> Result<ValueQuant<Int<u8>, Int<u8>>>
pub const fn modulo_mul_cycles( self, other: u8, modulus: u8, ) -> Result<ValueQuant<Int<u8>, Int<u8>>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced.
Performs operations internally as u16
.
§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_u8).modulo_mul_cycles(0, m)?, (0, 0)];
assert_eq![Int(4_u8).modulo_mul_cycles(1, m)?, (1, 1)];
assert_eq![Int(4_u8).modulo_mul_cycles(2, m)?, (2, 2)];
assert_eq![Int(4_u8).modulo_mul_cycles(3, m)?, (0, 4)];
assert_eq![Int(4_u8).modulo_mul_cycles(4, m)?, (1, 5)];
Sourcepub const fn modulo_mul_cycles_unchecked(
self,
other: u8,
modulus: u8,
) -> ValueQuant<Int<u8>, Int<u8>>
pub const fn modulo_mul_cycles_unchecked( self, other: u8, modulus: u8, ) -> ValueQuant<Int<u8>, Int<u8>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced,
unchecked version.
Performs operations internally as u16
.
§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: u8) -> Result<Int<u8>>
Available on crate features _int_i16
and cast
only.
pub const fn modulo_mul_inv(self, modulus: u8) -> Result<Int<u8>>
_int_i16
and cast
only.Calculates the modular multiplicative inverse.
Performs operations internally as i16
.
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_u8).modulo_mul_inv(m), Err(NumError::NoInverse)];
assert_eq![Int(1_u8).modulo_mul_inv(m)?, 1];
assert_eq![Int(2_u8).modulo_mul_inv(m)?, 3];
assert_eq![Int(3_u8).modulo_mul_inv(m)?, 2];
assert_eq![Int(4_u8).modulo_mul_inv(m)?, 4];
Sourcepub const fn modulo_mul_inv_unchecked(self, modulus: u8) -> Int<u8>
Available on crate features _int_i16
and cast
only.
pub const fn modulo_mul_inv_unchecked(self, modulus: u8) -> Int<u8>
_int_i16
and cast
only.Calculates the modular multiplicative inverse, unchecked version.
Performs operations internally as i16
.
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: u8, modulus: u8) -> Result<Int<u8>>
Available on crate features _int_i16
and cast
only.
pub const fn modulo_div(self, other: u8, modulus: u8) -> Result<Int<u8>>
_int_i16
and cast
only.Computes self / other
over modulus
.
Performs operations internally as i16
.
$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_u8).modulo_div(2, m)?, 0];
assert_eq![Int(1_u8).modulo_div(2, m)?, 2];
assert_eq![Int(2_u8).modulo_div(2, m)?, 1];
assert_eq![Int(3_u8).modulo_div(2, m)?, 0];
assert_eq![Int(4_u8).modulo_div(2, m)?, 2];
Source§impl Int<u16>
Available on crate feature _int_u16
only.
impl Int<u16>
_int_u16
only.§Integer modulo related methods for u16
- 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: u16) -> Result<Int<u16>>
pub const fn modulo(self, modulus: u16) -> Result<Int<u16>>
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_u16).modulo(m)?, 0];
assert_eq![Int(1_u16).modulo(m)?, 1];
assert_eq![Int(2_u16).modulo(m)?, 2];
assert_eq![Int(3_u16).modulo(m)?, 0];
assert_eq![Int(4_u16).modulo(m)?, 1];
Sourcepub const fn modulo_unchecked(self, modulus: u16) -> Int<u16>
pub const fn modulo_unchecked(self, modulus: u16) -> Int<u16>
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_u16).modulo_unchecked(m), 0];
assert_eq![Int(1_u16).modulo_unchecked(m), 1];
assert_eq![Int(2_u16).modulo_unchecked(m), 2];
assert_eq![Int(3_u16).modulo_unchecked(m), 0];
assert_eq![Int(4_u16).modulo_unchecked(m), 1];
Sourcepub const fn modulo_cycles(
self,
modulus: u16,
) -> Result<ValueQuant<Int<u16>, Int<u16>>>
pub const fn modulo_cycles( self, modulus: u16, ) -> Result<ValueQuant<Int<u16>, Int<u16>>>
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_u16).modulo_cycles(m)?, (0, 0)];
assert_eq![Int(1_u16).modulo_cycles(m)?, (1, 0)];
assert_eq![Int(2_u16).modulo_cycles(m)?, (2, 0)];
assert_eq![Int(3_u16).modulo_cycles(m)?, (0, 1)];
Sourcepub const fn modulo_cycles_unchecked(
self,
modulus: u16,
) -> ValueQuant<Int<u16>, Int<u16>>
pub const fn modulo_cycles_unchecked( self, modulus: u16, ) -> ValueQuant<Int<u16>, Int<u16>>
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_u16).modulo_cycles_unchecked(m), (0, 0)];
assert_eq![Int(1_u16).modulo_cycles_unchecked(m), (1, 0)];
assert_eq![Int(2_u16).modulo_cycles_unchecked(m), (2, 0)];
assert_eq![Int(3_u16).modulo_cycles_unchecked(m), (0, 1)];
Sourcepub const fn modulo_add(self, other: u16, modulus: u16) -> Result<Int<u16>>
pub const fn modulo_add(self, other: u16, modulus: u16) -> Result<Int<u16>>
Computes the modulo of self + other
over modulus
.
Performs operations internally as u32
.
§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_u16).modulo_add(0, m)?, 1];
assert_eq![Int(4_u16).modulo_add(1, m)?, 2];
assert_eq![Int(4_u16).modulo_add(2, m)?, 0];
assert_eq![Int(4_u16).modulo_add(3, m)?, 1];
assert_eq![Int(4_u16).modulo_add(4, m)?, 2];
Sourcepub const fn modulo_add_cycles(
self,
other: u16,
modulus: u16,
) -> Result<ValueQuant<Int<u16>, Int<u16>>>
pub const fn modulo_add_cycles( self, other: u16, modulus: u16, ) -> Result<ValueQuant<Int<u16>, Int<u16>>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced.
Performs operations internally as u32
.
§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_u16).modulo_add_cycles(0, m)?, (1, 1)];
assert_eq![Int(4_u16).modulo_add_cycles(1, m)?, (2, 1)];
assert_eq![Int(4_u16).modulo_add_cycles(2, m)?, (0, 2)];
assert_eq![Int(4_u16).modulo_add_cycles(3, m)?, (1, 2)];
assert_eq![Int(4_u16).modulo_add_cycles(4, m)?, (2, 2)];
Sourcepub const fn modulo_add_cycles_unchecked(
self,
other: u16,
modulus: u16,
) -> ValueQuant<Int<u16>, Int<u16>>
pub const fn modulo_add_cycles_unchecked( self, other: u16, modulus: u16, ) -> ValueQuant<Int<u16>, Int<u16>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced,
unchecked version.
Performs operations internally as u32
.
§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: u16) -> Result<Int<u16>>
pub const fn modulo_add_inv(self, modulus: u16) -> Result<Int<u16>>
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_u16).modulo_add_inv(m)?, 0];
assert_eq![Int(1_u16).modulo_add_inv(m)?, 2];
assert_eq![Int(2_u16).modulo_add_inv(m)?, 1];
assert_eq![Int(3_u16).modulo_add_inv(m)?, 0];
Sourcepub const fn modulo_add_inv_unchecked(self, modulus: u16) -> Int<u16>
pub const fn modulo_add_inv_unchecked(self, modulus: u16) -> Int<u16>
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: u16, modulus: u16) -> Result<Int<u16>>
pub const fn modulo_sub(self, other: u16, modulus: u16) -> Result<Int<u16>>
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_u16).modulo_sub(0, m)?, 1];
assert_eq![Int(4_u16).modulo_sub(1, m)?, 0];
assert_eq![Int(4_u16).modulo_sub(2, m)?, 2];
assert_eq![Int(4_u16).modulo_sub(3, m)?, 1];
assert_eq![Int(4_u16).modulo_sub(4, m)?, 0];
Sourcepub const fn modulo_sub_unchecked(self, other: u16, modulus: u16) -> Int<u16>
pub const fn modulo_sub_unchecked(self, other: u16, modulus: u16) -> Int<u16>
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: u16,
modulus: u16,
) -> Result<ValueQuant<Int<u16>, Int<u16>>>
pub const fn modulo_sub_cycles( self, other: u16, modulus: u16, ) -> Result<ValueQuant<Int<u16>, Int<u16>>>
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_u16).modulo_sub_cycles(0, m)?, (1, 1)];
assert_eq![Int(4_u16).modulo_sub_cycles(1, m)?, (0, 1)];
assert_eq![Int(4_u16).modulo_sub_cycles(2, m)?, (2, 0)];
assert_eq![Int(4_u16).modulo_sub_cycles(3, m)?, (1, 0)];
assert_eq![Int(4_u16).modulo_sub_cycles(4, m)?, (0, 0)];
Sourcepub const fn modulo_sub_cycles_unchecked(
self,
other: u16,
modulus: u16,
) -> ValueQuant<Int<u16>, Int<u16>>
pub const fn modulo_sub_cycles_unchecked( self, other: u16, modulus: u16, ) -> ValueQuant<Int<u16>, Int<u16>>
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: u16, modulus: u16) -> Result<Int<u16>>
pub const fn modulo_mul(self, other: u16, modulus: u16) -> Result<Int<u16>>
Computes the modulo of self + other
over modulus
.
Performs operations internally as u32
.
§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_u16).modulo_mul(0, m)?, 0];
assert_eq![Int(4_u16).modulo_mul(1, m)?, 1];
assert_eq![Int(4_u16).modulo_mul(2, m)?, 2];
assert_eq![Int(4_u16).modulo_mul(3, m)?, 0];
assert_eq![Int(4_u16).modulo_mul(4, m)?, 1];
Sourcepub const fn modulo_mul_cycles(
self,
other: u16,
modulus: u16,
) -> Result<ValueQuant<Int<u16>, Int<u16>>>
pub const fn modulo_mul_cycles( self, other: u16, modulus: u16, ) -> Result<ValueQuant<Int<u16>, Int<u16>>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced.
Performs operations internally as u32
.
§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_u16).modulo_mul_cycles(0, m)?, (0, 0)];
assert_eq![Int(4_u16).modulo_mul_cycles(1, m)?, (1, 1)];
assert_eq![Int(4_u16).modulo_mul_cycles(2, m)?, (2, 2)];
assert_eq![Int(4_u16).modulo_mul_cycles(3, m)?, (0, 4)];
assert_eq![Int(4_u16).modulo_mul_cycles(4, m)?, (1, 5)];
Sourcepub const fn modulo_mul_cycles_unchecked(
self,
other: u16,
modulus: u16,
) -> ValueQuant<Int<u16>, Int<u16>>
pub const fn modulo_mul_cycles_unchecked( self, other: u16, modulus: u16, ) -> ValueQuant<Int<u16>, Int<u16>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced,
unchecked version.
Performs operations internally as u32
.
§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: u16) -> Result<Int<u16>>
Available on crate features _int_i32
and cast
only.
pub const fn modulo_mul_inv(self, modulus: u16) -> Result<Int<u16>>
_int_i32
and cast
only.Calculates the modular multiplicative inverse.
Performs operations internally as i32
.
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_u16).modulo_mul_inv(m), Err(NumError::NoInverse)];
assert_eq![Int(1_u16).modulo_mul_inv(m)?, 1];
assert_eq![Int(2_u16).modulo_mul_inv(m)?, 3];
assert_eq![Int(3_u16).modulo_mul_inv(m)?, 2];
assert_eq![Int(4_u16).modulo_mul_inv(m)?, 4];
Sourcepub const fn modulo_mul_inv_unchecked(self, modulus: u16) -> Int<u16>
Available on crate features _int_i32
and cast
only.
pub const fn modulo_mul_inv_unchecked(self, modulus: u16) -> Int<u16>
_int_i32
and cast
only.Calculates the modular multiplicative inverse, unchecked version.
Performs operations internally as i32
.
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: u16, modulus: u16) -> Result<Int<u16>>
Available on crate features _int_i32
and cast
only.
pub const fn modulo_div(self, other: u16, modulus: u16) -> Result<Int<u16>>
_int_i32
and cast
only.Computes self / other
over modulus
.
Performs operations internally as i32
.
$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_u16).modulo_div(2, m)?, 0];
assert_eq![Int(1_u16).modulo_div(2, m)?, 2];
assert_eq![Int(2_u16).modulo_div(2, m)?, 1];
assert_eq![Int(3_u16).modulo_div(2, m)?, 0];
assert_eq![Int(4_u16).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<u64>
Available on crate feature _int_u64
only.
impl Int<u64>
_int_u64
only.§Integer modulo related methods for u64
- 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: u64) -> Result<Int<u64>>
pub const fn modulo(self, modulus: u64) -> Result<Int<u64>>
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_u64).modulo(m)?, 0];
assert_eq![Int(1_u64).modulo(m)?, 1];
assert_eq![Int(2_u64).modulo(m)?, 2];
assert_eq![Int(3_u64).modulo(m)?, 0];
assert_eq![Int(4_u64).modulo(m)?, 1];
Sourcepub const fn modulo_unchecked(self, modulus: u64) -> Int<u64>
pub const fn modulo_unchecked(self, modulus: u64) -> Int<u64>
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_u64).modulo_unchecked(m), 0];
assert_eq![Int(1_u64).modulo_unchecked(m), 1];
assert_eq![Int(2_u64).modulo_unchecked(m), 2];
assert_eq![Int(3_u64).modulo_unchecked(m), 0];
assert_eq![Int(4_u64).modulo_unchecked(m), 1];
Sourcepub const fn modulo_cycles(
self,
modulus: u64,
) -> Result<ValueQuant<Int<u64>, Int<u64>>>
pub const fn modulo_cycles( self, modulus: u64, ) -> Result<ValueQuant<Int<u64>, Int<u64>>>
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_u64).modulo_cycles(m)?, (0, 0)];
assert_eq![Int(1_u64).modulo_cycles(m)?, (1, 0)];
assert_eq![Int(2_u64).modulo_cycles(m)?, (2, 0)];
assert_eq![Int(3_u64).modulo_cycles(m)?, (0, 1)];
Sourcepub const fn modulo_cycles_unchecked(
self,
modulus: u64,
) -> ValueQuant<Int<u64>, Int<u64>>
pub const fn modulo_cycles_unchecked( self, modulus: u64, ) -> ValueQuant<Int<u64>, Int<u64>>
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_u64).modulo_cycles_unchecked(m), (0, 0)];
assert_eq![Int(1_u64).modulo_cycles_unchecked(m), (1, 0)];
assert_eq![Int(2_u64).modulo_cycles_unchecked(m), (2, 0)];
assert_eq![Int(3_u64).modulo_cycles_unchecked(m), (0, 1)];
Sourcepub const fn modulo_add(self, other: u64, modulus: u64) -> Result<Int<u64>>
pub const fn modulo_add(self, other: u64, modulus: u64) -> Result<Int<u64>>
Computes the modulo of self + other
over modulus
.
Performs operations internally as u128
.
§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_u64).modulo_add(0, m)?, 1];
assert_eq![Int(4_u64).modulo_add(1, m)?, 2];
assert_eq![Int(4_u64).modulo_add(2, m)?, 0];
assert_eq![Int(4_u64).modulo_add(3, m)?, 1];
assert_eq![Int(4_u64).modulo_add(4, m)?, 2];
Sourcepub const fn modulo_add_cycles(
self,
other: u64,
modulus: u64,
) -> Result<ValueQuant<Int<u64>, Int<u64>>>
pub const fn modulo_add_cycles( self, other: u64, modulus: u64, ) -> Result<ValueQuant<Int<u64>, Int<u64>>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced.
Performs operations internally as u128
.
§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_u64).modulo_add_cycles(0, m)?, (1, 1)];
assert_eq![Int(4_u64).modulo_add_cycles(1, m)?, (2, 1)];
assert_eq![Int(4_u64).modulo_add_cycles(2, m)?, (0, 2)];
assert_eq![Int(4_u64).modulo_add_cycles(3, m)?, (1, 2)];
assert_eq![Int(4_u64).modulo_add_cycles(4, m)?, (2, 2)];
Sourcepub const fn modulo_add_cycles_unchecked(
self,
other: u64,
modulus: u64,
) -> ValueQuant<Int<u64>, Int<u64>>
pub const fn modulo_add_cycles_unchecked( self, other: u64, modulus: u64, ) -> ValueQuant<Int<u64>, Int<u64>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced,
unchecked version.
Performs operations internally as u128
.
§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: u64) -> Result<Int<u64>>
pub const fn modulo_add_inv(self, modulus: u64) -> Result<Int<u64>>
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_u64).modulo_add_inv(m)?, 0];
assert_eq![Int(1_u64).modulo_add_inv(m)?, 2];
assert_eq![Int(2_u64).modulo_add_inv(m)?, 1];
assert_eq![Int(3_u64).modulo_add_inv(m)?, 0];
Sourcepub const fn modulo_add_inv_unchecked(self, modulus: u64) -> Int<u64>
pub const fn modulo_add_inv_unchecked(self, modulus: u64) -> Int<u64>
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: u64, modulus: u64) -> Result<Int<u64>>
pub const fn modulo_sub(self, other: u64, modulus: u64) -> Result<Int<u64>>
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_u64).modulo_sub(0, m)?, 1];
assert_eq![Int(4_u64).modulo_sub(1, m)?, 0];
assert_eq![Int(4_u64).modulo_sub(2, m)?, 2];
assert_eq![Int(4_u64).modulo_sub(3, m)?, 1];
assert_eq![Int(4_u64).modulo_sub(4, m)?, 0];
Sourcepub const fn modulo_sub_unchecked(self, other: u64, modulus: u64) -> Int<u64>
pub const fn modulo_sub_unchecked(self, other: u64, modulus: u64) -> Int<u64>
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: u64,
modulus: u64,
) -> Result<ValueQuant<Int<u64>, Int<u64>>>
pub const fn modulo_sub_cycles( self, other: u64, modulus: u64, ) -> Result<ValueQuant<Int<u64>, Int<u64>>>
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_u64).modulo_sub_cycles(0, m)?, (1, 1)];
assert_eq![Int(4_u64).modulo_sub_cycles(1, m)?, (0, 1)];
assert_eq![Int(4_u64).modulo_sub_cycles(2, m)?, (2, 0)];
assert_eq![Int(4_u64).modulo_sub_cycles(3, m)?, (1, 0)];
assert_eq![Int(4_u64).modulo_sub_cycles(4, m)?, (0, 0)];
Sourcepub const fn modulo_sub_cycles_unchecked(
self,
other: u64,
modulus: u64,
) -> ValueQuant<Int<u64>, Int<u64>>
pub const fn modulo_sub_cycles_unchecked( self, other: u64, modulus: u64, ) -> ValueQuant<Int<u64>, Int<u64>>
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: u64, modulus: u64) -> Result<Int<u64>>
pub const fn modulo_mul(self, other: u64, modulus: u64) -> Result<Int<u64>>
Computes the modulo of self + other
over modulus
.
Performs operations internally as u128
.
§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_u64).modulo_mul(0, m)?, 0];
assert_eq![Int(4_u64).modulo_mul(1, m)?, 1];
assert_eq![Int(4_u64).modulo_mul(2, m)?, 2];
assert_eq![Int(4_u64).modulo_mul(3, m)?, 0];
assert_eq![Int(4_u64).modulo_mul(4, m)?, 1];
Sourcepub const fn modulo_mul_cycles(
self,
other: u64,
modulus: u64,
) -> Result<ValueQuant<Int<u64>, Int<u64>>>
pub const fn modulo_mul_cycles( self, other: u64, modulus: u64, ) -> Result<ValueQuant<Int<u64>, Int<u64>>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced.
Performs operations internally as u128
.
§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_u64).modulo_mul_cycles(0, m)?, (0, 0)];
assert_eq![Int(4_u64).modulo_mul_cycles(1, m)?, (1, 1)];
assert_eq![Int(4_u64).modulo_mul_cycles(2, m)?, (2, 2)];
assert_eq![Int(4_u64).modulo_mul_cycles(3, m)?, (0, 4)];
assert_eq![Int(4_u64).modulo_mul_cycles(4, m)?, (1, 5)];
Sourcepub const fn modulo_mul_cycles_unchecked(
self,
other: u64,
modulus: u64,
) -> ValueQuant<Int<u64>, Int<u64>>
pub const fn modulo_mul_cycles_unchecked( self, other: u64, modulus: u64, ) -> ValueQuant<Int<u64>, Int<u64>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced,
unchecked version.
Performs operations internally as u128
.
§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: u64) -> Result<Int<u64>>
Available on crate features _int_i128
and cast
only.
pub const fn modulo_mul_inv(self, modulus: u64) -> Result<Int<u64>>
_int_i128
and cast
only.Calculates the modular multiplicative inverse.
Performs operations internally as i128
.
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_u64).modulo_mul_inv(m), Err(NumError::NoInverse)];
assert_eq![Int(1_u64).modulo_mul_inv(m)?, 1];
assert_eq![Int(2_u64).modulo_mul_inv(m)?, 3];
assert_eq![Int(3_u64).modulo_mul_inv(m)?, 2];
assert_eq![Int(4_u64).modulo_mul_inv(m)?, 4];
Sourcepub const fn modulo_mul_inv_unchecked(self, modulus: u64) -> Int<u64>
Available on crate features _int_i128
and cast
only.
pub const fn modulo_mul_inv_unchecked(self, modulus: u64) -> Int<u64>
_int_i128
and cast
only.Calculates the modular multiplicative inverse, unchecked version.
Performs operations internally as i128
.
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: u64, modulus: u64) -> Result<Int<u64>>
Available on crate features _int_i128
and cast
only.
pub const fn modulo_div(self, other: u64, modulus: u64) -> Result<Int<u64>>
_int_i128
and cast
only.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
,
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_u64).modulo_div(2, m)?, 0];
assert_eq![Int(1_u64).modulo_div(2, m)?, 2];
assert_eq![Int(2_u64).modulo_div(2, m)?, 1];
assert_eq![Int(3_u64).modulo_div(2, m)?, 0];
assert_eq![Int(4_u64).modulo_div(2, m)?, 2];
Source§impl Int<u128>
Available on crate feature _int_u128
only.
impl Int<u128>
_int_u128
only.§Integer modulo related methods for u128
- 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: u128) -> Result<Int<u128>>
pub const fn modulo(self, modulus: u128) -> Result<Int<u128>>
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_u128).modulo(m)?, 0];
assert_eq![Int(1_u128).modulo(m)?, 1];
assert_eq![Int(2_u128).modulo(m)?, 2];
assert_eq![Int(3_u128).modulo(m)?, 0];
assert_eq![Int(4_u128).modulo(m)?, 1];
Sourcepub const fn modulo_unchecked(self, modulus: u128) -> Int<u128>
pub const fn modulo_unchecked(self, modulus: u128) -> Int<u128>
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_u128).modulo_unchecked(m), 0];
assert_eq![Int(1_u128).modulo_unchecked(m), 1];
assert_eq![Int(2_u128).modulo_unchecked(m), 2];
assert_eq![Int(3_u128).modulo_unchecked(m), 0];
assert_eq![Int(4_u128).modulo_unchecked(m), 1];
Sourcepub const fn modulo_cycles(
self,
modulus: u128,
) -> Result<ValueQuant<Int<u128>, Int<u128>>>
pub const fn modulo_cycles( self, modulus: u128, ) -> Result<ValueQuant<Int<u128>, Int<u128>>>
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_u128).modulo_cycles(m)?, (0, 0)];
assert_eq![Int(1_u128).modulo_cycles(m)?, (1, 0)];
assert_eq![Int(2_u128).modulo_cycles(m)?, (2, 0)];
assert_eq![Int(3_u128).modulo_cycles(m)?, (0, 1)];
Sourcepub const fn modulo_cycles_unchecked(
self,
modulus: u128,
) -> ValueQuant<Int<u128>, Int<u128>>
pub const fn modulo_cycles_unchecked( self, modulus: u128, ) -> ValueQuant<Int<u128>, Int<u128>>
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_u128).modulo_cycles_unchecked(m), (0, 0)];
assert_eq![Int(1_u128).modulo_cycles_unchecked(m), (1, 0)];
assert_eq![Int(2_u128).modulo_cycles_unchecked(m), (2, 0)];
assert_eq![Int(3_u128).modulo_cycles_unchecked(m), (0, 1)];
Sourcepub const fn modulo_add(self, other: u128, modulus: u128) -> Result<Int<u128>>
pub const fn modulo_add(self, other: u128, modulus: u128) -> Result<Int<u128>>
Computes the modulo of self + other
over modulus
.
Performs operations internally as u128
.
§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_u128).modulo_add(0, m)?, 1];
assert_eq![Int(4_u128).modulo_add(1, m)?, 2];
assert_eq![Int(4_u128).modulo_add(2, m)?, 0];
assert_eq![Int(4_u128).modulo_add(3, m)?, 1];
assert_eq![Int(4_u128).modulo_add(4, m)?, 2];
Sourcepub const fn modulo_add_cycles(
self,
other: u128,
modulus: u128,
) -> Result<ValueQuant<Int<u128>, Int<u128>>>
pub const fn modulo_add_cycles( self, other: u128, modulus: u128, ) -> Result<ValueQuant<Int<u128>, Int<u128>>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced.
Performs operations internally as u128
.
§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_u128).modulo_add_cycles(0, m)?, (1, 1)];
assert_eq![Int(4_u128).modulo_add_cycles(1, m)?, (2, 1)];
assert_eq![Int(4_u128).modulo_add_cycles(2, m)?, (0, 2)];
assert_eq![Int(4_u128).modulo_add_cycles(3, m)?, (1, 2)];
assert_eq![Int(4_u128).modulo_add_cycles(4, m)?, (2, 2)];
Sourcepub const fn modulo_add_cycles_unchecked(
self,
other: u128,
modulus: u128,
) -> ValueQuant<Int<u128>, Int<u128>>
pub const fn modulo_add_cycles_unchecked( self, other: u128, modulus: u128, ) -> ValueQuant<Int<u128>, Int<u128>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced,
unchecked version.
Performs operations internally as u128
.
§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: u128) -> Result<Int<u128>>
pub const fn modulo_add_inv(self, modulus: u128) -> Result<Int<u128>>
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_u128).modulo_add_inv(m)?, 0];
assert_eq![Int(1_u128).modulo_add_inv(m)?, 2];
assert_eq![Int(2_u128).modulo_add_inv(m)?, 1];
assert_eq![Int(3_u128).modulo_add_inv(m)?, 0];
Sourcepub const fn modulo_add_inv_unchecked(self, modulus: u128) -> Int<u128>
pub const fn modulo_add_inv_unchecked(self, modulus: u128) -> Int<u128>
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: u128, modulus: u128) -> Result<Int<u128>>
pub const fn modulo_sub(self, other: u128, modulus: u128) -> Result<Int<u128>>
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_u128).modulo_sub(0, m)?, 1];
assert_eq![Int(4_u128).modulo_sub(1, m)?, 0];
assert_eq![Int(4_u128).modulo_sub(2, m)?, 2];
assert_eq![Int(4_u128).modulo_sub(3, m)?, 1];
assert_eq![Int(4_u128).modulo_sub(4, m)?, 0];
Sourcepub const fn modulo_sub_unchecked(self, other: u128, modulus: u128) -> Int<u128>
pub const fn modulo_sub_unchecked(self, other: u128, modulus: u128) -> Int<u128>
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: u128,
modulus: u128,
) -> Result<ValueQuant<Int<u128>, Int<u128>>>
pub const fn modulo_sub_cycles( self, other: u128, modulus: u128, ) -> Result<ValueQuant<Int<u128>, Int<u128>>>
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_u128).modulo_sub_cycles(0, m)?, (1, 1)];
assert_eq![Int(4_u128).modulo_sub_cycles(1, m)?, (0, 1)];
assert_eq![Int(4_u128).modulo_sub_cycles(2, m)?, (2, 0)];
assert_eq![Int(4_u128).modulo_sub_cycles(3, m)?, (1, 0)];
assert_eq![Int(4_u128).modulo_sub_cycles(4, m)?, (0, 0)];
Sourcepub const fn modulo_sub_cycles_unchecked(
self,
other: u128,
modulus: u128,
) -> ValueQuant<Int<u128>, Int<u128>>
pub const fn modulo_sub_cycles_unchecked( self, other: u128, modulus: u128, ) -> ValueQuant<Int<u128>, Int<u128>>
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: u128, modulus: u128) -> Result<Int<u128>>
pub const fn modulo_mul(self, other: u128, modulus: u128) -> Result<Int<u128>>
Computes the modulo of self + other
over modulus
.
Performs operations internally as u128
.
§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_u128).modulo_mul(0, m)?, 0];
assert_eq![Int(4_u128).modulo_mul(1, m)?, 1];
assert_eq![Int(4_u128).modulo_mul(2, m)?, 2];
assert_eq![Int(4_u128).modulo_mul(3, m)?, 0];
assert_eq![Int(4_u128).modulo_mul(4, m)?, 1];
Sourcepub const fn modulo_mul_cycles(
self,
other: u128,
modulus: u128,
) -> Result<ValueQuant<Int<u128>, Int<u128>>>
pub const fn modulo_mul_cycles( self, other: u128, modulus: u128, ) -> Result<ValueQuant<Int<u128>, Int<u128>>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced.
Performs operations internally as u128
.
§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_u128).modulo_mul_cycles(0, m)?, (0, 0)];
assert_eq![Int(4_u128).modulo_mul_cycles(1, m)?, (1, 1)];
assert_eq![Int(4_u128).modulo_mul_cycles(2, m)?, (2, 2)];
assert_eq![Int(4_u128).modulo_mul_cycles(3, m)?, (0, 4)];
assert_eq![Int(4_u128).modulo_mul_cycles(4, m)?, (1, 5)];
Sourcepub const fn modulo_mul_cycles_unchecked(
self,
other: u128,
modulus: u128,
) -> ValueQuant<Int<u128>, Int<u128>>
pub const fn modulo_mul_cycles_unchecked( self, other: u128, modulus: u128, ) -> ValueQuant<Int<u128>, Int<u128>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced,
unchecked version.
Performs operations internally as u128
.
§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: u128) -> Result<Int<u128>>
Available on crate features _int_i128
and cast
only.
pub const fn modulo_mul_inv(self, modulus: u128) -> Result<Int<u128>>
_int_i128
and cast
only.Calculates the modular multiplicative inverse.
Performs operations internally as i128
.
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_u128).modulo_mul_inv(m), Err(NumError::NoInverse)];
assert_eq![Int(1_u128).modulo_mul_inv(m)?, 1];
assert_eq![Int(2_u128).modulo_mul_inv(m)?, 3];
assert_eq![Int(3_u128).modulo_mul_inv(m)?, 2];
assert_eq![Int(4_u128).modulo_mul_inv(m)?, 4];
Sourcepub const fn modulo_mul_inv_unchecked(self, modulus: u128) -> Int<u128>
Available on crate features _int_i128
and cast
only.
pub const fn modulo_mul_inv_unchecked(self, modulus: u128) -> Int<u128>
_int_i128
and cast
only.Calculates the modular multiplicative inverse, unchecked version.
Performs operations internally as i128
.
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: u128, modulus: u128) -> Result<Int<u128>>
Available on crate features _int_i128
and cast
only.
pub const fn modulo_div(self, other: u128, modulus: u128) -> Result<Int<u128>>
_int_i128
and cast
only.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
,
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_u128).modulo_div(2, m)?, 0];
assert_eq![Int(1_u128).modulo_div(2, m)?, 2];
assert_eq![Int(2_u128).modulo_div(2, m)?, 1];
assert_eq![Int(3_u128).modulo_div(2, m)?, 0];
assert_eq![Int(4_u128).modulo_div(2, m)?, 2];
Source§impl Int<usize>
Available on crate feature _int_usize
only.
impl Int<usize>
_int_usize
only.§Integer modulo related methods for usize
- 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: usize) -> Result<Int<usize>>
pub const fn modulo(self, modulus: usize) -> Result<Int<usize>>
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_usize).modulo(m)?, 0];
assert_eq![Int(1_usize).modulo(m)?, 1];
assert_eq![Int(2_usize).modulo(m)?, 2];
assert_eq![Int(3_usize).modulo(m)?, 0];
assert_eq![Int(4_usize).modulo(m)?, 1];
Sourcepub const fn modulo_unchecked(self, modulus: usize) -> Int<usize>
pub const fn modulo_unchecked(self, modulus: usize) -> Int<usize>
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_usize).modulo_unchecked(m), 0];
assert_eq![Int(1_usize).modulo_unchecked(m), 1];
assert_eq![Int(2_usize).modulo_unchecked(m), 2];
assert_eq![Int(3_usize).modulo_unchecked(m), 0];
assert_eq![Int(4_usize).modulo_unchecked(m), 1];
Sourcepub const fn modulo_cycles(
self,
modulus: usize,
) -> Result<ValueQuant<Int<usize>, Int<usize>>>
pub const fn modulo_cycles( self, modulus: usize, ) -> Result<ValueQuant<Int<usize>, Int<usize>>>
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_usize).modulo_cycles(m)?, (0, 0)];
assert_eq![Int(1_usize).modulo_cycles(m)?, (1, 0)];
assert_eq![Int(2_usize).modulo_cycles(m)?, (2, 0)];
assert_eq![Int(3_usize).modulo_cycles(m)?, (0, 1)];
Sourcepub const fn modulo_cycles_unchecked(
self,
modulus: usize,
) -> ValueQuant<Int<usize>, Int<usize>>
pub const fn modulo_cycles_unchecked( self, modulus: usize, ) -> ValueQuant<Int<usize>, Int<usize>>
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_usize).modulo_cycles_unchecked(m), (0, 0)];
assert_eq![Int(1_usize).modulo_cycles_unchecked(m), (1, 0)];
assert_eq![Int(2_usize).modulo_cycles_unchecked(m), (2, 0)];
assert_eq![Int(3_usize).modulo_cycles_unchecked(m), (0, 1)];
Sourcepub const fn modulo_add(
self,
other: usize,
modulus: usize,
) -> Result<Int<usize>>
pub const fn modulo_add( self, other: usize, modulus: usize, ) -> Result<Int<usize>>
Computes the modulo of self + other
over modulus
.
Performs operations internally as usize_up
.
§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_usize).modulo_add(0, m)?, 1];
assert_eq![Int(4_usize).modulo_add(1, m)?, 2];
assert_eq![Int(4_usize).modulo_add(2, m)?, 0];
assert_eq![Int(4_usize).modulo_add(3, m)?, 1];
assert_eq![Int(4_usize).modulo_add(4, m)?, 2];
Sourcepub const fn modulo_add_cycles(
self,
other: usize,
modulus: usize,
) -> Result<ValueQuant<Int<usize>, Int<usize>>>
pub const fn modulo_add_cycles( self, other: usize, modulus: usize, ) -> Result<ValueQuant<Int<usize>, Int<usize>>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced.
Performs operations internally as usize_up
.
§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_usize).modulo_add_cycles(0, m)?, (1, 1)];
assert_eq![Int(4_usize).modulo_add_cycles(1, m)?, (2, 1)];
assert_eq![Int(4_usize).modulo_add_cycles(2, m)?, (0, 2)];
assert_eq![Int(4_usize).modulo_add_cycles(3, m)?, (1, 2)];
assert_eq![Int(4_usize).modulo_add_cycles(4, m)?, (2, 2)];
Sourcepub const fn modulo_add_cycles_unchecked(
self,
other: usize,
modulus: usize,
) -> ValueQuant<Int<usize>, Int<usize>>
pub const fn modulo_add_cycles_unchecked( self, other: usize, modulus: usize, ) -> ValueQuant<Int<usize>, Int<usize>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced,
unchecked version.
Performs operations internally as usize_up
.
§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: usize) -> Result<Int<usize>>
pub const fn modulo_add_inv(self, modulus: usize) -> Result<Int<usize>>
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_usize).modulo_add_inv(m)?, 0];
assert_eq![Int(1_usize).modulo_add_inv(m)?, 2];
assert_eq![Int(2_usize).modulo_add_inv(m)?, 1];
assert_eq![Int(3_usize).modulo_add_inv(m)?, 0];
Sourcepub const fn modulo_add_inv_unchecked(self, modulus: usize) -> Int<usize>
pub const fn modulo_add_inv_unchecked(self, modulus: usize) -> Int<usize>
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: usize,
modulus: usize,
) -> Result<Int<usize>>
pub const fn modulo_sub( self, other: usize, modulus: usize, ) -> Result<Int<usize>>
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_usize).modulo_sub(0, m)?, 1];
assert_eq![Int(4_usize).modulo_sub(1, m)?, 0];
assert_eq![Int(4_usize).modulo_sub(2, m)?, 2];
assert_eq![Int(4_usize).modulo_sub(3, m)?, 1];
assert_eq![Int(4_usize).modulo_sub(4, m)?, 0];
Sourcepub const fn modulo_sub_unchecked(
self,
other: usize,
modulus: usize,
) -> Int<usize>
pub const fn modulo_sub_unchecked( self, other: usize, modulus: usize, ) -> Int<usize>
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: usize,
modulus: usize,
) -> Result<ValueQuant<Int<usize>, Int<usize>>>
pub const fn modulo_sub_cycles( self, other: usize, modulus: usize, ) -> Result<ValueQuant<Int<usize>, Int<usize>>>
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_usize).modulo_sub_cycles(0, m)?, (1, 1)];
assert_eq![Int(4_usize).modulo_sub_cycles(1, m)?, (0, 1)];
assert_eq![Int(4_usize).modulo_sub_cycles(2, m)?, (2, 0)];
assert_eq![Int(4_usize).modulo_sub_cycles(3, m)?, (1, 0)];
assert_eq![Int(4_usize).modulo_sub_cycles(4, m)?, (0, 0)];
Sourcepub const fn modulo_sub_cycles_unchecked(
self,
other: usize,
modulus: usize,
) -> ValueQuant<Int<usize>, Int<usize>>
pub const fn modulo_sub_cycles_unchecked( self, other: usize, modulus: usize, ) -> ValueQuant<Int<usize>, Int<usize>>
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: usize,
modulus: usize,
) -> Result<Int<usize>>
pub const fn modulo_mul( self, other: usize, modulus: usize, ) -> Result<Int<usize>>
Computes the modulo of self + other
over modulus
.
Performs operations internally as usize_up
.
§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_usize).modulo_mul(0, m)?, 0];
assert_eq![Int(4_usize).modulo_mul(1, m)?, 1];
assert_eq![Int(4_usize).modulo_mul(2, m)?, 2];
assert_eq![Int(4_usize).modulo_mul(3, m)?, 0];
assert_eq![Int(4_usize).modulo_mul(4, m)?, 1];
Sourcepub const fn modulo_mul_cycles(
self,
other: usize,
modulus: usize,
) -> Result<ValueQuant<Int<usize>, Int<usize>>>
pub const fn modulo_mul_cycles( self, other: usize, modulus: usize, ) -> Result<ValueQuant<Int<usize>, Int<usize>>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced.
Performs operations internally as usize_up
.
§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_usize).modulo_mul_cycles(0, m)?, (0, 0)];
assert_eq![Int(4_usize).modulo_mul_cycles(1, m)?, (1, 1)];
assert_eq![Int(4_usize).modulo_mul_cycles(2, m)?, (2, 2)];
assert_eq![Int(4_usize).modulo_mul_cycles(3, m)?, (0, 4)];
assert_eq![Int(4_usize).modulo_mul_cycles(4, m)?, (1, 5)];
Sourcepub const fn modulo_mul_cycles_unchecked(
self,
other: usize,
modulus: usize,
) -> ValueQuant<Int<usize>, Int<usize>>
pub const fn modulo_mul_cycles_unchecked( self, other: usize, modulus: usize, ) -> ValueQuant<Int<usize>, Int<usize>>
Computes the modulo of self + other
over modulus
,
and the number of cycles the result is reduced,
unchecked version.
Performs operations internally as usize_up
.
§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: usize) -> Result<Int<usize>>
Available on crate features _int_i128
and cast
only.
pub const fn modulo_mul_inv(self, modulus: usize) -> Result<Int<usize>>
_int_i128
and cast
only.Calculates the modular multiplicative inverse.
Performs operations internally as isize_up
.
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_usize).modulo_mul_inv(m), Err(NumError::NoInverse)];
assert_eq![Int(1_usize).modulo_mul_inv(m)?, 1];
assert_eq![Int(2_usize).modulo_mul_inv(m)?, 3];
assert_eq![Int(3_usize).modulo_mul_inv(m)?, 2];
assert_eq![Int(4_usize).modulo_mul_inv(m)?, 4];
Sourcepub const fn modulo_mul_inv_unchecked(self, modulus: usize) -> Int<usize>
Available on crate features _int_i128
and cast
only.
pub const fn modulo_mul_inv_unchecked(self, modulus: usize) -> Int<usize>
_int_i128
and cast
only.Calculates the modular multiplicative inverse, unchecked version.
Performs operations internally as isize_up
.
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: usize,
modulus: usize,
) -> Result<Int<usize>>
Available on crate features _int_i128
and cast
only.
pub const fn modulo_div( self, other: usize, modulus: usize, ) -> Result<Int<usize>>
_int_i128
and cast
only.Computes self / other
over modulus
.
Performs operations internally as isize_up
.
$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_usize).modulo_div(2, m)?, 0];
assert_eq![Int(1_usize).modulo_div(2, m)?, 2];
assert_eq![Int(2_usize).modulo_div(2, m)?, 1];
assert_eq![Int(3_usize).modulo_div(2, m)?, 0];
assert_eq![Int(4_usize).modulo_div(2, m)?, 2];
Source§impl Int<i8>
Available on crate feature _int_i8
only.
impl Int<i8>
_int_i8
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_i8).is_prime()];
assert![Int(2_i8).is_prime()];
assert![!Int(1_i8).is_prime()];
assert![!Int(-2_i8).is_prime()];
§Features
This will only be const if the _cmp_i8 feature is enabled.
Sourcepub const fn prime_nth(self) -> Result<Int<i8>>
pub const fn prime_nth(self) -> Result<Int<i8>>
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_i8).prime_nth()];
assert_eq![Ok(Int(3)), Int(1_i8).prime_nth()];
assert_eq![Ok(Int(127)), Int(30_i8).prime_nth()];
assert_eq![Ok(Int(127)), Int(-30_i8).prime_nth()];
assert![Int(31_i8).prime_nth().is_err()];
§Features
This will only be const if the _cmp_i8 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 i16
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_i8).prime_pi()];
assert_eq![2, Int(3_i8).prime_pi()];
assert_eq![31, Int(127_i8).prime_pi()];
assert_eq![0, Int(-5_i8).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_i8 feature is enabled.
Sourcepub const fn totient(self) -> Int<i8>
pub const fn totient(self) -> Int<i8>
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_i8).totient()];
assert_eq![Int(6), Int(9_i8).totient()];
assert_eq![Int(12), Int(13_i8).totient()];
assert_eq![Int(22), Int(-23_i8).totient()];
assert_eq![Int(2), Int(-3_i8).totient()];
§Links
Source§impl Int<i16>
Available on crate feature _int_i16
only.
impl Int<i16>
_int_i16
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_i16).is_prime()];
assert![Int(2_i16).is_prime()];
assert![!Int(1_i16).is_prime()];
assert![!Int(-2_i16).is_prime()];
§Features
This will only be const if the _cmp_i16 feature is enabled.
Sourcepub const fn prime_nth(self) -> Result<Int<i16>>
pub const fn prime_nth(self) -> Result<Int<i16>>
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_i16).prime_nth()];
assert_eq![Ok(Int(3)), Int(1_i16).prime_nth()];
assert_eq![Ok(Int(127)), Int(30_i16).prime_nth()];
assert_eq![Ok(Int(127)), Int(-30_i16).prime_nth()];
assert![Int(31_i8).prime_nth().is_err()];
§Features
This will only be const if the _cmp_i16 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 i32
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_i16).prime_pi()];
assert_eq![2, Int(3_i16).prime_pi()];
assert_eq![31, Int(127_i16).prime_pi()];
assert_eq![0, Int(-5_i16).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_i16 feature is enabled.
Sourcepub const fn totient(self) -> Int<i16>
pub const fn totient(self) -> Int<i16>
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_i16).totient()];
assert_eq![Int(6), Int(9_i16).totient()];
assert_eq![Int(12), Int(13_i16).totient()];
assert_eq![Int(22), Int(-23_i16).totient()];
assert_eq![Int(2), Int(-3_i16).totient()];
§Links
Source§impl Int<i32>
Available on crate feature _int_i32
only.
impl Int<i32>
_int_i32
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_i32).is_prime()];
assert![Int(2_i32).is_prime()];
assert![!Int(1_i32).is_prime()];
assert![!Int(-2_i32).is_prime()];
§Features
This will only be const if the _cmp_i32 feature is enabled.
Sourcepub const fn prime_nth(self) -> Result<Int<i32>>
pub const fn prime_nth(self) -> Result<Int<i32>>
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_i32).prime_nth()];
assert_eq![Ok(Int(3)), Int(1_i32).prime_nth()];
assert_eq![Ok(Int(127)), Int(30_i32).prime_nth()];
assert_eq![Ok(Int(127)), Int(-30_i32).prime_nth()];
assert![Int(31_i8).prime_nth().is_err()];
§Features
This will only be const if the _cmp_i32 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 i64
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_i32).prime_pi()];
assert_eq![2, Int(3_i32).prime_pi()];
assert_eq![31, Int(127_i32).prime_pi()];
assert_eq![0, Int(-5_i32).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_i32 feature is enabled.
Sourcepub const fn totient(self) -> Int<i32>
pub const fn totient(self) -> Int<i32>
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_i32).totient()];
assert_eq![Int(6), Int(9_i32).totient()];
assert_eq![Int(12), Int(13_i32).totient()];
assert_eq![Int(22), Int(-23_i32).totient()];
assert_eq![Int(2), Int(-3_i32).totient()];
§Links
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<i128>
Available on crate feature _int_i128
only.
impl Int<i128>
_int_i128
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_i128).is_prime()];
assert![Int(2_i128).is_prime()];
assert![!Int(1_i128).is_prime()];
assert![!Int(-2_i128).is_prime()];
§Features
This will only be const if the _cmp_i128 feature is enabled.
Sourcepub const fn prime_nth(self) -> Result<Int<i128>>
pub const fn prime_nth(self) -> Result<Int<i128>>
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_i128).prime_nth()];
assert_eq![Ok(Int(3)), Int(1_i128).prime_nth()];
assert_eq![Ok(Int(127)), Int(30_i128).prime_nth()];
assert_eq![Ok(Int(127)), Int(-30_i128).prime_nth()];
assert![Int(31_i8).prime_nth().is_err()];
§Features
This will only be const if the _cmp_i128 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_i128).prime_pi()];
assert_eq![2, Int(3_i128).prime_pi()];
assert_eq![31, Int(127_i128).prime_pi()];
assert_eq![0, Int(-5_i128).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_i128 feature is enabled.
Sourcepub const fn totient(self) -> Int<i128>
pub const fn totient(self) -> Int<i128>
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_i128).totient()];
assert_eq![Int(6), Int(9_i128).totient()];
assert_eq![Int(12), Int(13_i128).totient()];
assert_eq![Int(22), Int(-23_i128).totient()];
assert_eq![Int(2), Int(-3_i128).totient()];
§Links
Source§impl Int<isize>
Available on crate feature _int_isize
only.
impl Int<isize>
_int_isize
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_isize).is_prime()];
assert![Int(2_isize).is_prime()];
assert![!Int(1_isize).is_prime()];
assert![!Int(-2_isize).is_prime()];
§Features
This will only be const if the _cmp_isize feature is enabled.
Sourcepub const fn prime_nth(self) -> Result<Int<isize>>
pub const fn prime_nth(self) -> Result<Int<isize>>
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_isize).prime_nth()];
assert_eq![Ok(Int(3)), Int(1_isize).prime_nth()];
assert_eq![Ok(Int(127)), Int(30_isize).prime_nth()];
assert_eq![Ok(Int(127)), Int(-30_isize).prime_nth()];
assert![Int(31_i8).prime_nth().is_err()];
§Features
This will only be const if the _cmp_isize 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 isize_up
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_isize).prime_pi()];
assert_eq![2, Int(3_isize).prime_pi()];
assert_eq![31, Int(127_isize).prime_pi()];
assert_eq![0, Int(-5_isize).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_isize feature is enabled.
Sourcepub const fn totient(self) -> Int<isize>
pub const fn totient(self) -> Int<isize>
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_isize).totient()];
assert_eq![Int(6), Int(9_isize).totient()];
assert_eq![Int(12), Int(13_isize).totient()];
assert_eq![Int(22), Int(-23_isize).totient()];
assert_eq![Int(2), Int(-3_isize).totient()];
§Links
Source§impl Int<u8>
Available on crate feature _int_u8
only.
impl Int<u8>
_int_u8
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_u8).is_prime()];
assert![Int(2_u8).is_prime()];
assert![!Int(1_u8).is_prime()];
§Features
This will only be const if the _cmp_u8 feature is enabled.
Sourcepub const fn prime_nth(self) -> Result<Int<u8>>
pub const fn prime_nth(self) -> Result<Int<u8>>
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_u8).prime_nth()];
assert_eq![Ok(Int(3)), Int(1_u8).prime_nth()];
assert_eq![Ok(Int(251)), Int(53_u8).prime_nth()];
assert![Int(54_u8).prime_nth().is_err()];
§Features
This will only be const if the _cmp_u8 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 u16
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_u8).prime_pi()];
assert_eq![2, Int(3_u8).prime_pi()];
assert_eq![31, Int(127_u8).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_u8 feature is enabled.
Sourcepub const fn totient(self) -> Int<u8>
pub const fn totient(self) -> Int<u8>
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_u8).totient()];
assert_eq![Int(6), Int(9_u8).totient()];
assert_eq![Int(12), Int(13_u8).totient()];
§Links
Source§impl Int<u16>
Available on crate feature _int_u16
only.
impl Int<u16>
_int_u16
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_u16).is_prime()];
assert![Int(2_u16).is_prime()];
assert![!Int(1_u16).is_prime()];
§Features
This will only be const if the _cmp_u16 feature is enabled.
Sourcepub const fn prime_nth(self) -> Result<Int<u16>>
pub const fn prime_nth(self) -> Result<Int<u16>>
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_u16).prime_nth()];
assert_eq![Ok(Int(3)), Int(1_u16).prime_nth()];
assert_eq![Ok(Int(251)), Int(53_u16).prime_nth()];
assert![Int(54_u8).prime_nth().is_err()];
§Features
This will only be const if the _cmp_u16 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 u32
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_u16).prime_pi()];
assert_eq![2, Int(3_u16).prime_pi()];
assert_eq![31, Int(127_u16).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_u16 feature is enabled.
Sourcepub const fn totient(self) -> Int<u16>
pub const fn totient(self) -> Int<u16>
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_u16).totient()];
assert_eq![Int(6), Int(9_u16).totient()];
assert_eq![Int(12), Int(13_u16).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<u64>
Available on crate feature _int_u64
only.
impl Int<u64>
_int_u64
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_u64).is_prime()];
assert![Int(2_u64).is_prime()];
assert![!Int(1_u64).is_prime()];
§Features
This will only be const if the _cmp_u64 feature is enabled.
Sourcepub const fn prime_nth(self) -> Result<Int<u64>>
pub const fn prime_nth(self) -> Result<Int<u64>>
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_u64).prime_nth()];
assert_eq![Ok(Int(3)), Int(1_u64).prime_nth()];
assert_eq![Ok(Int(251)), Int(53_u64).prime_nth()];
assert![Int(54_u8).prime_nth().is_err()];
§Features
This will only be const if the _cmp_u64 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 u128
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_u64).prime_pi()];
assert_eq![2, Int(3_u64).prime_pi()];
assert_eq![31, Int(127_u64).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_u64 feature is enabled.
Sourcepub const fn totient(self) -> Int<u64>
pub const fn totient(self) -> Int<u64>
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_u64).totient()];
assert_eq![Int(6), Int(9_u64).totient()];
assert_eq![Int(12), Int(13_u64).totient()];
§Links
Source§impl Int<u128>
Available on crate feature _int_u128
only.
impl Int<u128>
_int_u128
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_u128).is_prime()];
assert![Int(2_u128).is_prime()];
assert![!Int(1_u128).is_prime()];
§Features
This will only be const if the _cmp_u128 feature is enabled.
Sourcepub const fn prime_nth(self) -> Result<Int<u128>>
pub const fn prime_nth(self) -> Result<Int<u128>>
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_u128).prime_nth()];
assert_eq![Ok(Int(3)), Int(1_u128).prime_nth()];
assert_eq![Ok(Int(251)), Int(53_u128).prime_nth()];
assert![Int(54_u8).prime_nth().is_err()];
§Features
This will only be const if the _cmp_u128 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 u128
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_u128).prime_pi()];
assert_eq![2, Int(3_u128).prime_pi()];
assert_eq![31, Int(127_u128).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_u128 feature is enabled.
Sourcepub const fn totient(self) -> Int<u128>
pub const fn totient(self) -> Int<u128>
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_u128).totient()];
assert_eq![Int(6), Int(9_u128).totient()];
assert_eq![Int(12), Int(13_u128).totient()];
§Links
Source§impl Int<usize>
Available on crate feature _int_usize
only.
impl Int<usize>
_int_usize
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_usize).is_prime()];
assert![Int(2_usize).is_prime()];
assert![!Int(1_usize).is_prime()];
Sourcepub const fn prime_nth(self) -> Result<Int<usize>>
pub const fn prime_nth(self) -> Result<Int<usize>>
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_usize).prime_nth()];
assert_eq![Ok(Int(3)), Int(1_usize).prime_nth()];
assert_eq![Ok(Int(251)), Int(53_usize).prime_nth()];
assert![Int(54_u8).prime_nth().is_err()];
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 usize_up
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_usize).prime_pi()];
assert_eq![2, Int(3_usize).prime_pi()];
assert_eq![31, Int(127_usize).prime_pi()];
§Links
Sourcepub const fn totient(self) -> Int<usize>
pub const fn totient(self) -> Int<usize>
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_usize).totient()];
assert_eq![Int(6), Int(9_usize).totient()];
assert_eq![Int(12), Int(13_usize).totient()];
§Links
Source§impl Int<i8>
Available on crate feature _int_i8
only.
impl Int<i8>
_int_i8
only.§Integer root related methods for i8
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_i8).is_square(), false];
assert_eq![Int(13_i8).is_square(), false];
assert_eq![Int(16_i8).is_square(), true];
assert_eq![Int(20_i8).is_square(), false];
assert_eq![Int(21_i8).is_square(), false];
assert_eq![Int(-16_i8).is_square(), false];
§Features
This will only be const if the _cmp_i8 feature is enabled.
Sourcepub const fn sqrt_ceil(self) -> Result<Int<i8>>
pub const fn sqrt_ceil(self) -> Result<Int<i8>>
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_i8).sqrt_ceil(), Ok(Int(4))];
assert_eq![Int(13_i8).sqrt_ceil(), Ok(Int(4))];
assert_eq![Int(16_i8).sqrt_ceil(), Ok(Int(4))];
assert_eq![Int(20_i8).sqrt_ceil(), Ok(Int(5))];
assert_eq![Int(21_i8).sqrt_ceil(), Ok(Int(5))];
assert_eq![Int(-4_i8).sqrt_ceil(), Err(NonNegativeRequired)];
§Features
This will only be const if the _cmp_i8 feature is enabled.
Sourcepub const fn sqrt_floor(self) -> Result<Int<i8>>
pub const fn sqrt_floor(self) -> Result<Int<i8>>
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_i8).sqrt_floor(), Ok(Int(3))];
assert_eq![Int(13_i8).sqrt_floor(), Ok(Int(3))];
assert_eq![Int(16_i8).sqrt_floor(), Ok(Int(4))];
assert_eq![Int(20_i8).sqrt_floor(), Ok(Int(4))];
assert_eq![Int(21_i8).sqrt_floor(), Ok(Int(4))];
assert_eq![Int(-4_i8).sqrt_floor(), Err(NonNegativeRequired)];
§Features
This will only be const if the _cmp_i8 feature is enabled.
Sourcepub const fn sqrt_round(self) -> Result<Int<i8>>
pub const fn sqrt_round(self) -> Result<Int<i8>>
Returns the rounded integer square root.
Performs operations internally as i16
.
§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_i8).sqrt_round(), Ok(Int(3))];
assert_eq![Int(13_i8).sqrt_round(), Ok(Int(4))];
assert_eq![Int(16_i8).sqrt_round(), Ok(Int(4))];
assert_eq![Int(20_i8).sqrt_round(), Ok(Int(4))];
assert_eq![Int(21_i8).sqrt_round(), Ok(Int(5))];
assert_eq![Int(-4_i8).sqrt_round(), Err(NonNegativeRequired)];
Sourcepub const fn root_ceil(self, nth: u32) -> Result<Int<i8>>
pub const fn root_ceil(self, nth: u32) -> Result<Int<i8>>
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_i8).root_ceil(4), Ok(Int(3))];
assert_eq![Int(70_i8).root_ceil(4), Ok(Int(3))];
assert_eq![Int(81_i8).root_ceil(4), Ok(Int(3))];
assert_eq![Int(88_i8).root_ceil(4), Ok(Int(4))];
assert_eq![Int(114_i8).root_ceil(4), Ok(Int(4))];
assert_eq![Int(-81i8).root_ceil(4), Err(NonNegativeRequired)];
assert_eq![Int(i8::MAX).root_ceil(1), Ok(Int(i8::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<i8>>
pub const fn root_floor(self, nth: u32) -> Result<Int<i8>>
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_i8).root_floor(4), Ok(Int(2))];
assert_eq![Int(70_i8).root_floor(4), Ok(Int(2))];
assert_eq![Int(81_i8).root_floor(4), Ok(Int(3))];
assert_eq![Int(88_i8).root_floor(4), Ok(Int(3))];
assert_eq![Int(114_i8).root_floor(4), Ok(Int(3))];
assert_eq![Int(-81_i8).root_floor(4), Err(NonNegativeRequired)];
assert_eq![Int(i8::MAX).root_floor(1), Ok(Int(i8::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<i16>
Available on crate feature _int_i16
only.
impl Int<i16>
_int_i16
only.§Integer root related methods for i16
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_i16).is_square(), false];
assert_eq![Int(13_i16).is_square(), false];
assert_eq![Int(16_i16).is_square(), true];
assert_eq![Int(20_i16).is_square(), false];
assert_eq![Int(21_i16).is_square(), false];
assert_eq![Int(-16_i16).is_square(), false];
§Features
This will only be const if the _cmp_i16 feature is enabled.
Sourcepub const fn sqrt_ceil(self) -> Result<Int<i16>>
pub const fn sqrt_ceil(self) -> Result<Int<i16>>
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_i16).sqrt_ceil(), Ok(Int(4))];
assert_eq![Int(13_i16).sqrt_ceil(), Ok(Int(4))];
assert_eq![Int(16_i16).sqrt_ceil(), Ok(Int(4))];
assert_eq![Int(20_i16).sqrt_ceil(), Ok(Int(5))];
assert_eq![Int(21_i16).sqrt_ceil(), Ok(Int(5))];
assert_eq![Int(-4_i16).sqrt_ceil(), Err(NonNegativeRequired)];
§Features
This will only be const if the _cmp_i16 feature is enabled.
Sourcepub const fn sqrt_floor(self) -> Result<Int<i16>>
pub const fn sqrt_floor(self) -> Result<Int<i16>>
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_i16).sqrt_floor(), Ok(Int(3))];
assert_eq![Int(13_i16).sqrt_floor(), Ok(Int(3))];
assert_eq![Int(16_i16).sqrt_floor(), Ok(Int(4))];
assert_eq![Int(20_i16).sqrt_floor(), Ok(Int(4))];
assert_eq![Int(21_i16).sqrt_floor(), Ok(Int(4))];
assert_eq![Int(-4_i16).sqrt_floor(), Err(NonNegativeRequired)];
§Features
This will only be const if the _cmp_i16 feature is enabled.
Sourcepub const fn sqrt_round(self) -> Result<Int<i16>>
pub const fn sqrt_round(self) -> Result<Int<i16>>
Returns the rounded integer square root.
Performs operations internally as i32
.
§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_i16).sqrt_round(), Ok(Int(3))];
assert_eq![Int(13_i16).sqrt_round(), Ok(Int(4))];
assert_eq![Int(16_i16).sqrt_round(), Ok(Int(4))];
assert_eq![Int(20_i16).sqrt_round(), Ok(Int(4))];
assert_eq![Int(21_i16).sqrt_round(), Ok(Int(5))];
assert_eq![Int(-4_i16).sqrt_round(), Err(NonNegativeRequired)];
Sourcepub const fn root_ceil(self, nth: u32) -> Result<Int<i16>>
pub const fn root_ceil(self, nth: u32) -> Result<Int<i16>>
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_i16).root_ceil(4), Ok(Int(3))];
assert_eq![Int(70_i16).root_ceil(4), Ok(Int(3))];
assert_eq![Int(81_i16).root_ceil(4), Ok(Int(3))];
assert_eq![Int(88_i16).root_ceil(4), Ok(Int(4))];
assert_eq![Int(114_i16).root_ceil(4), Ok(Int(4))];
assert_eq![Int(-81i16).root_ceil(4), Err(NonNegativeRequired)];
assert_eq![Int(i16::MAX).root_ceil(1), Ok(Int(i16::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<i16>>
pub const fn root_floor(self, nth: u32) -> Result<Int<i16>>
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_i16).root_floor(4), Ok(Int(2))];
assert_eq![Int(70_i16).root_floor(4), Ok(Int(2))];
assert_eq![Int(81_i16).root_floor(4), Ok(Int(3))];
assert_eq![Int(88_i16).root_floor(4), Ok(Int(3))];
assert_eq![Int(114_i16).root_floor(4), Ok(Int(3))];
assert_eq![Int(-81_i16).root_floor(4), Err(NonNegativeRequired)];
assert_eq![Int(i16::MAX).root_floor(1), Ok(Int(i16::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<i32>
Available on crate feature _int_i32
only.
impl Int<i32>
_int_i32
only.§Integer root related methods for i32
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_i32).is_square(), false];
assert_eq![Int(13_i32).is_square(), false];
assert_eq![Int(16_i32).is_square(), true];
assert_eq![Int(20_i32).is_square(), false];
assert_eq![Int(21_i32).is_square(), false];
assert_eq![Int(-16_i32).is_square(), false];
§Features
This will only be const if the _cmp_i32 feature is enabled.
Sourcepub const fn sqrt_ceil(self) -> Result<Int<i32>>
pub const fn sqrt_ceil(self) -> Result<Int<i32>>
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_i32).sqrt_ceil(), Ok(Int(4))];
assert_eq![Int(13_i32).sqrt_ceil(), Ok(Int(4))];
assert_eq![Int(16_i32).sqrt_ceil(), Ok(Int(4))];
assert_eq![Int(20_i32).sqrt_ceil(), Ok(Int(5))];
assert_eq![Int(21_i32).sqrt_ceil(), Ok(Int(5))];
assert_eq![Int(-4_i32).sqrt_ceil(), Err(NonNegativeRequired)];
§Features
This will only be const if the _cmp_i32 feature is enabled.
Sourcepub const fn sqrt_floor(self) -> Result<Int<i32>>
pub const fn sqrt_floor(self) -> Result<Int<i32>>
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_i32).sqrt_floor(), Ok(Int(3))];
assert_eq![Int(13_i32).sqrt_floor(), Ok(Int(3))];
assert_eq![Int(16_i32).sqrt_floor(), Ok(Int(4))];
assert_eq![Int(20_i32).sqrt_floor(), Ok(Int(4))];
assert_eq![Int(21_i32).sqrt_floor(), Ok(Int(4))];
assert_eq![Int(-4_i32).sqrt_floor(), Err(NonNegativeRequired)];
§Features
This will only be const if the _cmp_i32 feature is enabled.
Sourcepub const fn sqrt_round(self) -> Result<Int<i32>>
pub const fn sqrt_round(self) -> Result<Int<i32>>
Returns the rounded integer square root.
Performs operations internally as i64
.
§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_i32).sqrt_round(), Ok(Int(3))];
assert_eq![Int(13_i32).sqrt_round(), Ok(Int(4))];
assert_eq![Int(16_i32).sqrt_round(), Ok(Int(4))];
assert_eq![Int(20_i32).sqrt_round(), Ok(Int(4))];
assert_eq![Int(21_i32).sqrt_round(), Ok(Int(5))];
assert_eq![Int(-4_i32).sqrt_round(), Err(NonNegativeRequired)];
Sourcepub const fn root_ceil(self, nth: u32) -> Result<Int<i32>>
pub const fn root_ceil(self, nth: u32) -> Result<Int<i32>>
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_i32).root_ceil(4), Ok(Int(3))];
assert_eq![Int(70_i32).root_ceil(4), Ok(Int(3))];
assert_eq![Int(81_i32).root_ceil(4), Ok(Int(3))];
assert_eq![Int(88_i32).root_ceil(4), Ok(Int(4))];
assert_eq![Int(114_i32).root_ceil(4), Ok(Int(4))];
assert_eq![Int(-81i32).root_ceil(4), Err(NonNegativeRequired)];
assert_eq![Int(i32::MAX).root_ceil(1), Ok(Int(i32::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<i32>>
pub const fn root_floor(self, nth: u32) -> Result<Int<i32>>
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_i32).root_floor(4), Ok(Int(2))];
assert_eq![Int(70_i32).root_floor(4), Ok(Int(2))];
assert_eq![Int(81_i32).root_floor(4), Ok(Int(3))];
assert_eq![Int(88_i32).root_floor(4), Ok(Int(3))];
assert_eq![Int(114_i32).root_floor(4), Ok(Int(3))];
assert_eq![Int(-81_i32).root_floor(4), Err(NonNegativeRequired)];
assert_eq![Int(i32::MAX).root_floor(1), Ok(Int(i32::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<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<i128>
Available on crate feature _int_i128
only.
impl Int<i128>
_int_i128
only.§Integer root related methods for i128
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_i128).is_square(), false];
assert_eq![Int(13_i128).is_square(), false];
assert_eq![Int(16_i128).is_square(), true];
assert_eq![Int(20_i128).is_square(), false];
assert_eq![Int(21_i128).is_square(), false];
assert_eq![Int(-16_i128).is_square(), false];
§Features
This will only be const if the _cmp_i128 feature is enabled.
Sourcepub const fn sqrt_ceil(self) -> Result<Int<i128>>
pub const fn sqrt_ceil(self) -> Result<Int<i128>>
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_i128).sqrt_ceil(), Ok(Int(4))];
assert_eq![Int(13_i128).sqrt_ceil(), Ok(Int(4))];
assert_eq![Int(16_i128).sqrt_ceil(), Ok(Int(4))];
assert_eq![Int(20_i128).sqrt_ceil(), Ok(Int(5))];
assert_eq![Int(21_i128).sqrt_ceil(), Ok(Int(5))];
assert_eq![Int(-4_i128).sqrt_ceil(), Err(NonNegativeRequired)];
§Features
This will only be const if the _cmp_i128 feature is enabled.
Sourcepub const fn sqrt_floor(self) -> Result<Int<i128>>
pub const fn sqrt_floor(self) -> Result<Int<i128>>
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_i128).sqrt_floor(), Ok(Int(3))];
assert_eq![Int(13_i128).sqrt_floor(), Ok(Int(3))];
assert_eq![Int(16_i128).sqrt_floor(), Ok(Int(4))];
assert_eq![Int(20_i128).sqrt_floor(), Ok(Int(4))];
assert_eq![Int(21_i128).sqrt_floor(), Ok(Int(4))];
assert_eq![Int(-4_i128).sqrt_floor(), Err(NonNegativeRequired)];
§Features
This will only be const if the _cmp_i128 feature is enabled.
Sourcepub const fn sqrt_round(self) -> Result<Int<i128>>
pub const fn sqrt_round(self) -> Result<Int<i128>>
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_i128).sqrt_round(), Ok(Int(3))];
assert_eq![Int(13_i128).sqrt_round(), Ok(Int(4))];
assert_eq![Int(16_i128).sqrt_round(), Ok(Int(4))];
assert_eq![Int(20_i128).sqrt_round(), Ok(Int(4))];
assert_eq![Int(21_i128).sqrt_round(), Ok(Int(5))];
assert_eq![Int(-4_i128).sqrt_round(), Err(NonNegativeRequired)];
Sourcepub const fn root_ceil(self, nth: u32) -> Result<Int<i128>>
pub const fn root_ceil(self, nth: u32) -> Result<Int<i128>>
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_i128).root_ceil(4), Ok(Int(3))];
assert_eq![Int(70_i128).root_ceil(4), Ok(Int(3))];
assert_eq![Int(81_i128).root_ceil(4), Ok(Int(3))];
assert_eq![Int(88_i128).root_ceil(4), Ok(Int(4))];
assert_eq![Int(114_i128).root_ceil(4), Ok(Int(4))];
assert_eq![Int(-81i128).root_ceil(4), Err(NonNegativeRequired)];
assert_eq![Int(i128::MAX).root_ceil(1), Ok(Int(i128::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<i128>>
pub const fn root_floor(self, nth: u32) -> Result<Int<i128>>
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_i128).root_floor(4), Ok(Int(2))];
assert_eq![Int(70_i128).root_floor(4), Ok(Int(2))];
assert_eq![Int(81_i128).root_floor(4), Ok(Int(3))];
assert_eq![Int(88_i128).root_floor(4), Ok(Int(3))];
assert_eq![Int(114_i128).root_floor(4), Ok(Int(3))];
assert_eq![Int(-81_i128).root_floor(4), Err(NonNegativeRequired)];
assert_eq![Int(i128::MAX).root_floor(1), Ok(Int(i128::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<isize>
Available on crate feature _int_isize
only.
impl Int<isize>
_int_isize
only.§Integer root related methods for isize
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_isize).is_square(), false];
assert_eq![Int(13_isize).is_square(), false];
assert_eq![Int(16_isize).is_square(), true];
assert_eq![Int(20_isize).is_square(), false];
assert_eq![Int(21_isize).is_square(), false];
assert_eq![Int(-16_isize).is_square(), false];
§Features
This will only be const if the _cmp_isize feature is enabled.
Sourcepub const fn sqrt_ceil(self) -> Result<Int<isize>>
pub const fn sqrt_ceil(self) -> Result<Int<isize>>
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_isize).sqrt_ceil(), Ok(Int(4))];
assert_eq![Int(13_isize).sqrt_ceil(), Ok(Int(4))];
assert_eq![Int(16_isize).sqrt_ceil(), Ok(Int(4))];
assert_eq![Int(20_isize).sqrt_ceil(), Ok(Int(5))];
assert_eq![Int(21_isize).sqrt_ceil(), Ok(Int(5))];
assert_eq![Int(-4_isize).sqrt_ceil(), Err(NonNegativeRequired)];
§Features
This will only be const if the _cmp_isize feature is enabled.
Sourcepub const fn sqrt_floor(self) -> Result<Int<isize>>
pub const fn sqrt_floor(self) -> Result<Int<isize>>
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_isize).sqrt_floor(), Ok(Int(3))];
assert_eq![Int(13_isize).sqrt_floor(), Ok(Int(3))];
assert_eq![Int(16_isize).sqrt_floor(), Ok(Int(4))];
assert_eq![Int(20_isize).sqrt_floor(), Ok(Int(4))];
assert_eq![Int(21_isize).sqrt_floor(), Ok(Int(4))];
assert_eq![Int(-4_isize).sqrt_floor(), Err(NonNegativeRequired)];
§Features
This will only be const if the _cmp_isize feature is enabled.
Sourcepub const fn sqrt_round(self) -> Result<Int<isize>>
pub const fn sqrt_round(self) -> Result<Int<isize>>
Returns the rounded integer square root.
Performs operations internally as isize_up
.
§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_isize).sqrt_round(), Ok(Int(3))];
assert_eq![Int(13_isize).sqrt_round(), Ok(Int(4))];
assert_eq![Int(16_isize).sqrt_round(), Ok(Int(4))];
assert_eq![Int(20_isize).sqrt_round(), Ok(Int(4))];
assert_eq![Int(21_isize).sqrt_round(), Ok(Int(5))];
assert_eq![Int(-4_isize).sqrt_round(), Err(NonNegativeRequired)];
Sourcepub const fn root_ceil(self, nth: u32) -> Result<Int<isize>>
pub const fn root_ceil(self, nth: u32) -> Result<Int<isize>>
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_isize).root_ceil(4), Ok(Int(3))];
assert_eq![Int(70_isize).root_ceil(4), Ok(Int(3))];
assert_eq![Int(81_isize).root_ceil(4), Ok(Int(3))];
assert_eq![Int(88_isize).root_ceil(4), Ok(Int(4))];
assert_eq![Int(114_isize).root_ceil(4), Ok(Int(4))];
assert_eq![Int(-81isize).root_ceil(4), Err(NonNegativeRequired)];
assert_eq![Int(isize::MAX).root_ceil(1), Ok(Int(isize::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<isize>>
pub const fn root_floor(self, nth: u32) -> Result<Int<isize>>
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_isize).root_floor(4), Ok(Int(2))];
assert_eq![Int(70_isize).root_floor(4), Ok(Int(2))];
assert_eq![Int(81_isize).root_floor(4), Ok(Int(3))];
assert_eq![Int(88_isize).root_floor(4), Ok(Int(3))];
assert_eq![Int(114_isize).root_floor(4), Ok(Int(3))];
assert_eq![Int(-81_isize).root_floor(4), Err(NonNegativeRequired)];
assert_eq![Int(isize::MAX).root_floor(1), Ok(Int(isize::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<u8>
Available on crate feature _int_u8
only.
impl Int<u8>
_int_u8
only.§Integer root related methods for u8
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_u8).is_square(), false];
assert_eq![Int(13_u8).is_square(), false];
assert_eq![Int(16_u8).is_square(), true];
assert_eq![Int(20_u8).is_square(), false];
assert_eq![Int(21_u8).is_square(), false];
§Features
This will only be const if the _cmp_u8 feature is enabled.
Sourcepub const fn sqrt_ceil(self) -> Int<u8>
pub const fn sqrt_ceil(self) -> Int<u8>
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_u8).sqrt_ceil(), Int(4)];
assert_eq![Int(13_u8).sqrt_ceil(), Int(4)];
assert_eq![Int(16_u8).sqrt_ceil(), Int(4)];
assert_eq![Int(20_u8).sqrt_ceil(), Int(5)];
assert_eq![Int(21_u8).sqrt_ceil(), Int(5)];
§Features
This will only be const if the _cmp_u8 feature is enabled.
Sourcepub const fn sqrt_floor(self) -> Int<u8>
pub const fn sqrt_floor(self) -> Int<u8>
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_u8).sqrt_floor(), Int(3)];
assert_eq![Int(13_u8).sqrt_floor(), Int(3)];
assert_eq![Int(16_u8).sqrt_floor(), Int(4)];
assert_eq![Int(20_u8).sqrt_floor(), Int(4)];
assert_eq![Int(21_u8).sqrt_floor(), Int(4)];
§Features
This will only be const if the _cmp_u8 feature is enabled.
Sourcepub const fn sqrt_round(self) -> Result<Int<u8>>
pub const fn sqrt_round(self) -> Result<Int<u8>>
Returns the rounded integer square root.
Performs operations internally as u16
.
§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_u8).sqrt_round(), Ok(Int(3))];
assert_eq![Int(13_u8).sqrt_round(), Ok(Int(4))];
assert_eq![Int(16_u8).sqrt_round(), Ok(Int(4))];
assert_eq![Int(20_u8).sqrt_round(), Ok(Int(4))];
assert_eq![Int(21_u8).sqrt_round(), Ok(Int(5))];
Sourcepub const fn root_ceil(self, nth: u32) -> Result<Int<u8>>
pub const fn root_ceil(self, nth: u32) -> Result<Int<u8>>
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_u8).root_ceil(4), Ok(Int(3))];
assert_eq![Int(70_u8).root_ceil(4), Ok(Int(3))];
assert_eq![Int(81_u8).root_ceil(4), Ok(Int(3))];
assert_eq![Int(88_u8).root_ceil(4), Ok(Int(4))];
assert_eq![Int(114_u8).root_ceil(4), Ok(Int(4))];
assert_eq![Int(u8::MAX).root_ceil(1), Ok(Int(u8::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<u8>>
pub const fn root_floor(self, nth: u32) -> Result<Int<u8>>
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_u8).root_floor(4), Ok(Int(2))];
assert_eq![Int(70_u8).root_floor(4), Ok(Int(2))];
assert_eq![Int(81_u8).root_floor(4), Ok(Int(3))];
assert_eq![Int(88_u8).root_floor(4), Ok(Int(3))];
assert_eq![Int(114_u8).root_floor(4), Ok(Int(3))];
assert_eq![Int(u8::MAX).root_floor(1), Ok(Int(u8::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 $.
Source§impl Int<u16>
Available on crate feature _int_u16
only.
impl Int<u16>
_int_u16
only.§Integer root related methods for u16
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_u16).is_square(), false];
assert_eq![Int(13_u16).is_square(), false];
assert_eq![Int(16_u16).is_square(), true];
assert_eq![Int(20_u16).is_square(), false];
assert_eq![Int(21_u16).is_square(), false];
§Features
This will only be const if the _cmp_u16 feature is enabled.
Sourcepub const fn sqrt_ceil(self) -> Int<u16>
pub const fn sqrt_ceil(self) -> Int<u16>
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_u16).sqrt_ceil(), Int(4)];
assert_eq![Int(13_u16).sqrt_ceil(), Int(4)];
assert_eq![Int(16_u16).sqrt_ceil(), Int(4)];
assert_eq![Int(20_u16).sqrt_ceil(), Int(5)];
assert_eq![Int(21_u16).sqrt_ceil(), Int(5)];
§Features
This will only be const if the _cmp_u16 feature is enabled.
Sourcepub const fn sqrt_floor(self) -> Int<u16>
pub const fn sqrt_floor(self) -> Int<u16>
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_u16).sqrt_floor(), Int(3)];
assert_eq![Int(13_u16).sqrt_floor(), Int(3)];
assert_eq![Int(16_u16).sqrt_floor(), Int(4)];
assert_eq![Int(20_u16).sqrt_floor(), Int(4)];
assert_eq![Int(21_u16).sqrt_floor(), Int(4)];
§Features
This will only be const if the _cmp_u16 feature is enabled.
Sourcepub const fn sqrt_round(self) -> Result<Int<u16>>
pub const fn sqrt_round(self) -> Result<Int<u16>>
Returns the rounded integer square root.
Performs operations internally as u32
.
§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_u16).sqrt_round(), Ok(Int(3))];
assert_eq![Int(13_u16).sqrt_round(), Ok(Int(4))];
assert_eq![Int(16_u16).sqrt_round(), Ok(Int(4))];
assert_eq![Int(20_u16).sqrt_round(), Ok(Int(4))];
assert_eq![Int(21_u16).sqrt_round(), Ok(Int(5))];
Sourcepub const fn root_ceil(self, nth: u32) -> Result<Int<u16>>
pub const fn root_ceil(self, nth: u32) -> Result<Int<u16>>
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_u16).root_ceil(4), Ok(Int(3))];
assert_eq![Int(70_u16).root_ceil(4), Ok(Int(3))];
assert_eq![Int(81_u16).root_ceil(4), Ok(Int(3))];
assert_eq![Int(88_u16).root_ceil(4), Ok(Int(4))];
assert_eq![Int(114_u16).root_ceil(4), Ok(Int(4))];
assert_eq![Int(u16::MAX).root_ceil(1), Ok(Int(u16::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<u16>>
pub const fn root_floor(self, nth: u32) -> Result<Int<u16>>
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_u16).root_floor(4), Ok(Int(2))];
assert_eq![Int(70_u16).root_floor(4), Ok(Int(2))];
assert_eq![Int(81_u16).root_floor(4), Ok(Int(3))];
assert_eq![Int(88_u16).root_floor(4), Ok(Int(3))];
assert_eq![Int(114_u16).root_floor(4), Ok(Int(3))];
assert_eq![Int(u16::MAX).root_floor(1), Ok(Int(u16::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 $.
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 $.
Source§impl Int<u64>
Available on crate feature _int_u64
only.
impl Int<u64>
_int_u64
only.§Integer root related methods for u64
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_u64).is_square(), false];
assert_eq![Int(13_u64).is_square(), false];
assert_eq![Int(16_u64).is_square(), true];
assert_eq![Int(20_u64).is_square(), false];
assert_eq![Int(21_u64).is_square(), false];
§Features
This will only be const if the _cmp_u64 feature is enabled.
Sourcepub const fn sqrt_ceil(self) -> Int<u64>
pub const fn sqrt_ceil(self) -> Int<u64>
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_u64).sqrt_ceil(), Int(4)];
assert_eq![Int(13_u64).sqrt_ceil(), Int(4)];
assert_eq![Int(16_u64).sqrt_ceil(), Int(4)];
assert_eq![Int(20_u64).sqrt_ceil(), Int(5)];
assert_eq![Int(21_u64).sqrt_ceil(), Int(5)];
§Features
This will only be const if the _cmp_u64 feature is enabled.
Sourcepub const fn sqrt_floor(self) -> Int<u64>
pub const fn sqrt_floor(self) -> Int<u64>
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_u64).sqrt_floor(), Int(3)];
assert_eq![Int(13_u64).sqrt_floor(), Int(3)];
assert_eq![Int(16_u64).sqrt_floor(), Int(4)];
assert_eq![Int(20_u64).sqrt_floor(), Int(4)];
assert_eq![Int(21_u64).sqrt_floor(), Int(4)];
§Features
This will only be const if the _cmp_u64 feature is enabled.
Sourcepub const fn sqrt_round(self) -> Result<Int<u64>>
pub const fn sqrt_round(self) -> Result<Int<u64>>
Returns the rounded integer square root.
Performs operations internally as u128
.
§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_u64).sqrt_round(), Ok(Int(3))];
assert_eq![Int(13_u64).sqrt_round(), Ok(Int(4))];
assert_eq![Int(16_u64).sqrt_round(), Ok(Int(4))];
assert_eq![Int(20_u64).sqrt_round(), Ok(Int(4))];
assert_eq![Int(21_u64).sqrt_round(), Ok(Int(5))];
Sourcepub const fn root_ceil(self, nth: u32) -> Result<Int<u64>>
pub const fn root_ceil(self, nth: u32) -> Result<Int<u64>>
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_u64).root_ceil(4), Ok(Int(3))];
assert_eq![Int(70_u64).root_ceil(4), Ok(Int(3))];
assert_eq![Int(81_u64).root_ceil(4), Ok(Int(3))];
assert_eq![Int(88_u64).root_ceil(4), Ok(Int(4))];
assert_eq![Int(114_u64).root_ceil(4), Ok(Int(4))];
assert_eq![Int(u64::MAX).root_ceil(1), Ok(Int(u64::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<u64>>
pub const fn root_floor(self, nth: u32) -> Result<Int<u64>>
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_u64).root_floor(4), Ok(Int(2))];
assert_eq![Int(70_u64).root_floor(4), Ok(Int(2))];
assert_eq![Int(81_u64).root_floor(4), Ok(Int(3))];
assert_eq![Int(88_u64).root_floor(4), Ok(Int(3))];
assert_eq![Int(114_u64).root_floor(4), Ok(Int(3))];
assert_eq![Int(u64::MAX).root_floor(1), Ok(Int(u64::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 $.
Source§impl Int<u128>
Available on crate feature _int_u128
only.
impl Int<u128>
_int_u128
only.§Integer root related methods for u128
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_u128).is_square(), false];
assert_eq![Int(13_u128).is_square(), false];
assert_eq![Int(16_u128).is_square(), true];
assert_eq![Int(20_u128).is_square(), false];
assert_eq![Int(21_u128).is_square(), false];
§Features
This will only be const if the _cmp_u128 feature is enabled.
Sourcepub const fn sqrt_ceil(self) -> Int<u128>
pub const fn sqrt_ceil(self) -> Int<u128>
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_u128).sqrt_ceil(), Int(4)];
assert_eq![Int(13_u128).sqrt_ceil(), Int(4)];
assert_eq![Int(16_u128).sqrt_ceil(), Int(4)];
assert_eq![Int(20_u128).sqrt_ceil(), Int(5)];
assert_eq![Int(21_u128).sqrt_ceil(), Int(5)];
§Features
This will only be const if the _cmp_u128 feature is enabled.
Sourcepub const fn sqrt_floor(self) -> Int<u128>
pub const fn sqrt_floor(self) -> Int<u128>
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_u128).sqrt_floor(), Int(3)];
assert_eq![Int(13_u128).sqrt_floor(), Int(3)];
assert_eq![Int(16_u128).sqrt_floor(), Int(4)];
assert_eq![Int(20_u128).sqrt_floor(), Int(4)];
assert_eq![Int(21_u128).sqrt_floor(), Int(4)];
§Features
This will only be const if the _cmp_u128 feature is enabled.
Sourcepub const fn sqrt_round(self) -> Result<Int<u128>>
pub const fn sqrt_round(self) -> Result<Int<u128>>
Returns the rounded integer square root.
Performs operations internally as u128
.
§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_u128).sqrt_round(), Ok(Int(3))];
assert_eq![Int(13_u128).sqrt_round(), Ok(Int(4))];
assert_eq![Int(16_u128).sqrt_round(), Ok(Int(4))];
assert_eq![Int(20_u128).sqrt_round(), Ok(Int(4))];
assert_eq![Int(21_u128).sqrt_round(), Ok(Int(5))];
Sourcepub const fn root_ceil(self, nth: u32) -> Result<Int<u128>>
pub const fn root_ceil(self, nth: u32) -> Result<Int<u128>>
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_u128).root_ceil(4), Ok(Int(3))];
assert_eq![Int(70_u128).root_ceil(4), Ok(Int(3))];
assert_eq![Int(81_u128).root_ceil(4), Ok(Int(3))];
assert_eq![Int(88_u128).root_ceil(4), Ok(Int(4))];
assert_eq![Int(114_u128).root_ceil(4), Ok(Int(4))];
assert_eq![Int(u128::MAX).root_ceil(1), Ok(Int(u128::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<u128>>
pub const fn root_floor(self, nth: u32) -> Result<Int<u128>>
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_u128).root_floor(4), Ok(Int(2))];
assert_eq![Int(70_u128).root_floor(4), Ok(Int(2))];
assert_eq![Int(81_u128).root_floor(4), Ok(Int(3))];
assert_eq![Int(88_u128).root_floor(4), Ok(Int(3))];
assert_eq![Int(114_u128).root_floor(4), Ok(Int(3))];
assert_eq![Int(u128::MAX).root_floor(1), Ok(Int(u128::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 $.
Source§impl Int<usize>
Available on crate feature _int_usize
only.
impl Int<usize>
_int_usize
only.§Integer root related methods for usize
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_usize).is_square(), false];
assert_eq![Int(13_usize).is_square(), false];
assert_eq![Int(16_usize).is_square(), true];
assert_eq![Int(20_usize).is_square(), false];
assert_eq![Int(21_usize).is_square(), false];
Sourcepub const fn sqrt_ceil(self) -> Int<usize>
pub const fn sqrt_ceil(self) -> Int<usize>
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_usize).sqrt_ceil(), Int(4)];
assert_eq![Int(13_usize).sqrt_ceil(), Int(4)];
assert_eq![Int(16_usize).sqrt_ceil(), Int(4)];
assert_eq![Int(20_usize).sqrt_ceil(), Int(5)];
assert_eq![Int(21_usize).sqrt_ceil(), Int(5)];
Sourcepub const fn sqrt_floor(self) -> Int<usize>
pub const fn sqrt_floor(self) -> Int<usize>
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_usize).sqrt_floor(), Int(3)];
assert_eq![Int(13_usize).sqrt_floor(), Int(3)];
assert_eq![Int(16_usize).sqrt_floor(), Int(4)];
assert_eq![Int(20_usize).sqrt_floor(), Int(4)];
assert_eq![Int(21_usize).sqrt_floor(), Int(4)];
Sourcepub const fn sqrt_round(self) -> Result<Int<usize>>
pub const fn sqrt_round(self) -> Result<Int<usize>>
Returns the rounded integer square root.
Performs operations internally as usize_up
.
§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_usize).sqrt_round(), Ok(Int(3))];
assert_eq![Int(13_usize).sqrt_round(), Ok(Int(4))];
assert_eq![Int(16_usize).sqrt_round(), Ok(Int(4))];
assert_eq![Int(20_usize).sqrt_round(), Ok(Int(4))];
assert_eq![Int(21_usize).sqrt_round(), Ok(Int(5))];
Sourcepub const fn root_ceil(self, nth: u32) -> Result<Int<usize>>
pub const fn root_ceil(self, nth: u32) -> Result<Int<usize>>
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_usize).root_ceil(4), Ok(Int(3))];
assert_eq![Int(70_usize).root_ceil(4), Ok(Int(3))];
assert_eq![Int(81_usize).root_ceil(4), Ok(Int(3))];
assert_eq![Int(88_usize).root_ceil(4), Ok(Int(4))];
assert_eq![Int(114_usize).root_ceil(4), Ok(Int(4))];
assert_eq![Int(usize::MAX).root_ceil(1), Ok(Int(usize::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<usize>>
pub const fn root_floor(self, nth: u32) -> Result<Int<usize>>
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_usize).root_floor(4), Ok(Int(2))];
assert_eq![Int(70_usize).root_floor(4), Ok(Int(2))];
assert_eq![Int(81_usize).root_floor(4), Ok(Int(3))];
assert_eq![Int(88_usize).root_floor(4), Ok(Int(3))];
assert_eq![Int(114_usize).root_floor(4), Ok(Int(3))];
assert_eq![Int(usize::MAX).root_floor(1), Ok(Int(usize::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<'s, 'o> Add<&'o Int<isize>> for &'s Int<isize>
Available on crate feature _int_isize
only.
impl<'s, 'o> Add<&'o Int<isize>> for &'s Int<isize>
_int_isize
only.Source§impl<'s, 'o> Add<&'o Int<usize>> for &'s Int<usize>
Available on crate feature _int_usize
only.
impl<'s, 'o> Add<&'o Int<usize>> for &'s Int<usize>
_int_usize
only.Source§impl<'o> AddAssign<&'o i128> for Int<i128>
Available on crate feature _int_i128
only.
impl<'o> AddAssign<&'o i128> for Int<i128>
_int_i128
only.Source§fn add_assign(&mut self, other: &'o i128)
fn add_assign(&mut self, other: &'o i128)
+=
operation. Read moreSource§impl<'o> AddAssign<&'o i16> for Int<i16>
Available on crate feature _int_i16
only.
impl<'o> AddAssign<&'o i16> for Int<i16>
_int_i16
only.Source§fn add_assign(&mut self, other: &'o i16)
fn add_assign(&mut self, other: &'o i16)
+=
operation. Read moreSource§impl<'o> AddAssign<&'o i32> for Int<i32>
Available on crate feature _int_i32
only.
impl<'o> AddAssign<&'o i32> for Int<i32>
_int_i32
only.Source§fn add_assign(&mut self, other: &'o i32)
fn add_assign(&mut self, other: &'o i32)
+=
operation. Read moreSource§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 i8> for Int<i8>
Available on crate feature _int_i8
only.
impl<'o> AddAssign<&'o i8> for Int<i8>
_int_i8
only.Source§fn add_assign(&mut self, other: &'o i8)
fn add_assign(&mut self, other: &'o i8)
+=
operation. Read moreSource§impl<'o> AddAssign<&'o isize> for Int<isize>
Available on crate feature _int_isize
only.
impl<'o> AddAssign<&'o isize> for Int<isize>
_int_isize
only.Source§fn add_assign(&mut self, other: &'o isize)
fn add_assign(&mut self, other: &'o isize)
+=
operation. Read moreSource§impl<'o> AddAssign<&'o u128> for Int<u128>
Available on crate feature _int_u128
only.
impl<'o> AddAssign<&'o u128> for Int<u128>
_int_u128
only.Source§fn add_assign(&mut self, other: &'o u128)
fn add_assign(&mut self, other: &'o u128)
+=
operation. Read moreSource§impl<'o> AddAssign<&'o u16> for Int<u16>
Available on crate feature _int_u16
only.
impl<'o> AddAssign<&'o u16> for Int<u16>
_int_u16
only.Source§fn add_assign(&mut self, other: &'o u16)
fn add_assign(&mut self, other: &'o u16)
+=
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<'o> AddAssign<&'o u64> for Int<u64>
Available on crate feature _int_u64
only.
impl<'o> AddAssign<&'o u64> for Int<u64>
_int_u64
only.Source§fn add_assign(&mut self, other: &'o u64)
fn add_assign(&mut self, other: &'o u64)
+=
operation. Read moreSource§impl<'o> AddAssign<&'o u8> for Int<u8>
Available on crate feature _int_i8
only.
impl<'o> AddAssign<&'o u8> for Int<u8>
_int_i8
only.Source§fn add_assign(&mut self, other: &'o u8)
fn add_assign(&mut self, other: &'o u8)
+=
operation. Read moreSource§impl<'o> AddAssign<&'o usize> for Int<usize>
Available on crate feature _int_usize
only.
impl<'o> AddAssign<&'o usize> for Int<usize>
_int_usize
only.Source§fn add_assign(&mut self, other: &'o usize)
fn add_assign(&mut self, other: &'o usize)
+=
operation. Read moreSource§impl AddAssign<i128> for Int<i128>
Available on crate feature _int_i128
only.
impl AddAssign<i128> for Int<i128>
_int_i128
only.Source§fn add_assign(&mut self, other: i128)
fn add_assign(&mut self, other: i128)
+=
operation. Read moreSource§impl AddAssign<i16> for Int<i16>
Available on crate feature _int_i16
only.
impl AddAssign<i16> for Int<i16>
_int_i16
only.Source§fn add_assign(&mut self, other: i16)
fn add_assign(&mut self, other: i16)
+=
operation. Read moreSource§impl AddAssign<i32> for Int<i32>
Available on crate feature _int_i32
only.
impl AddAssign<i32> for Int<i32>
_int_i32
only.Source§fn add_assign(&mut self, other: i32)
fn add_assign(&mut self, other: i32)
+=
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<i8> for Int<i8>
Available on crate feature _int_i8
only.
impl AddAssign<i8> for Int<i8>
_int_i8
only.Source§fn add_assign(&mut self, other: i8)
fn add_assign(&mut self, other: i8)
+=
operation. Read moreSource§impl AddAssign<isize> for Int<isize>
Available on crate feature _int_isize
only.
impl AddAssign<isize> for Int<isize>
_int_isize
only.Source§fn add_assign(&mut self, other: isize)
fn add_assign(&mut self, other: isize)
+=
operation. Read moreSource§impl AddAssign<u128> for Int<u128>
Available on crate feature _int_u128
only.
impl AddAssign<u128> for Int<u128>
_int_u128
only.Source§fn add_assign(&mut self, other: u128)
fn add_assign(&mut self, other: u128)
+=
operation. Read moreSource§impl AddAssign<u16> for Int<u16>
Available on crate feature _int_u16
only.
impl AddAssign<u16> for Int<u16>
_int_u16
only.Source§fn add_assign(&mut self, other: u16)
fn add_assign(&mut self, other: u16)
+=
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 AddAssign<u64> for Int<u64>
Available on crate feature _int_u64
only.
impl AddAssign<u64> for Int<u64>
_int_u64
only.Source§fn add_assign(&mut self, other: u64)
fn add_assign(&mut self, other: u64)
+=
operation. Read moreSource§impl AddAssign<u8> for Int<u8>
Available on crate feature _int_i8
only.
impl AddAssign<u8> for Int<u8>
_int_i8
only.Source§fn add_assign(&mut self, other: u8)
fn add_assign(&mut self, other: u8)
+=
operation. Read moreSource§impl AddAssign<usize> for Int<usize>
Available on crate feature _int_usize
only.
impl AddAssign<usize> for Int<usize>
_int_usize
only.Source§fn add_assign(&mut self, other: usize)
fn add_assign(&mut self, other: usize)
+=
operation. Read moreSource§impl<'s, 'o> Div<&'o Int<isize>> for &'s Int<isize>
Available on crate feature _int_isize
only.
impl<'s, 'o> Div<&'o Int<isize>> for &'s Int<isize>
_int_isize
only.Source§impl<'s, 'o> Div<&'o Int<usize>> for &'s Int<usize>
Available on crate feature _int_usize
only.
impl<'s, 'o> Div<&'o Int<usize>> for &'s Int<usize>
_int_usize
only.Source§impl<'o> DivAssign<&'o i128> for Int<i128>
Available on crate feature _int_i128
only.
impl<'o> DivAssign<&'o i128> for Int<i128>
_int_i128
only.Source§fn div_assign(&mut self, other: &'o i128)
fn div_assign(&mut self, other: &'o i128)
/=
operation. Read moreSource§impl<'o> DivAssign<&'o i16> for Int<i16>
Available on crate feature _int_i16
only.
impl<'o> DivAssign<&'o i16> for Int<i16>
_int_i16
only.Source§fn div_assign(&mut self, other: &'o i16)
fn div_assign(&mut self, other: &'o i16)
/=
operation. Read moreSource§impl<'o> DivAssign<&'o i32> for Int<i32>
Available on crate feature _int_i32
only.
impl<'o> DivAssign<&'o i32> for Int<i32>
_int_i32
only.Source§fn div_assign(&mut self, other: &'o i32)
fn div_assign(&mut self, other: &'o i32)
/=
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 i8> for Int<i8>
Available on crate feature _int_i8
only.
impl<'o> DivAssign<&'o i8> for Int<i8>
_int_i8
only.Source§fn div_assign(&mut self, other: &'o i8)
fn div_assign(&mut self, other: &'o i8)
/=
operation. Read moreSource§impl<'o> DivAssign<&'o isize> for Int<isize>
Available on crate feature _int_isize
only.
impl<'o> DivAssign<&'o isize> for Int<isize>
_int_isize
only.Source§fn div_assign(&mut self, other: &'o isize)
fn div_assign(&mut self, other: &'o isize)
/=
operation. Read moreSource§impl<'o> DivAssign<&'o u128> for Int<u128>
Available on crate feature _int_u128
only.
impl<'o> DivAssign<&'o u128> for Int<u128>
_int_u128
only.Source§fn div_assign(&mut self, other: &'o u128)
fn div_assign(&mut self, other: &'o u128)
/=
operation. Read moreSource§impl<'o> DivAssign<&'o u16> for Int<u16>
Available on crate feature _int_u16
only.
impl<'o> DivAssign<&'o u16> for Int<u16>
_int_u16
only.Source§fn div_assign(&mut self, other: &'o u16)
fn div_assign(&mut self, other: &'o u16)
/=
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<'o> DivAssign<&'o u64> for Int<u64>
Available on crate feature _int_u64
only.
impl<'o> DivAssign<&'o u64> for Int<u64>
_int_u64
only.Source§fn div_assign(&mut self, other: &'o u64)
fn div_assign(&mut self, other: &'o u64)
/=
operation. Read moreSource§impl<'o> DivAssign<&'o u8> for Int<u8>
Available on crate feature _int_i8
only.
impl<'o> DivAssign<&'o u8> for Int<u8>
_int_i8
only.Source§fn div_assign(&mut self, other: &'o u8)
fn div_assign(&mut self, other: &'o u8)
/=
operation. Read moreSource§impl<'o> DivAssign<&'o usize> for Int<usize>
Available on crate feature _int_usize
only.
impl<'o> DivAssign<&'o usize> for Int<usize>
_int_usize
only.Source§fn div_assign(&mut self, other: &'o usize)
fn div_assign(&mut self, other: &'o usize)
/=
operation. Read moreSource§impl DivAssign<i128> for Int<i128>
Available on crate feature _int_i128
only.
impl DivAssign<i128> for Int<i128>
_int_i128
only.Source§fn div_assign(&mut self, other: i128)
fn div_assign(&mut self, other: i128)
/=
operation. Read moreSource§impl DivAssign<i16> for Int<i16>
Available on crate feature _int_i16
only.
impl DivAssign<i16> for Int<i16>
_int_i16
only.Source§fn div_assign(&mut self, other: i16)
fn div_assign(&mut self, other: i16)
/=
operation. Read moreSource§impl DivAssign<i32> for Int<i32>
Available on crate feature _int_i32
only.
impl DivAssign<i32> for Int<i32>
_int_i32
only.Source§fn div_assign(&mut self, other: i32)
fn div_assign(&mut self, other: i32)
/=
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<i8> for Int<i8>
Available on crate feature _int_i8
only.
impl DivAssign<i8> for Int<i8>
_int_i8
only.Source§fn div_assign(&mut self, other: i8)
fn div_assign(&mut self, other: i8)
/=
operation. Read moreSource§impl DivAssign<isize> for Int<isize>
Available on crate feature _int_isize
only.
impl DivAssign<isize> for Int<isize>
_int_isize
only.Source§fn div_assign(&mut self, other: isize)
fn div_assign(&mut self, other: isize)
/=
operation. Read moreSource§impl DivAssign<u128> for Int<u128>
Available on crate feature _int_u128
only.
impl DivAssign<u128> for Int<u128>
_int_u128
only.Source§fn div_assign(&mut self, other: u128)
fn div_assign(&mut self, other: u128)
/=
operation. Read moreSource§impl DivAssign<u16> for Int<u16>
Available on crate feature _int_u16
only.
impl DivAssign<u16> for Int<u16>
_int_u16
only.Source§fn div_assign(&mut self, other: u16)
fn div_assign(&mut self, other: u16)
/=
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 DivAssign<u64> for Int<u64>
Available on crate feature _int_u64
only.
impl DivAssign<u64> for Int<u64>
_int_u64
only.Source§fn div_assign(&mut self, other: u64)
fn div_assign(&mut self, other: u64)
/=
operation. Read moreSource§impl DivAssign<u8> for Int<u8>
Available on crate feature _int_i8
only.
impl DivAssign<u8> for Int<u8>
_int_i8
only.Source§fn div_assign(&mut self, other: u8)
fn div_assign(&mut self, other: u8)
/=
operation. Read moreSource§impl DivAssign<usize> for Int<usize>
Available on crate feature _int_usize
only.
impl DivAssign<usize> for Int<usize>
_int_usize
only.Source§fn div_assign(&mut self, other: usize)
fn div_assign(&mut self, other: usize)
/=
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<'s, 'o> Mul<&'o Int<isize>> for &'s Int<isize>
Available on crate feature _int_isize
only.
impl<'s, 'o> Mul<&'o Int<isize>> for &'s Int<isize>
_int_isize
only.Source§impl<'s, 'o> Mul<&'o Int<usize>> for &'s Int<usize>
Available on crate feature _int_usize
only.
impl<'s, 'o> Mul<&'o Int<usize>> for &'s Int<usize>
_int_usize
only.Source§impl<'o> MulAssign<&'o i128> for Int<i128>
Available on crate feature _int_i128
only.
impl<'o> MulAssign<&'o i128> for Int<i128>
_int_i128
only.Source§fn mul_assign(&mut self, other: &'o i128)
fn mul_assign(&mut self, other: &'o i128)
*=
operation. Read moreSource§impl<'o> MulAssign<&'o i16> for Int<i16>
Available on crate feature _int_i16
only.
impl<'o> MulAssign<&'o i16> for Int<i16>
_int_i16
only.Source§fn mul_assign(&mut self, other: &'o i16)
fn mul_assign(&mut self, other: &'o i16)
*=
operation. Read moreSource§impl<'o> MulAssign<&'o i32> for Int<i32>
Available on crate feature _int_i32
only.
impl<'o> MulAssign<&'o i32> for Int<i32>
_int_i32
only.Source§fn mul_assign(&mut self, other: &'o i32)
fn mul_assign(&mut self, other: &'o i32)
*=
operation. Read moreSource§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 i8> for Int<i8>
Available on crate feature _int_i8
only.
impl<'o> MulAssign<&'o i8> for Int<i8>
_int_i8
only.Source§fn mul_assign(&mut self, other: &'o i8)
fn mul_assign(&mut self, other: &'o i8)
*=
operation. Read moreSource§impl<'o> MulAssign<&'o isize> for Int<isize>
Available on crate feature _int_isize
only.
impl<'o> MulAssign<&'o isize> for Int<isize>
_int_isize
only.Source§fn mul_assign(&mut self, other: &'o isize)
fn mul_assign(&mut self, other: &'o isize)
*=
operation. Read moreSource§impl<'o> MulAssign<&'o u128> for Int<u128>
Available on crate feature _int_u128
only.
impl<'o> MulAssign<&'o u128> for Int<u128>
_int_u128
only.Source§fn mul_assign(&mut self, other: &'o u128)
fn mul_assign(&mut self, other: &'o u128)
*=
operation. Read moreSource§impl<'o> MulAssign<&'o u16> for Int<u16>
Available on crate feature _int_u16
only.
impl<'o> MulAssign<&'o u16> for Int<u16>
_int_u16
only.Source§fn mul_assign(&mut self, other: &'o u16)
fn mul_assign(&mut self, other: &'o u16)
*=
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<'o> MulAssign<&'o u64> for Int<u64>
Available on crate feature _int_u64
only.
impl<'o> MulAssign<&'o u64> for Int<u64>
_int_u64
only.Source§fn mul_assign(&mut self, other: &'o u64)
fn mul_assign(&mut self, other: &'o u64)
*=
operation. Read moreSource§impl<'o> MulAssign<&'o u8> for Int<u8>
Available on crate feature _int_i8
only.
impl<'o> MulAssign<&'o u8> for Int<u8>
_int_i8
only.Source§fn mul_assign(&mut self, other: &'o u8)
fn mul_assign(&mut self, other: &'o u8)
*=
operation. Read moreSource§impl<'o> MulAssign<&'o usize> for Int<usize>
Available on crate feature _int_usize
only.
impl<'o> MulAssign<&'o usize> for Int<usize>
_int_usize
only.Source§fn mul_assign(&mut self, other: &'o usize)
fn mul_assign(&mut self, other: &'o usize)
*=
operation. Read moreSource§impl MulAssign<i128> for Int<i128>
Available on crate feature _int_i128
only.
impl MulAssign<i128> for Int<i128>
_int_i128
only.Source§fn mul_assign(&mut self, other: i128)
fn mul_assign(&mut self, other: i128)
*=
operation. Read moreSource§impl MulAssign<i16> for Int<i16>
Available on crate feature _int_i16
only.
impl MulAssign<i16> for Int<i16>
_int_i16
only.Source§fn mul_assign(&mut self, other: i16)
fn mul_assign(&mut self, other: i16)
*=
operation. Read moreSource§impl MulAssign<i32> for Int<i32>
Available on crate feature _int_i32
only.
impl MulAssign<i32> for Int<i32>
_int_i32
only.Source§fn mul_assign(&mut self, other: i32)
fn mul_assign(&mut self, other: i32)
*=
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<i8> for Int<i8>
Available on crate feature _int_i8
only.
impl MulAssign<i8> for Int<i8>
_int_i8
only.Source§fn mul_assign(&mut self, other: i8)
fn mul_assign(&mut self, other: i8)
*=
operation. Read moreSource§impl MulAssign<isize> for Int<isize>
Available on crate feature _int_isize
only.
impl MulAssign<isize> for Int<isize>
_int_isize
only.Source§fn mul_assign(&mut self, other: isize)
fn mul_assign(&mut self, other: isize)
*=
operation. Read moreSource§impl MulAssign<u128> for Int<u128>
Available on crate feature _int_u128
only.
impl MulAssign<u128> for Int<u128>
_int_u128
only.Source§fn mul_assign(&mut self, other: u128)
fn mul_assign(&mut self, other: u128)
*=
operation. Read moreSource§impl MulAssign<u16> for Int<u16>
Available on crate feature _int_u16
only.
impl MulAssign<u16> for Int<u16>
_int_u16
only.Source§fn mul_assign(&mut self, other: u16)
fn mul_assign(&mut self, other: u16)
*=
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 MulAssign<u64> for Int<u64>
Available on crate feature _int_u64
only.
impl MulAssign<u64> for Int<u64>
_int_u64
only.Source§fn mul_assign(&mut self, other: u64)
fn mul_assign(&mut self, other: u64)
*=
operation. Read moreSource§impl MulAssign<u8> for Int<u8>
Available on crate feature _int_i8
only.
impl MulAssign<u8> for Int<u8>
_int_i8
only.Source§fn mul_assign(&mut self, other: u8)
fn mul_assign(&mut self, other: u8)
*=
operation. Read moreSource§impl MulAssign<usize> for Int<usize>
Available on crate feature _int_usize
only.
impl MulAssign<usize> for Int<usize>
_int_usize
only.Source§fn mul_assign(&mut self, other: usize)
fn mul_assign(&mut self, other: usize)
*=
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<'s, 'o> Rem<&'o Int<isize>> for &'s Int<isize>
Available on crate feature _int_isize
only.
impl<'s, 'o> Rem<&'o Int<isize>> for &'s Int<isize>
_int_isize
only.Source§impl<'s, 'o> Rem<&'o Int<usize>> for &'s Int<usize>
Available on crate feature _int_usize
only.
impl<'s, 'o> Rem<&'o Int<usize>> for &'s Int<usize>
_int_usize
only.Source§impl<'o> RemAssign<&'o i128> for Int<i128>
Available on crate feature _int_i128
only.
impl<'o> RemAssign<&'o i128> for Int<i128>
_int_i128
only.Source§fn rem_assign(&mut self, other: &'o i128)
fn rem_assign(&mut self, other: &'o i128)
%=
operation. Read moreSource§impl<'o> RemAssign<&'o i16> for Int<i16>
Available on crate feature _int_i16
only.
impl<'o> RemAssign<&'o i16> for Int<i16>
_int_i16
only.Source§fn rem_assign(&mut self, other: &'o i16)
fn rem_assign(&mut self, other: &'o i16)
%=
operation. Read moreSource§impl<'o> RemAssign<&'o i32> for Int<i32>
Available on crate feature _int_i32
only.
impl<'o> RemAssign<&'o i32> for Int<i32>
_int_i32
only.Source§fn rem_assign(&mut self, other: &'o i32)
fn rem_assign(&mut self, other: &'o i32)
%=
operation. Read moreSource§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 i8> for Int<i8>
Available on crate feature _int_i8
only.
impl<'o> RemAssign<&'o i8> for Int<i8>
_int_i8
only.Source§fn rem_assign(&mut self, other: &'o i8)
fn rem_assign(&mut self, other: &'o i8)
%=
operation. Read moreSource§impl<'o> RemAssign<&'o isize> for Int<isize>
Available on crate feature _int_isize
only.
impl<'o> RemAssign<&'o isize> for Int<isize>
_int_isize
only.Source§fn rem_assign(&mut self, other: &'o isize)
fn rem_assign(&mut self, other: &'o isize)
%=
operation. Read moreSource§impl<'o> RemAssign<&'o u128> for Int<u128>
Available on crate feature _int_u128
only.
impl<'o> RemAssign<&'o u128> for Int<u128>
_int_u128
only.Source§fn rem_assign(&mut self, other: &'o u128)
fn rem_assign(&mut self, other: &'o u128)
%=
operation. Read moreSource§impl<'o> RemAssign<&'o u16> for Int<u16>
Available on crate feature _int_u16
only.
impl<'o> RemAssign<&'o u16> for Int<u16>
_int_u16
only.Source§fn rem_assign(&mut self, other: &'o u16)
fn rem_assign(&mut self, other: &'o u16)
%=
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<'o> RemAssign<&'o u64> for Int<u64>
Available on crate feature _int_u64
only.
impl<'o> RemAssign<&'o u64> for Int<u64>
_int_u64
only.Source§fn rem_assign(&mut self, other: &'o u64)
fn rem_assign(&mut self, other: &'o u64)
%=
operation. Read moreSource§impl<'o> RemAssign<&'o u8> for Int<u8>
Available on crate feature _int_i8
only.
impl<'o> RemAssign<&'o u8> for Int<u8>
_int_i8
only.Source§fn rem_assign(&mut self, other: &'o u8)
fn rem_assign(&mut self, other: &'o u8)
%=
operation. Read moreSource§impl<'o> RemAssign<&'o usize> for Int<usize>
Available on crate feature _int_usize
only.
impl<'o> RemAssign<&'o usize> for Int<usize>
_int_usize
only.Source§fn rem_assign(&mut self, other: &'o usize)
fn rem_assign(&mut self, other: &'o usize)
%=
operation. Read moreSource§impl RemAssign<i128> for Int<i128>
Available on crate feature _int_i128
only.
impl RemAssign<i128> for Int<i128>
_int_i128
only.Source§fn rem_assign(&mut self, other: i128)
fn rem_assign(&mut self, other: i128)
%=
operation. Read moreSource§impl RemAssign<i16> for Int<i16>
Available on crate feature _int_i16
only.
impl RemAssign<i16> for Int<i16>
_int_i16
only.Source§fn rem_assign(&mut self, other: i16)
fn rem_assign(&mut self, other: i16)
%=
operation. Read moreSource§impl RemAssign<i32> for Int<i32>
Available on crate feature _int_i32
only.
impl RemAssign<i32> for Int<i32>
_int_i32
only.Source§fn rem_assign(&mut self, other: i32)
fn rem_assign(&mut self, other: i32)
%=
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<i8> for Int<i8>
Available on crate feature _int_i8
only.
impl RemAssign<i8> for Int<i8>
_int_i8
only.Source§fn rem_assign(&mut self, other: i8)
fn rem_assign(&mut self, other: i8)
%=
operation. Read moreSource§impl RemAssign<isize> for Int<isize>
Available on crate feature _int_isize
only.
impl RemAssign<isize> for Int<isize>
_int_isize
only.Source§fn rem_assign(&mut self, other: isize)
fn rem_assign(&mut self, other: isize)
%=
operation. Read moreSource§impl RemAssign<u128> for Int<u128>
Available on crate feature _int_u128
only.
impl RemAssign<u128> for Int<u128>
_int_u128
only.Source§fn rem_assign(&mut self, other: u128)
fn rem_assign(&mut self, other: u128)
%=
operation. Read moreSource§impl RemAssign<u16> for Int<u16>
Available on crate feature _int_u16
only.
impl RemAssign<u16> for Int<u16>
_int_u16
only.Source§fn rem_assign(&mut self, other: u16)
fn rem_assign(&mut self, other: u16)
%=
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 RemAssign<u64> for Int<u64>
Available on crate feature _int_u64
only.
impl RemAssign<u64> for Int<u64>
_int_u64
only.Source§fn rem_assign(&mut self, other: u64)
fn rem_assign(&mut self, other: u64)
%=
operation. Read moreSource§impl RemAssign<u8> for Int<u8>
Available on crate feature _int_i8
only.
impl RemAssign<u8> for Int<u8>
_int_i8
only.Source§fn rem_assign(&mut self, other: u8)
fn rem_assign(&mut self, other: u8)
%=
operation. Read moreSource§impl RemAssign<usize> for Int<usize>
Available on crate feature _int_usize
only.
impl RemAssign<usize> for Int<usize>
_int_usize
only.Source§fn rem_assign(&mut self, other: usize)
fn rem_assign(&mut self, other: usize)
%=
operation. Read moreSource§impl<'s, 'o> Sub<&'o Int<isize>> for &'s Int<isize>
Available on crate feature _int_isize
only.
impl<'s, 'o> Sub<&'o Int<isize>> for &'s Int<isize>
_int_isize
only.Source§impl<'s, 'o> Sub<&'o Int<usize>> for &'s Int<usize>
Available on crate feature _int_usize
only.
impl<'s, 'o> Sub<&'o Int<usize>> for &'s Int<usize>
_int_usize
only.Source§impl<'o> SubAssign<&'o i128> for Int<i128>
Available on crate feature _int_i128
only.
impl<'o> SubAssign<&'o i128> for Int<i128>
_int_i128
only.Source§fn sub_assign(&mut self, other: &'o i128)
fn sub_assign(&mut self, other: &'o i128)
-=
operation. Read moreSource§impl<'o> SubAssign<&'o i16> for Int<i16>
Available on crate feature _int_i16
only.
impl<'o> SubAssign<&'o i16> for Int<i16>
_int_i16
only.Source§fn sub_assign(&mut self, other: &'o i16)
fn sub_assign(&mut self, other: &'o i16)
-=
operation. Read moreSource§impl<'o> SubAssign<&'o i32> for Int<i32>
Available on crate feature _int_i32
only.
impl<'o> SubAssign<&'o i32> for Int<i32>
_int_i32
only.Source§fn sub_assign(&mut self, other: &'o i32)
fn sub_assign(&mut self, other: &'o i32)
-=
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 i8> for Int<i8>
Available on crate feature _int_i8
only.
impl<'o> SubAssign<&'o i8> for Int<i8>
_int_i8
only.Source§fn sub_assign(&mut self, other: &'o i8)
fn sub_assign(&mut self, other: &'o i8)
-=
operation. Read moreSource§impl<'o> SubAssign<&'o isize> for Int<isize>
Available on crate feature _int_isize
only.
impl<'o> SubAssign<&'o isize> for Int<isize>
_int_isize
only.Source§fn sub_assign(&mut self, other: &'o isize)
fn sub_assign(&mut self, other: &'o isize)
-=
operation. Read moreSource§impl<'o> SubAssign<&'o u128> for Int<u128>
Available on crate feature _int_u128
only.
impl<'o> SubAssign<&'o u128> for Int<u128>
_int_u128
only.Source§fn sub_assign(&mut self, other: &'o u128)
fn sub_assign(&mut self, other: &'o u128)
-=
operation. Read moreSource§impl<'o> SubAssign<&'o u16> for Int<u16>
Available on crate feature _int_u16
only.
impl<'o> SubAssign<&'o u16> for Int<u16>
_int_u16
only.Source§fn sub_assign(&mut self, other: &'o u16)
fn sub_assign(&mut self, other: &'o u16)
-=
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<'o> SubAssign<&'o u64> for Int<u64>
Available on crate feature _int_u64
only.
impl<'o> SubAssign<&'o u64> for Int<u64>
_int_u64
only.Source§fn sub_assign(&mut self, other: &'o u64)
fn sub_assign(&mut self, other: &'o u64)
-=
operation. Read moreSource§impl<'o> SubAssign<&'o u8> for Int<u8>
Available on crate feature _int_i8
only.
impl<'o> SubAssign<&'o u8> for Int<u8>
_int_i8
only.Source§fn sub_assign(&mut self, other: &'o u8)
fn sub_assign(&mut self, other: &'o u8)
-=
operation. Read moreSource§impl<'o> SubAssign<&'o usize> for Int<usize>
Available on crate feature _int_usize
only.
impl<'o> SubAssign<&'o usize> for Int<usize>
_int_usize
only.Source§fn sub_assign(&mut self, other: &'o usize)
fn sub_assign(&mut self, other: &'o usize)
-=
operation. Read moreSource§impl SubAssign<i128> for Int<i128>
Available on crate feature _int_i128
only.
impl SubAssign<i128> for Int<i128>
_int_i128
only.Source§fn sub_assign(&mut self, other: i128)
fn sub_assign(&mut self, other: i128)
-=
operation. Read moreSource§impl SubAssign<i16> for Int<i16>
Available on crate feature _int_i16
only.
impl SubAssign<i16> for Int<i16>
_int_i16
only.Source§fn sub_assign(&mut self, other: i16)
fn sub_assign(&mut self, other: i16)
-=
operation. Read moreSource§impl SubAssign<i32> for Int<i32>
Available on crate feature _int_i32
only.
impl SubAssign<i32> for Int<i32>
_int_i32
only.Source§fn sub_assign(&mut self, other: i32)
fn sub_assign(&mut self, other: i32)
-=
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<i8> for Int<i8>
Available on crate feature _int_i8
only.
impl SubAssign<i8> for Int<i8>
_int_i8
only.Source§fn sub_assign(&mut self, other: i8)
fn sub_assign(&mut self, other: i8)
-=
operation. Read moreSource§impl SubAssign<isize> for Int<isize>
Available on crate feature _int_isize
only.
impl SubAssign<isize> for Int<isize>
_int_isize
only.Source§fn sub_assign(&mut self, other: isize)
fn sub_assign(&mut self, other: isize)
-=
operation. Read moreSource§impl SubAssign<u128> for Int<u128>
Available on crate feature _int_u128
only.
impl SubAssign<u128> for Int<u128>
_int_u128
only.Source§fn sub_assign(&mut self, other: u128)
fn sub_assign(&mut self, other: u128)
-=
operation. Read moreSource§impl SubAssign<u16> for Int<u16>
Available on crate feature _int_u16
only.
impl SubAssign<u16> for Int<u16>
_int_u16
only.Source§fn sub_assign(&mut self, other: u16)
fn sub_assign(&mut self, other: u16)
-=
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 moreSource§impl SubAssign<u64> for Int<u64>
Available on crate feature _int_u64
only.
impl SubAssign<u64> for Int<u64>
_int_u64
only.Source§fn sub_assign(&mut self, other: u64)
fn sub_assign(&mut self, other: u64)
-=
operation. Read moreSource§impl SubAssign<u8> for Int<u8>
Available on crate feature _int_i8
only.
impl SubAssign<u8> for Int<u8>
_int_i8
only.Source§fn sub_assign(&mut self, other: u8)
fn sub_assign(&mut self, other: u8)
-=
operation. Read moreSource§impl SubAssign<usize> for Int<usize>
Available on crate feature _int_usize
only.
impl SubAssign<usize> for Int<usize>
_int_usize
only.Source§fn sub_assign(&mut self, other: usize)
fn sub_assign(&mut self, other: usize)
-=
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§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
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 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_val(&self) -> usize ⓘ
fn mem_align_of_val(&self) -> 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§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError> ⓘ
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError> ⓘ
§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out
indicating that a T
is niched.