baml.Bigint

An arbitrary-precision signed integer, like Python's `int` or JavaScript's `BigInt`. Where `int` panics on overflow, `bigint` grows.

Reference version

Signature

class baml.Bigint

An arbitrary-precision signed integer, like Python's int or JavaScript's BigInt. Where int panics on overflow, bigint grows.

Literals carry a trailing n and may be decimal (42n), hex (0xFFn), octal (0o755n), or binary (0b1010n), with _ digit separators allowed (1_000_000n).

&, |, and ^ treat a bigint as an infinite sign-extended two's complement bit string, matching JavaScript BigInt and Python int: (-1n) & 1n is 1n, (-1n) | 0n is -1n, (-1n) ^ 0n is -1n.

Panics

Bigint operators and methods raise the panics below. They are not declared via throws because they signal that the program tried to do something invalid, rather than a recoverable runtime condition.

  • baml.panics.DivisionByZero — a / 0n or a % 0n.
  • baml.panics.NegativeBitShift — a << -1n or a >> -1n. The shift count must be non-negative.
  • baml.panics.AllocFailure — an operand or result would need more than about 268M bits, bigint's workspace cap. Raised by a * b, a << n, and a.pow(n) when the predicted result exceeds the cap, and by bigint.parse(s) when s carries more decimal digits than the cap permits.

Source:<builtin>/baml/bigint.bamlbytes 1270–9273

Static methods

function

_random_byte_count

(lower: bigint, upper: bigint) -> int throws never

(internal) Number of bytes needed for one draw over [lower, upper).

function

_random_in_range

(draw: uint8array, lower: bigint, upper: bigint) -> bigint throws never

(internal) Maps one draw onto [lower, upper), returning upper when the draw must be rejected.

function

parse

(text: string) -> bigint throws baml.errors.ParseError

Parses text as a base ten signed integer.

Accepts an optional leading + or - followed by one or more ASCII digits, and nothing else: no surrounding whitespace, no underscore separators, no 0x / 0o / 0b prefix, no Unicode digits, no scientific notation. Preprocess the string if you need any of those.

Throws

  • baml.errors.ParseError if text is empty or holds a non-digit character.

Examples

bigint.parse("42")       // 42n
bigint.parse("-7")       // -7n
bigint.parse("+0")       // 0n
bigint.parse("")         // throws — empty
bigint.parse("12a")      // throws — non-digit
bigint.parse("0x2a")     // throws — hex prefix not accepted
bigint.parse("1_000")    // throws — underscores not accepted
bigint.parse(" 5 ")      // throws — whitespace; trim first
bigint.parse("99999999999999999999")  // 99999999999999999999n
function

random

(
lower: bigint,
upper: bigint,
rng: baml.random.Rng = …
) -> bigint throws baml.errors.InvalidArgument

Returns a uniformly distributed random integer in the half-open range [lower, upper), drawn from rng.

rng defaults to random.SystemRandom, the host's cryptographic entropy source; pass a seeded generator to make the draw reproducible. Rejection sampling avoids modulo bias, so one result may consume several draws from rng.

Throws

  • baml.errors.InvalidArgument if lower >= upper, which would leave the range empty.

Examples

bigint.random(0n, 10n)         // some value in {0, 1, ..., 9}
bigint.random(-5n, 5n)         // some value in {-5, -4, ..., 4}
bigint.random(0n, 1n)          // always 0  (single-element range)
bigint.random(5n, 5n)          // throws — empty range
bigint.random(10n, 0n)         // throws — lower > upper

// Reproducible: the same seed replays the same values.
let rng = baml.random.Xoshiro256PlusPlus.new(seed = my_seed);
bigint.random(0n, 10n, rng = rng)

Instance methods

function

abs

(self) -> bigint throws never

Returns the absolute value of self.

Examples

(-7n).abs()         // 7n
(3n).abs()          // 3n
function

ilog

(self, base: bigint) -> bigint throws baml.errors.InvalidArgument

Returns the largest n such that base ** n <= self: the logarithm of self in base, rounded down.

Throws

  • baml.errors.InvalidArgument if self <= 0 or base < 2.

Examples

(1000n).ilog(10n)   // 3n
(1024n).ilog(2n)    // 10n
(1n).ilog(10n)      // 0n
(0n).ilog(10n)      // throws
(10n).ilog(1n)      // throws
function

isqrt

(self) -> bigint throws baml.errors.InvalidArgument

Returns the largest r such that r * r <= self: the integer square root of self.

Throws

  • baml.errors.InvalidArgument if self is negative.

Examples

(10n).isqrt()   // 3n
(16n).isqrt()   // 4n
(-1n).isqrt()   // throws
function

pow

(self, exp: bigint) -> bigint throws never

Returns self ** exp.

0 ** 0 is 1n, by convention. A negative exp gives 0n, because the exact value 1 / self ** -exp lies in (-1, 1) for |self| > 1 and rounds toward zero. That holds uniformly: for self == 1n or self == -1n the exact value is ±1n, and the result is still 0n.

Panics

  • baml.panics.AllocFailure if the estimated result exceeds bigint's workspace cap.

Examples

(2n).pow(10n)      // 1024n
(2n).pow(0n)       // 1n
(0n).pow(0n)       // 1n   (convention)
(2n).pow(-1n)      // 0n
(-2n).pow(3n)      // -8n
(10n).pow(100n)    // a very large number
function

to_int

(self) -> int throws baml.errors.InvalidArgument

Narrows self to a fixed-width int.

Throws

  • baml.errors.InvalidArgument if self falls outside int's range. BAML integers are 63-bit signed, so int.min_value() through int.max_value() is narrower than a machine 64-bit integer.

Examples

