pub struct BigInt { /* private fields */ }Expand description
An arbitrary-precision integer.
Implementations§
Source§impl BigInt
impl BigInt
Sourcepub fn from_f64_truncated(value: f64) -> BigInt
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.
Sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
Whether the value is below zero.
Sourcepub fn to_f64(&self) -> f64
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.
Sourcepub fn to_i64(&self) -> Option<i64>
pub fn to_i64(&self) -> Option<i64>
The value as an i64 when it fits, which BigInt.asIntN-free paths use.
Sourcepub fn to_u64_wrapping(&self) -> u64
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.
Sourcepub fn to_i64_wrapping(&self) -> i64
pub fn to_i64_wrapping(&self) -> i64
to_u64_wrapping reinterpreted as signed.
Sourcepub fn from_radix(text: &str, radix: u32) -> Option<BigInt>
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.
Sourcepub fn from_string(text: &str) -> Option<BigInt>
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 bothSyntaxErrors, not-1and16; - 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.
Sourcepub fn div_rem(&self, other: &BigInt) -> Option<(BigInt, BigInt)>
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.
Sourcepub fn pow(&self, exponent: &BigInt) -> Option<BigInt>
pub fn pow(&self, exponent: &BigInt) -> Option<BigInt>
self raised to exponent, by repeated squaring.
Sourcepub fn shr(&self, bits: u64) -> BigInt
pub fn shr(&self, bits: u64) -> BigInt
self >> bits, arithmetic — the same as floor(self / 2^bits).
Sourcepub fn bitand(&self, other: &BigInt) -> BigInt
pub fn bitand(&self, other: &BigInt) -> BigInt
self & other, on the infinite two’s-complement form.
Sourcepub fn to_decimal_string(&self) -> String
pub fn to_decimal_string(&self) -> String
The decimal text, which is what toString() and the printer need.
Sourcepub fn to_radix_string(&self, radix: u32) -> String
pub fn to_radix_string(&self, radix: u32) -> String
The text in radix.