pub struct BigInt { /* private fields */ }Expand description
Implementations§
Source§impl BigInt
impl BigInt
Sourcepub fn to_signed_bytes_be(&self) -> Vec<u8> ⓘ
pub fn to_signed_bytes_be(&self) -> Vec<u8> ⓘ
Returns the two’s-complement big-endian byte representation of this value, using the minimal number of bytes.
§Examples
use oxinum_int::native::BigInt;
assert_eq!(BigInt::from(0i64).to_signed_bytes_be(), vec![0u8]);
assert_eq!(BigInt::from(1i64).to_signed_bytes_be(), vec![1u8]);
assert_eq!(BigInt::from(-1i64).to_signed_bytes_be(), vec![0xFFu8]);
assert_eq!(BigInt::from(127i64).to_signed_bytes_be(), vec![0x7Fu8]);
assert_eq!(BigInt::from(-128i64).to_signed_bytes_be(), vec![0x80u8]);
assert_eq!(BigInt::from(128i64).to_signed_bytes_be(), vec![0x00u8, 0x80]);
assert_eq!(BigInt::from(129i64).to_signed_bytes_be(), vec![0x00u8, 0x81]);
assert_eq!(BigInt::from(-129i64).to_signed_bytes_be(), vec![0xFFu8, 0x7F]);Sourcepub fn to_signed_bytes_le(&self) -> Vec<u8> ⓘ
pub fn to_signed_bytes_le(&self) -> Vec<u8> ⓘ
Returns the two’s-complement little-endian byte representation of this value, using the minimal number of bytes.
§Examples
use oxinum_int::native::BigInt;
assert_eq!(BigInt::from(0i64).to_signed_bytes_le(), vec![0u8]);
assert_eq!(BigInt::from(1i64).to_signed_bytes_le(), vec![1u8]);
assert_eq!(BigInt::from(-1i64).to_signed_bytes_le(), vec![0xFFu8]);
assert_eq!(BigInt::from(128i64).to_signed_bytes_le(), vec![0x80u8, 0x00]);
assert_eq!(BigInt::from(-129i64).to_signed_bytes_le(), vec![0x7Fu8, 0xFF]);Sourcepub fn from_signed_bytes_be(bytes: &[u8]) -> BigInt
pub fn from_signed_bytes_be(bytes: &[u8]) -> BigInt
Construct a BigInt from a two’s-complement big-endian byte slice.
An empty slice decodes as zero.
§Examples
use oxinum_int::native::BigInt;
assert_eq!(BigInt::from_signed_bytes_be(&[]), BigInt::zero());
assert_eq!(BigInt::from_signed_bytes_be(&[0x00]), BigInt::zero());
assert_eq!(BigInt::from_signed_bytes_be(&[0x01]), BigInt::from(1i64));
assert_eq!(BigInt::from_signed_bytes_be(&[0xFF]), BigInt::from(-1i64));
assert_eq!(BigInt::from_signed_bytes_be(&[0x80]), BigInt::from(-128i64));
assert_eq!(BigInt::from_signed_bytes_be(&[0xFF, 0x7F]), BigInt::from(-129i64));Sourcepub fn from_signed_bytes_le(bytes: &[u8]) -> BigInt
pub fn from_signed_bytes_le(bytes: &[u8]) -> BigInt
Construct a BigInt from a two’s-complement little-endian byte slice.
An empty slice decodes as zero.
§Examples
use oxinum_int::native::BigInt;
assert_eq!(BigInt::from_signed_bytes_le(&[]), BigInt::zero());
assert_eq!(BigInt::from_signed_bytes_le(&[0xFF]), BigInt::from(-1i64));
assert_eq!(BigInt::from_signed_bytes_le(&[0x7F, 0xFF]), BigInt::from(-129i64));Source§impl BigInt
impl BigInt
Sourcepub fn zero() -> BigInt
pub fn zero() -> BigInt
Construct a zero BigInt.
§Examples
use oxinum_int::native::BigInt;
assert!(BigInt::zero().is_zero());Sourcepub fn one() -> BigInt
pub fn one() -> BigInt
Construct a BigInt equal to 1.
§Examples
use oxinum_int::native::BigInt;
assert!(BigInt::one().is_one());Sourcepub fn from_parts(sign: Sign, mag: BigUint) -> BigInt
pub fn from_parts(sign: Sign, mag: BigUint) -> BigInt
Construct from an existing (sign, magnitude) pair. Re-canonicalizes
zero so that BigInt::from_parts(Sign::Negative, BigUint::ZERO) is
indistinguishable from BigInt::zero().
§Examples
use oxinum_int::native::{BigInt, BigUint};
use oxinum_core::Sign;
let a = BigInt::from_parts(Sign::Negative, BigUint::from_u64(7));
assert_eq!(format!("{a}"), "-7");
// -0 canonicalizes to +0.
let neg_zero = BigInt::from_parts(Sign::Negative, BigUint::ZERO);
assert_eq!(neg_zero, BigInt::zero());Sourcepub fn into_parts(self) -> (Sign, BigUint)
pub fn into_parts(self) -> (Sign, BigUint)
Decompose into (sign, magnitude). For zero, the returned sign is
always Sign::Positive.
§Examples
use oxinum_int::native::{BigInt, BigUint};
use oxinum_core::Sign;
let n = BigInt::from(-42i64);
let (s, m) = n.into_parts();
assert_eq!(s, Sign::Negative);
assert_eq!(m, BigUint::from_u64(42));Sourcepub fn sign(&self) -> Sign
pub fn sign(&self) -> Sign
Returns the sign of this number. For zero, returns Sign::Positive
(canonical-zero invariant).
Sourcepub fn signum(&self) -> Sign
pub fn signum(&self) -> Sign
Returns the sign as a method that follows the standard signum
convention: +1, -1, or 0-as-Positive. Use Self::sign for
the raw sign enum.
§Examples
use oxinum_int::native::BigInt;
use oxinum_core::Sign;
assert_eq!(BigInt::from(5i64).signum(), Sign::Positive);
assert_eq!(BigInt::from(-3i64).signum(), Sign::Negative);
// Zero is canonically positive in dashu_base::Sign.
assert_eq!(BigInt::zero().signum(), Sign::Positive);Sourcepub fn magnitude(&self) -> &BigUint
pub fn magnitude(&self) -> &BigUint
Returns a reference to the magnitude (always non-negative).
Sourcepub fn abs(&self) -> BigInt
pub fn abs(&self) -> BigInt
Returns the absolute value as a non-negative BigInt.
§Examples
use oxinum_int::native::BigInt;
assert_eq!(BigInt::from(-42i64).abs(), BigInt::from(42i64));
assert_eq!(BigInt::from(42i64).abs(), BigInt::from(42i64));Sourcepub fn is_one(&self) -> bool
pub fn is_one(&self) -> bool
Returns true if this value is +1 (sign positive AND magnitude one).
Sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
Returns true if this value is strictly negative.
Sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
Returns true if this value is strictly positive.
Source§impl BigInt
impl BigInt
Sourcepub fn nth_root(&self, n: u32) -> Result<BigInt, OxiNumError>
pub fn nth_root(&self, n: u32) -> Result<BigInt, OxiNumError>
Integer n-th root for signed values.
n == 0: error (OxiNumError::Precision).- Negative self with even
n: error (no real even root of a negative integer). - Negative self with odd
n: returns the unique negative root. - Otherwise: floor of the positive real root.
§Examples
use oxinum_int::native::BigInt;
assert_eq!(BigInt::from(-8i64).nth_root(3).unwrap(), BigInt::from(-2i64));
assert_eq!(BigInt::from(27i64).nth_root(3).unwrap(), BigInt::from(3i64));
assert!(BigInt::from(-4i64).nth_root(2).is_err());
assert!(BigInt::from(10i64).nth_root(0).is_err());Trait Implementations§
Source§impl AddAssign for BigInt
impl AddAssign for BigInt
Source§fn add_assign(&mut self, rhs: BigInt)
fn add_assign(&mut self, rhs: BigInt)
+= operation. Read moreSource§impl AddAssign<&BigInt> for BigInt
impl AddAssign<&BigInt> for BigInt
Source§fn add_assign(&mut self, rhs: &BigInt)
fn add_assign(&mut self, rhs: &BigInt)
+= operation. Read moreSource§impl DivAssign for BigInt
impl DivAssign for BigInt
Source§fn div_assign(&mut self, rhs: BigInt)
fn div_assign(&mut self, rhs: BigInt)
/= operation. Read moreSource§impl DivAssign<&BigInt> for BigInt
impl DivAssign<&BigInt> for BigInt
Source§fn div_assign(&mut self, rhs: &BigInt)
fn div_assign(&mut self, rhs: &BigInt)
/= operation. Read moreimpl Eq for BigInt
Source§impl From<&BigInt> for BigRational
impl From<&BigInt> for BigRational
Source§fn from(n: &BigInt) -> BigRational
fn from(n: &BigInt) -> BigRational
Source§impl From<BigInt> for BigRational
impl From<BigInt> for BigRational
Source§fn from(n: BigInt) -> BigRational
fn from(n: BigInt) -> BigRational
Source§impl FromRadix for BigInt
impl FromRadix for BigInt
Source§fn from_radix(src: &str, radix: u32) -> Result<BigInt, OxiNumError>
fn from_radix(src: &str, radix: u32) -> Result<BigInt, OxiNumError>
Source§impl MulAssign for BigInt
impl MulAssign for BigInt
Source§fn mul_assign(&mut self, rhs: BigInt)
fn mul_assign(&mut self, rhs: BigInt)
*= operation. Read moreSource§impl MulAssign<&BigInt> for BigInt
impl MulAssign<&BigInt> for BigInt
Source§fn mul_assign(&mut self, rhs: &BigInt)
fn mul_assign(&mut self, rhs: &BigInt)
*= operation. Read moreSource§impl Ord for BigInt
impl Ord for BigInt
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for BigInt
impl PartialOrd for BigInt
Source§impl RemAssign for BigInt
impl RemAssign for BigInt
Source§fn rem_assign(&mut self, rhs: BigInt)
fn rem_assign(&mut self, rhs: BigInt)
%= operation. Read moreSource§impl RemAssign<&BigInt> for BigInt
impl RemAssign<&BigInt> for BigInt
Source§fn rem_assign(&mut self, rhs: &BigInt)
fn rem_assign(&mut self, rhs: &BigInt)
%= operation. Read moreSource§impl SubAssign for BigInt
impl SubAssign for BigInt
Source§fn sub_assign(&mut self, rhs: BigInt)
fn sub_assign(&mut self, rhs: BigInt)
-= operation. Read moreSource§impl SubAssign<&BigInt> for BigInt
impl SubAssign<&BigInt> for BigInt
Source§fn sub_assign(&mut self, rhs: &BigInt)
fn sub_assign(&mut self, rhs: &BigInt)
-= operation. Read more