Skip to main content

BigInt

Struct BigInt 

Source
pub struct BigInt { /* private fields */ }
Expand description

An arbitrary-precision integer.

Implementations§

Source§

impl BigInt

Source

pub fn zero() -> BigInt

Zero.

Source

pub fn one() -> BigInt

One.

Source

pub fn from_i64(value: i64) -> BigInt

The value of an i64.

Source

pub fn from_f64_truncated(value: f64) -> BigInt

The value of an f64 truncated toward zero, which the relational comparison needs.

Only called for a finite value whose magnitude is below 1.7e38, so the exponent is bounded and the word count stays small.

Source

pub fn is_zero(&self) -> bool

Whether this is zero.

Source

pub fn is_one(&self) -> bool

Whether this is one.

Source

pub fn is_negative(&self) -> bool

Whether the value is below zero.

Source

pub fn to_f64(&self) -> f64

The value as an f64, for the relational comparisons that accept a Number.

Rounding is the platform’s, which is what BigInt < Number needs: the comparison is on the mathematical values, and an f64 cannot tell 2^60 from 2^60 + 1 anyway.

Source

pub fn to_i64(&self) -> Option<i64>

The value as an i64 when it fits, which BigInt.asIntN-free paths use.

Source

pub fn from_u64(value: u64) -> BigInt

The value of a u64.

Source

pub fn to_u64_wrapping(&self) -> u64

The low 64 bits, as the two’s-complement bit pattern.

BigInt::asIntN(64, v) / asUintN(64, v): a typed array stores the value modulo 2^64, so new BigUint64Array(1)[0] = -1n reads back 2^64-1 and a value that needs more than 64 bits keeps only its low ones.

Source

pub fn to_i64_wrapping(&self) -> i64

to_u64_wrapping reinterpreted as signed.

Source

pub fn from_radix(text: &str, radix: u32) -> Option<BigInt>

Parse text in radix, which is what a literal and BigInt("…") both need.

Surrounding whitespace is not accepted here; the caller trims it, because the literal grammar and the string grammar differ in what they allow around the digits.

Source

pub fn from_string(text: &str) -> Option<BigInt>

Parse a JS BigInt string: optional sign, optional 0x/0o/0b, no fraction.

The grammar is StringIntegerLiteral, which is stricter than it looks and is why trim only happens at the very edges:

  • whitespace may surround the literal, but not sit between the sign and the digits — "- 1" and " +0x10 " are both SyntaxErrors, not -1 and 16;
  • a radix prefix has to follow the sign directly, with no whitespace between;
  • an empty literal (or all whitespace) is 0, but a bare sign is not.
Source

pub fn negate(&self) -> BigInt

The negation.

Source

pub fn abs(&self) -> BigInt

The absolute value.

Source

pub fn add(&self, other: &BigInt) -> BigInt

Signed addition.

Source

pub fn sub(&self, other: &BigInt) -> BigInt

Signed subtraction.

Source

pub fn mul(&self, other: &BigInt) -> BigInt

Signed multiplication.

Source

pub fn div_rem(&self, other: &BigInt) -> Option<(BigInt, BigInt)>

Signed division, truncating toward zero, with the remainder.

None for a zero divisor, which the caller turns into a RangeError.

Source

pub fn pow(&self, exponent: &BigInt) -> Option<BigInt>

self raised to exponent, by repeated squaring.

Source

pub fn shl(&self, bits: u64) -> BigInt

self << bits.

Source

pub fn shr(&self, bits: u64) -> BigInt

self >> bits, arithmetic — the same as floor(self / 2^bits).

Source

pub fn bitand(&self, other: &BigInt) -> BigInt

self & other, on the infinite two’s-complement form.

Source

pub fn bitor(&self, other: &BigInt) -> BigInt

self | other.

Source

pub fn bitxor(&self, other: &BigInt) -> BigInt

self ^ other.

Source

pub fn bitnot(&self) -> BigInt

~self.

Source

pub fn as_uint_n(&self, bits: u64) -> BigInt

BigInt.asUintN(bits, self).

Source

pub fn as_int_n(&self, bits: u64) -> BigInt

BigInt.asIntN(bits, self).

Source

pub fn to_decimal_string(&self) -> String

The decimal text, which is what toString() and the printer need.

Source

pub fn to_radix_string(&self, radix: u32) -> String

The text in radix.

Trait Implementations§

Source§

impl Clone for BigInt

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BigInt

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for BigInt

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for BigInt

Source§

impl Ord for BigInt

Source§

fn cmp(&self, other: &BigInt) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for BigInt

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for BigInt

Source§

fn partial_cmp(&self, other: &BigInt) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for BigInt

Source§

impl Trace for BigInt

Source§

fn trace(&self, _tracer: &mut Tracer<'_>)

Nothing in a bignum is a GC value: it is a sign and machine words.

Auto Trait Implementations§

§

impl Forget for BigInt

§

impl Freeze for BigInt

§

impl Move for BigInt

§

impl RefUnwindSafe for BigInt

§

impl Send for BigInt

§

impl Sync for BigInt

§

impl Unpin for BigInt

§

impl UnsafeUnpin for BigInt

§

impl UnwindSafe for BigInt

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.