(42n).to_int()                          // 42
(-7n).to_int()                          // -7
(4611686018427387903n).to_int()         // int.max_value()
(4611686018427387904n).to_int()         // throws — one past int.max_value()
(10n).pow(30n).to_int()                 // throws

Implementations

baml.Concrete for T

Source:<builtin>/baml/core.bamlbytes 763–795

baml.ops.Add for bigint

Output = bigint

Instance methods

function

add

(self, rhs: bigint) -> bigint throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 11105–11257

baml.ops.Add for bigint

Output = bigint

Instance methods

function

add

(self, rhs: int) -> bigint throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 12126–12272

baml.ops.BitAnd for bigint

Output = bigint

Instance methods

function

bit_and

(self, rhs: bigint) -> bigint throws never

Source:<builtin>/baml/ns_ops/bitwise.bamlbytes 9210–9369

baml.ops.BitAnd for bigint

Output = bigint

Instance methods

function

bit_and

(self, rhs: int) -> bigint throws never

Source:<builtin>/baml/ns_ops/bitwise.bamlbytes 8398–8551

baml.ops.BitOr for bigint

Output = bigint

Instance methods

function

bit_or

(self, rhs: bigint) -> bigint throws never

Source:<builtin>/baml/ns_ops/bitwise.bamlbytes 9371–9528

baml.ops.BitOr for bigint

Output = bigint

Instance methods

function

bit_or

(self, rhs: int) -> bigint throws never

Source:<builtin>/baml/ns_ops/bitwise.bamlbytes 8553–8704

baml.ops.BitXor for bigint

Output = bigint

Instance methods

function

bit_xor

(self, rhs: bigint) -> bigint throws never

Source:<builtin>/baml/ns_ops/bitwise.bamlbytes 9530–9689

baml.ops.BitXor for bigint

Output = bigint

Instance methods

function

bit_xor

(self, rhs: int) -> bigint throws never

Source:<builtin>/baml/ns_ops/bitwise.bamlbytes 8706–8859

baml.ops.Compare for bigint

Instance methods

function

clamp

(self, min: Self, max: Self) -> Self throws never

Clamps self into the range [min, max].

Callers should pass min <= max; if min > max the result is always min, because the lower clamp runs after the upper one. Written as the min/max chain rather than a three-way match so it agrees with int.clamp / float.clamp on that degenerate input too.

function

ge

(self, other: Self) -> bool throws never

Whether self orders after other or equals it. Defaults to self.cmp(other) != Ordering.Less.

function

gt

(self, other: Self) -> bool throws never

Whether self orders strictly after other. Defaults to self.cmp(other) == Ordering.Greater.

function

le

(self, other: Self) -> bool throws never

Whether self orders before other or equals it. Defaults to self.cmp(other) != Ordering.Greater.

function

lt

(self, other: Self) -> bool throws never

Whether self orders strictly before other. Defaults to self.cmp(other) == Ordering.Less.

function

max

(self, other: Self) -> Self throws never

Ties return self, so max is stable when two values compare Equal without being interchangeable.

function

min

(self, other: Self) -> Self throws never

Ties return self, matching max.

Source:<builtin>/baml/ns_ops/comparison.bamlbytes 10364–10637

baml.ops.Divide for bigint

Output = bigint

Instance methods

function

div

(self, rhs: bigint) -> bigint throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 11597–11790

baml.ops.Divide for bigint

Output = bigint

Instance methods

function

div

(self, rhs: int) -> bigint throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 12600–12787

baml.ops.Equals for bigint

Instance methods

function

eq

(self, other: bigint) -> bool throws never
function

neq

(self, other: Self) -> bool throws never

Whether self and other differ. Defaults to !self.eq(other).

Override this only to compute the answer more directly; an override that disagrees with !eq leaves the pair inconsistent. Equals for null overrides it with a constant false, which is consistent because null has exactly one value.

Source:<builtin>/baml/ns_ops/comparison.bamlbytes 10244–10362

baml.ops.Multiply for bigint

Output = bigint

Instance methods

function

mul

(self, rhs: bigint) -> bigint throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 11418–11595

baml.ops.Multiply for bigint

Output = bigint

Instance methods

function

mul

(self, rhs: int) -> bigint throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 12427–12598

baml.ops.Negate for bigint

Output = bigint

Instance methods

function

neg

(self) -> bigint throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 11990–12124

baml.ops.Remainder for bigint

Output = bigint

Instance methods

function

rem

(self, rhs: bigint) -> bigint throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 11792–11988

baml.ops.Remainder for bigint

Output = bigint

Instance methods

function

rem

(self, rhs: int) -> bigint throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 12789–12979

baml.ops.ShiftLeft for bigint

Output = bigint

Instance methods

function

shl

(self, rhs: bigint) -> bigint throws never

Source:<builtin>/baml/ns_ops/bitwise.bamlbytes 9691–9869

baml.ops.ShiftLeft for bigint

Output = bigint

Instance methods

function

shl

(self, rhs: int) -> bigint throws never

Source:<builtin>/baml/ns_ops/bitwise.bamlbytes 8861–9033

baml.ops.ShiftRight for bigint

Output = bigint

Instance methods

function

shr

(self, rhs: bigint) -> bigint throws never

Source:<builtin>/baml/ns_ops/bitwise.bamlbytes 9871–10050

baml.ops.ShiftRight for bigint

Output = bigint

Instance methods

function

shr

(self, rhs: int) -> bigint throws never

Source:<builtin>/baml/ns_ops/bitwise.bamlbytes 9035–9208

baml.ops.Subtract for bigint

Output = bigint

Instance methods

function

sub

(self, rhs: bigint) -> bigint throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 11259–11416

baml.ops.Subtract for bigint

Output = bigint

Instance methods

function

sub

(self, rhs: int) -> bigint throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 12274–12425