zena:bigint
import {…} from 'zena:bigint';
Arbitrary-precision signed integers (BigInt).
Values are immutable and represented in sign-and-magnitude form over
normalized little-endian u64 limbs. Multiplication switches from
schoolbook to Karatsuba at 32 limbs, division uses a single-limb fast path
and Knuth's Algorithm D over u64 limbs, and bitwise operations follow
infinite two's-complement semantics.
Examples ​
import { BigInt } from 'zena:bigint';
let a = BigInt.from('123456789012345678901234567890');
let b = BigInt.from(1000000007);
let (q, r) = a.divRem(b);
let p = a.modPow(BigInt.from(65537), b);
Classes
BigInt
final class BigInt implements Hashable, Stringable
An immutable arbitrary-precision signed integer.
static ZERO: BigInt
The additive identity 0.
static ONE: BigInt
The multiplicative identity 1.
sign: i32 { get; }
-1 if negative, 0 if zero, 1 if positive.
isZero: boolean { get; }
Whether this value equals 0.
isNegative: boolean { get; }
Whether this value is strictly less than 0.
isPositive: boolean { get; }
Whether this value is strictly greater than 0.
isEven: boolean { get; }
Whether this value is divisible by 2.
isOdd: boolean { get; }
Whether this value leaves remainder 1 or -1 modulo 2.
static from(value: i32): BigInt
Converts a signed 32-bit integer to BigInt.
static from(value: i64): BigInt
Converts a signed 64-bit integer to BigInt.
static from(value: u64): BigInt
Converts an unsigned 64-bit integer to BigInt.
static from(text: String, radix: i32 = 0): BigInt
Parses text in radix (2..36, or 0 to detect 0x, 0b, 0o
prefixes and otherwise default to base 10). Throws Error if radix is
out of range or text is invalid.
static fromI64(value: i64): BigInt
Converts a signed 64-bit integer to BigInt.
static fromU64(value: u64): BigInt
Converts an unsigned 64-bit integer to BigInt.
static parse(text: String, radix: i32 = 0): BigInt | null
Parses text in radix (2..36, or 0 to auto-detect 0x/0X,
0b/0B, and 0o/0O prefixes with base-10 fallback), returning null
when text is empty, radix is invalid, or any character is out of range.
bitLength(): i32
Minimum number of bits needed to represent the magnitude (0 for zero).
neg(): BigInt
Returns -this.
abs(): BigInt
Returns |this|.
divRem(other: BigInt): inline (BigInt, BigInt)
Truncating integer division and remainder (quotient, remainder) satisfying
quotient * other + remainder == this, with remainder carrying the sign
of this. Throws Error when other == 0.
div(other: BigInt): BigInt
Truncating integer quotient of this divided by other (rounded toward
zero). Throws Error when other == 0.
pow(exp: i64): BigInt
Raises this to non-negative 64-bit exponent exp (0 ** 0 == 1).
Throws Error when exp < 0.
compareTo(other: BigInt): i32
Returns -1 if this < other, 0 if equal, or 1 if this > other.
min(other: BigInt): BigInt
Returns the smaller of this and other.
max(other: BigInt): BigInt
Returns the larger of this and other.
hashCode(): i32
Content hash consistent with operator ==.
bitNot(): BigInt
Infinite two's-complement bitwise NOT (-(this + 1)).
gcd(other: BigInt): BigInt
Non-negative greatest common divisor of this and other.
modPow(exp: BigInt, mod: BigInt): BigInt
Modular exponentiation this^exp mod mod in [0, mod).
Throws Error if exp < 0 or mod <= 0.
tryToI64(): inline (true, i64) | inline (false, _)
Converts to i64 if within [I64_MIN, I64_MAX], returning (true, value)
or (false, _) on overflow.
toI64(): i64
Converts to i64, throwing Error if out of [I64_MIN, I64_MAX].
tryToU64(): inline (true, u64) | inline (false, _)
Converts to u64 if within [0, U64_MAX], returning (true, value) or
(false, _) on negative values or overflow.
toU64(): u64
Converts to u64, throwing Error if negative or greater than U64_MAX.
toString(radix: i32 = 10): String
Formats this value in radix (2..36) using lowercase digits a..z.
Throws Error when radix is outside 2..36.
operator +(other: BigInt): BigInt
Sum this + other.
operator -(other: BigInt): BigInt
Difference this - other.
operator *(other: BigInt): BigInt
Product this * other.
operator %(other: BigInt): BigInt
Remainder of truncating division this - this.div(other) * other, carrying
the sign of this. Throws Error when other == 0.
operator **(exp: i32): BigInt
Raises this to non-negative exponent exp (0 ** 0 == 1).
Throws Error when exp < 0.
operator ==(other: BigInt): boolean
Value equality.
operator &(other: BigInt): BigInt
Infinite two's-complement bitwise AND.
operator |(other: BigInt): BigInt
Infinite two's-complement bitwise OR.
operator ^(other: BigInt): BigInt
Infinite two's-complement bitwise XOR.
operator <<(bits: i32): BigInt
Shifts left by bits (this * 2^bits). Throws Error when bits < 0.
operator >>(bits: i32): BigInt
Arithmetic right shift by bits (floor(this / 2^bits)).
Throws Error when bits < 0.