#[repr(transparent)]pub struct Int(pub BigInt);Expand description
A wrapper type for arbitrary-precision signed integers (Int)
Tuple Fields§
§0: BigIntImplementations§
Methods from Deref<Target = StdBigInt>§
pub const ZERO: BigInt
Sourcepub fn to_bytes_be(&self) -> (Sign, Vec<u8>)
pub fn to_bytes_be(&self) -> (Sign, Vec<u8>)
Sourcepub fn to_bytes_le(&self) -> (Sign, Vec<u8>)
pub fn to_bytes_le(&self) -> (Sign, Vec<u8>)
Sourcepub fn to_u32_digits(&self) -> (Sign, Vec<u32>)
pub fn to_u32_digits(&self) -> (Sign, Vec<u32>)
Returns the sign and the u32 digits representation of the BigInt ordered least
significant digit first.
§Examples
use num_bigint::{BigInt, Sign};
assert_eq!(BigInt::from(-1125).to_u32_digits(), (Sign::Minus, vec![1125]));
assert_eq!(BigInt::from(4294967295u32).to_u32_digits(), (Sign::Plus, vec![4294967295]));
assert_eq!(BigInt::from(4294967296u64).to_u32_digits(), (Sign::Plus, vec![0, 1]));
assert_eq!(BigInt::from(-112500000000i64).to_u32_digits(), (Sign::Minus, vec![830850304, 26]));
assert_eq!(BigInt::from(112500000000i64).to_u32_digits(), (Sign::Plus, vec![830850304, 26]));Sourcepub fn to_u64_digits(&self) -> (Sign, Vec<u64>)
pub fn to_u64_digits(&self) -> (Sign, Vec<u64>)
Returns the sign and the u64 digits representation of the BigInt ordered least
significant digit first.
§Examples
use num_bigint::{BigInt, Sign};
assert_eq!(BigInt::from(-1125).to_u64_digits(), (Sign::Minus, vec![1125]));
assert_eq!(BigInt::from(4294967295u32).to_u64_digits(), (Sign::Plus, vec![4294967295]));
assert_eq!(BigInt::from(4294967296u64).to_u64_digits(), (Sign::Plus, vec![4294967296]));
assert_eq!(BigInt::from(-112500000000i64).to_u64_digits(), (Sign::Minus, vec![112500000000]));
assert_eq!(BigInt::from(112500000000i64).to_u64_digits(), (Sign::Plus, vec![112500000000]));
assert_eq!(BigInt::from(1u128 << 64).to_u64_digits(), (Sign::Plus, vec![0, 1]));Sourcepub fn iter_u32_digits(&self) -> U32Digits<'_>
pub fn iter_u32_digits(&self) -> U32Digits<'_>
Returns an iterator of u32 digits representation of the BigInt ordered least
significant digit first.
§Examples
use num_bigint::BigInt;
assert_eq!(BigInt::from(-1125).iter_u32_digits().collect::<Vec<u32>>(), vec![1125]);
assert_eq!(BigInt::from(4294967295u32).iter_u32_digits().collect::<Vec<u32>>(), vec![4294967295]);
assert_eq!(BigInt::from(4294967296u64).iter_u32_digits().collect::<Vec<u32>>(), vec![0, 1]);
assert_eq!(BigInt::from(-112500000000i64).iter_u32_digits().collect::<Vec<u32>>(), vec![830850304, 26]);
assert_eq!(BigInt::from(112500000000i64).iter_u32_digits().collect::<Vec<u32>>(), vec![830850304, 26]);Sourcepub fn iter_u64_digits(&self) -> U64Digits<'_>
pub fn iter_u64_digits(&self) -> U64Digits<'_>
Returns an iterator of u64 digits representation of the BigInt ordered least
significant digit first.
§Examples
use num_bigint::BigInt;
assert_eq!(BigInt::from(-1125).iter_u64_digits().collect::<Vec<u64>>(), vec![1125u64]);
assert_eq!(BigInt::from(4294967295u32).iter_u64_digits().collect::<Vec<u64>>(), vec![4294967295u64]);
assert_eq!(BigInt::from(4294967296u64).iter_u64_digits().collect::<Vec<u64>>(), vec![4294967296u64]);
assert_eq!(BigInt::from(-112500000000i64).iter_u64_digits().collect::<Vec<u64>>(), vec![112500000000u64]);
assert_eq!(BigInt::from(112500000000i64).iter_u64_digits().collect::<Vec<u64>>(), vec![112500000000u64]);
assert_eq!(BigInt::from(1u128 << 64).iter_u64_digits().collect::<Vec<u64>>(), vec![0, 1]);Sourcepub fn to_signed_bytes_be(&self) -> Vec<u8> ⓘ
pub fn to_signed_bytes_be(&self) -> Vec<u8> ⓘ
Sourcepub fn to_signed_bytes_le(&self) -> Vec<u8> ⓘ
pub fn to_signed_bytes_le(&self) -> Vec<u8> ⓘ
Sourcepub fn to_str_radix(&self, radix: u32) -> String
pub fn to_str_radix(&self, radix: u32) -> String
Returns the integer formatted as a string in the given radix.
radix must be in the range 2...36.
§Examples
use num_bigint::BigInt;
let i = BigInt::parse_bytes(b"ff", 16).unwrap();
assert_eq!(i.to_str_radix(16), "ff");Sourcepub fn to_radix_be(&self, radix: u32) -> (Sign, Vec<u8>)
pub fn to_radix_be(&self, radix: u32) -> (Sign, Vec<u8>)
Returns the integer in the requested base in big-endian digit order.
The output is not given in a human readable alphabet but as a zero
based u8 number.
radix must be in the range 2...256.
§Examples
use num_bigint::{BigInt, Sign};
assert_eq!(BigInt::from(-0xFFFFi64).to_radix_be(159),
(Sign::Minus, vec![2, 94, 27]));
// 0xFFFF = 65535 = 2*(159^2) + 94*159 + 27Sourcepub fn to_radix_le(&self, radix: u32) -> (Sign, Vec<u8>)
pub fn to_radix_le(&self, radix: u32) -> (Sign, Vec<u8>)
Returns the integer in the requested base in little-endian digit order.
The output is not given in a human readable alphabet but as a zero
based u8 number.
radix must be in the range 2...256.
§Examples
use num_bigint::{BigInt, Sign};
assert_eq!(BigInt::from(-0xFFFFi64).to_radix_le(159),
(Sign::Minus, vec![27, 94, 2]));
// 0xFFFF = 65535 = 27 + 94*159 + 2*(159^2)Sourcepub fn magnitude(&self) -> &BigUint
pub fn magnitude(&self) -> &BigUint
Returns the magnitude of the BigInt as a BigUint.
§Examples
use num_bigint::{BigInt, BigUint};
use num_traits::Zero;
assert_eq!(BigInt::from(1234).magnitude(), &BigUint::from(1234u32));
assert_eq!(BigInt::from(-4321).magnitude(), &BigUint::from(4321u32));
assert!(BigInt::ZERO.magnitude().is_zero());Sourcepub fn bits(&self) -> u64
pub fn bits(&self) -> u64
Determines the fewest bits necessary to express the BigInt,
not including the sign.
Sourcepub fn to_biguint(&self) -> Option<BigUint>
pub fn to_biguint(&self) -> Option<BigUint>
pub fn checked_add(&self, v: &BigInt) -> Option<BigInt>
pub fn checked_sub(&self, v: &BigInt) -> Option<BigInt>
pub fn checked_mul(&self, v: &BigInt) -> Option<BigInt>
pub fn checked_div(&self, v: &BigInt) -> Option<BigInt>
Sourcepub fn modpow(&self, exponent: &BigInt, modulus: &BigInt) -> BigInt
pub fn modpow(&self, exponent: &BigInt, modulus: &BigInt) -> BigInt
Returns (self ^ exponent) mod modulus
Note that this rounds like mod_floor, not like the % operator,
which makes a difference when given a negative self or modulus.
The result will be in the interval [0, modulus) for modulus > 0,
or in the interval (modulus, 0] for modulus < 0
Panics if the exponent is negative or the modulus is zero.
Sourcepub fn modinv(&self, modulus: &BigInt) -> Option<BigInt>
pub fn modinv(&self, modulus: &BigInt) -> Option<BigInt>
Returns the modular multiplicative inverse if it exists, otherwise None.
This solves for x such that self * x ≡ 1 (mod modulus).
Note that this rounds like mod_floor, not like the % operator,
which makes a difference when given a negative self or modulus.
The solution will be in the interval [0, modulus) for modulus > 0,
or in the interval (modulus, 0] for modulus < 0,
and it exists if and only if gcd(self, modulus) == 1.
use num_bigint::BigInt;
use num_integer::Integer;
use num_traits::{One, Zero};
let m = BigInt::from(383);
// Trivial cases
assert_eq!(BigInt::zero().modinv(&m), None);
assert_eq!(BigInt::one().modinv(&m), Some(BigInt::one()));
let neg1 = &m - 1u32;
assert_eq!(neg1.modinv(&m), Some(neg1));
// Positive self and modulus
let a = BigInt::from(271);
let x = a.modinv(&m).unwrap();
assert_eq!(x, BigInt::from(106));
assert_eq!(x.modinv(&m).unwrap(), a);
assert_eq!((&a * x).mod_floor(&m), BigInt::one());
// Negative self and positive modulus
let b = -&a;
let x = b.modinv(&m).unwrap();
assert_eq!(x, BigInt::from(277));
assert_eq!((&b * x).mod_floor(&m), BigInt::one());
// Positive self and negative modulus
let n = -&m;
let x = a.modinv(&n).unwrap();
assert_eq!(x, BigInt::from(-277));
assert_eq!((&a * x).mod_floor(&n), &n + 1);
// Negative self and modulus
let x = b.modinv(&n).unwrap();
assert_eq!(x, BigInt::from(-106));
assert_eq!((&b * x).mod_floor(&n), &n + 1);Sourcepub fn sqrt(&self) -> BigInt
pub fn sqrt(&self) -> BigInt
Returns the truncated principal square root of self –
see num_integer::Roots::sqrt().
Sourcepub fn cbrt(&self) -> BigInt
pub fn cbrt(&self) -> BigInt
Returns the truncated principal cube root of self –
see num_integer::Roots::cbrt().
Sourcepub fn nth_root(&self, n: u32) -> BigInt
pub fn nth_root(&self, n: u32) -> BigInt
Returns the truncated principal nth root of self –
See num_integer::Roots::nth_root().
Sourcepub fn trailing_zeros(&self) -> Option<u64>
pub fn trailing_zeros(&self) -> Option<u64>
Returns the number of least-significant bits that are zero,
or None if the entire number is zero.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Int
impl<'de> Deserialize<'de> for Int
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Ord for Int
impl Ord for Int
Source§impl PartialOrd for Int
impl PartialOrd for Int
Source§impl SafeAdd for Int
impl SafeAdd for Int
fn checked_add(&self, r: &Self) -> Option<Self>
fn saturating_add(&self, r: &Self) -> Self
fn wrapping_add(&self, r: &Self) -> Self
Source§impl SafeConvert<Decimal> for Int
impl SafeConvert<Decimal> for Int
fn checked_convert(self) -> Option<Decimal>
fn saturating_convert(self) -> Decimal
fn wrapping_convert(self) -> Decimal
Source§impl SafeConvert<Int> for Decimal
impl SafeConvert<Int> for Decimal
fn checked_convert(self) -> Option<Int>
fn saturating_convert(self) -> Int
fn wrapping_convert(self) -> Int
Source§impl SafeConvert<Int> for Int
impl SafeConvert<Int> for Int
fn checked_convert(self) -> Option<Int>
fn saturating_convert(self) -> Int
fn wrapping_convert(self) -> Int
Source§impl SafeConvert<Int> for Uint
impl SafeConvert<Int> for Uint
fn checked_convert(self) -> Option<Int>
fn saturating_convert(self) -> Int
fn wrapping_convert(self) -> Int
Source§impl SafeConvert<Int> for f32
impl SafeConvert<Int> for f32
fn checked_convert(self) -> Option<Int>
fn saturating_convert(self) -> Int
fn wrapping_convert(self) -> Int
Source§impl SafeConvert<Int> for f64
impl SafeConvert<Int> for f64
fn checked_convert(self) -> Option<Int>
fn saturating_convert(self) -> Int
fn wrapping_convert(self) -> Int
Source§impl SafeConvert<Int> for i128
impl SafeConvert<Int> for i128
fn checked_convert(self) -> Option<Int>
fn saturating_convert(self) -> Int
fn wrapping_convert(self) -> Int
Source§impl SafeConvert<Int> for i16
impl SafeConvert<Int> for i16
fn checked_convert(self) -> Option<Int>
fn saturating_convert(self) -> Int
fn wrapping_convert(self) -> Int
Source§impl SafeConvert<Int> for i32
impl SafeConvert<Int> for i32
fn checked_convert(self) -> Option<Int>
fn saturating_convert(self) -> Int
fn wrapping_convert(self) -> Int
Source§impl SafeConvert<Int> for i64
impl SafeConvert<Int> for i64
fn checked_convert(self) -> Option<Int>
fn saturating_convert(self) -> Int
fn wrapping_convert(self) -> Int
Source§impl SafeConvert<Int> for i8
impl SafeConvert<Int> for i8
fn checked_convert(self) -> Option<Int>
fn saturating_convert(self) -> Int
fn wrapping_convert(self) -> Int
Source§impl SafeConvert<Int> for u128
impl SafeConvert<Int> for u128
fn checked_convert(self) -> Option<Int>
fn saturating_convert(self) -> Int
fn wrapping_convert(self) -> Int
Source§impl SafeConvert<Int> for u16
impl SafeConvert<Int> for u16
fn checked_convert(self) -> Option<Int>
fn saturating_convert(self) -> Int
fn wrapping_convert(self) -> Int
Source§impl SafeConvert<Int> for u32
impl SafeConvert<Int> for u32
fn checked_convert(self) -> Option<Int>
fn saturating_convert(self) -> Int
fn wrapping_convert(self) -> Int
Source§impl SafeConvert<Int> for u64
impl SafeConvert<Int> for u64
fn checked_convert(self) -> Option<Int>
fn saturating_convert(self) -> Int
fn wrapping_convert(self) -> Int
Source§impl SafeConvert<Int> for u8
impl SafeConvert<Int> for u8
fn checked_convert(self) -> Option<Int>
fn saturating_convert(self) -> Int
fn wrapping_convert(self) -> Int
Source§impl SafeConvert<Uint> for Int
impl SafeConvert<Uint> for Int
fn checked_convert(self) -> Option<Uint>
fn saturating_convert(self) -> Uint
fn wrapping_convert(self) -> Uint
Source§impl SafeConvert<f32> for Int
impl SafeConvert<f32> for Int
fn checked_convert(self) -> Option<f32>
fn saturating_convert(self) -> f32
fn wrapping_convert(self) -> f32
Source§impl SafeConvert<f64> for Int
impl SafeConvert<f64> for Int
fn checked_convert(self) -> Option<f64>
fn saturating_convert(self) -> f64
fn wrapping_convert(self) -> f64
Source§impl SafeConvert<i128> for Int
impl SafeConvert<i128> for Int
fn checked_convert(self) -> Option<i128>
fn saturating_convert(self) -> i128
fn wrapping_convert(self) -> i128
Source§impl SafeConvert<i16> for Int
impl SafeConvert<i16> for Int
fn checked_convert(self) -> Option<i16>
fn saturating_convert(self) -> i16
fn wrapping_convert(self) -> i16
Source§impl SafeConvert<i32> for Int
impl SafeConvert<i32> for Int
fn checked_convert(self) -> Option<i32>
fn saturating_convert(self) -> i32
fn wrapping_convert(self) -> i32
Source§impl SafeConvert<i64> for Int
impl SafeConvert<i64> for Int
fn checked_convert(self) -> Option<i64>
fn saturating_convert(self) -> i64
fn wrapping_convert(self) -> i64
Source§impl SafeConvert<i8> for Int
impl SafeConvert<i8> for Int
fn checked_convert(self) -> Option<i8>
fn saturating_convert(self) -> i8
fn wrapping_convert(self) -> i8
Source§impl SafeConvert<u128> for Int
impl SafeConvert<u128> for Int
fn checked_convert(self) -> Option<u128>
fn saturating_convert(self) -> u128
fn wrapping_convert(self) -> u128
Source§impl SafeConvert<u16> for Int
impl SafeConvert<u16> for Int
fn checked_convert(self) -> Option<u16>
fn saturating_convert(self) -> u16
fn wrapping_convert(self) -> u16
Source§impl SafeConvert<u32> for Int
impl SafeConvert<u32> for Int
fn checked_convert(self) -> Option<u32>
fn saturating_convert(self) -> u32
fn wrapping_convert(self) -> u32
Source§impl SafeConvert<u64> for Int
impl SafeConvert<u64> for Int
fn checked_convert(self) -> Option<u64>
fn saturating_convert(self) -> u64
fn wrapping_convert(self) -> u64
Source§impl SafeConvert<u8> for Int
impl SafeConvert<u8> for Int
fn checked_convert(self) -> Option<u8>
fn saturating_convert(self) -> u8
fn wrapping_convert(self) -> u8
Source§impl SafeDiv for Int
impl SafeDiv for Int
fn checked_div(&self, r: &Self) -> Option<Self>
fn saturating_div(&self, r: &Self) -> Self
fn wrapping_div(&self, r: &Self) -> Self
Source§impl SafeMul for Int
impl SafeMul for Int
fn checked_mul(&self, r: &Self) -> Option<Self>
fn saturating_mul(&self, r: &Self) -> Self
fn wrapping_mul(&self, r: &Self) -> Self
Source§impl SafeRemainder for Int
impl SafeRemainder for Int
fn checked_rem(&self, r: &Self) -> Option<Self>
fn saturating_rem(&self, r: &Self) -> Self
fn wrapping_rem(&self, r: &Self) -> Self
Source§impl SafeSub for Int
impl SafeSub for Int
fn checked_sub(&self, r: &Self) -> Option<Self>
fn saturating_sub(&self, r: &Self) -> Self
fn wrapping_sub(&self, r: &Self) -> Self
Source§impl TryFromValue for Int
impl TryFromValue for Int
Source§fn try_from_value(value: &Value) -> Result<Self, FromValueError>
fn try_from_value(value: &Value) -> Result<Self, FromValueError>
impl Eq for Int
impl IsInt for Int
impl IsNumber for Int
impl StructuralPartialEq for Int
Auto Trait Implementations§
impl Freeze for Int
impl RefUnwindSafe for Int
impl Send for Int
impl Sync for Int
impl Unpin for Int
impl UnwindSafe for Int
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)