On this page

zena:bigint

zena
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 ​

zena
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

zena
final class BigInt implements Hashable, Stringable
Implements Hashable, Stringable

An immutable arbitrary-precision signed integer.

Properties
zena
static ZERO: BigInt
#

The additive identity 0.

zena
static ONE: BigInt
#

The multiplicative identity 1.

zena
sign: i32 { get; }
#

-1 if negative, 0 if zero, 1 if positive.

zena
isZero: boolean { get; }
#

Whether this value equals 0.

zena
isNegative: boolean { get; }
#

Whether this value is strictly less than 0.

zena
isPositive: boolean { get; }
#

Whether this value is strictly greater than 0.

zena
isEven: boolean { get; }
#

Whether this value is divisible by 2.

zena
isOdd: boolean { get; }
#

Whether this value leaves remainder 1 or -1 modulo 2.

Methods
zena
static from(value: i32): BigInt
#

Converts a signed 32-bit integer to BigInt.

zena
static from(value: i64): BigInt
#

Converts a signed 64-bit integer to BigInt.

zena
static from(value: u64): BigInt
#

Converts an unsigned 64-bit integer to BigInt.

zena
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.

zena
static fromI64(value: i64): BigInt
#

Converts a signed 64-bit integer to BigInt.

zena
static fromU64(value: u64): BigInt
#

Converts an unsigned 64-bit integer to BigInt.

zena
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.

zena
bitLength(): i32
#

Minimum number of bits needed to represent the magnitude (0 for zero).

zena
neg(): BigInt
#

Returns -this.

zena
abs(): BigInt
#

Returns |this|.

zena
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.

zena
div(other: BigInt): BigInt
#

Truncating integer quotient of this divided by other (rounded toward zero). Throws Error when other == 0.

zena
pow(exp: i64): BigInt
#

Raises this to non-negative 64-bit exponent exp (0 ** 0 == 1). Throws Error when exp < 0.

zena
compareTo(other: BigInt): i32
#

Returns -1 if this < other, 0 if equal, or 1 if this > other.

zena
min(other: BigInt): BigInt
#

Returns the smaller of this and other.

zena
max(other: BigInt): BigInt
#

Returns the larger of this and other.

zena
hashCode(): i32
#

Content hash consistent with operator ==.

zena
bitNot(): BigInt
#

Infinite two's-complement bitwise NOT (-(this + 1)).

zena
gcd(other: BigInt): BigInt
#

Non-negative greatest common divisor of this and other.

zena
modPow(exp: BigInt, mod: BigInt): BigInt
#

Modular exponentiation this^exp mod mod in [0, mod). Throws Error if exp < 0 or mod <= 0.

zena
tryToI64(): inline (true, i64) | inline (false, _)
#

Converts to i64 if within [I64_MIN, I64_MAX], returning (true, value) or (false, _) on overflow.

zena
toI64(): i64
#

Converts to i64, throwing Error if out of [I64_MIN, I64_MAX].

zena
tryToU64(): inline (true, u64) | inline (false, _)
#

Converts to u64 if within [0, U64_MAX], returning (true, value) or (false, _) on negative values or overflow.

zena
toU64(): u64
#

Converts to u64, throwing Error if negative or greater than U64_MAX.

zena
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.

Operators
zena
operator +(other: BigInt): BigInt
#

Sum this + other.

zena
operator -(other: BigInt): BigInt
#

Difference this - other.

zena
operator *(other: BigInt): BigInt
#

Product this * other.

zena
operator %(other: BigInt): BigInt
#

Remainder of truncating division this - this.div(other) * other, carrying the sign of this. Throws Error when other == 0.

zena
operator **(exp: i32): BigInt
#

Raises this to non-negative exponent exp (0 ** 0 == 1). Throws Error when exp < 0.

zena
operator ==(other: BigInt): boolean
#

Value equality.

zena
operator &(other: BigInt): BigInt
#

Infinite two's-complement bitwise AND.

zena
operator |(other: BigInt): BigInt
#

Infinite two's-complement bitwise OR.

zena
operator ^(other: BigInt): BigInt
#

Infinite two's-complement bitwise XOR.

zena
operator <<(bits: i32): BigInt
#

Shifts left by bits (this * 2^bits). Throws Error when bits < 0.

zena
operator >>(bits: i32): BigInt
#

Arithmetic right shift by bits (floor(this / 2^bits)). Throws Error when bits < 0.