baml.Bigint
An arbitrary-precision signed integer, like Python's `int` or JavaScript's `BigInt`. Where `int` panics on overflow, `bigint` grows.
Signature
class baml.BigintAn 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 / 0nora % 0n.baml.panics.NegativeBitShift—a << -1nora >> -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 bya * b,a << n, anda.pow(n)when the predicted result exceeds the cap, and bybigint.parse(s)whenscarries more decimal digits than the cap permits.
Source:<builtin>/baml/bigint.bamlbytes 1270–9273
Static methods
_random_byte_count
(lower: bigint, upper: bigint) -> int throws never(internal) Number of bytes needed for one draw over [lower, upper).
_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.
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.ParseErroriftextis 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
random
(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.InvalidArgumentiflower >= 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
abs
(self) -> bigint throws neverReturns the absolute value of self.
Examples
(-7n).abs() // 7n
(3n).abs() // 3n
Returns the largest n such that base ** n <= self: the logarithm of
self in base, rounded down.
Throws
baml.errors.InvalidArgumentifself <= 0orbase < 2.
Examples
(1000n).ilog(10n) // 3n
(1024n).ilog(2n) // 10n
(1n).ilog(10n) // 0n
(0n).ilog(10n) // throws
(10n).ilog(1n) // throws
Returns the largest r such that r * r <= self: the integer square
root of self.
Throws
baml.errors.InvalidArgumentifselfis negative.
Examples
(10n).isqrt() // 3n
(16n).isqrt() // 4n
(-1n).isqrt() // throws
pow
(self, exp: bigint) -> bigint throws neverReturns 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.AllocFailureif the estimated result exceedsbigint'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
Narrows self to a fixed-width int.
Throws
baml.errors.InvalidArgumentifselffalls outsideint's range. BAML integers are 63-bit signed, soint.min_value()throughint.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 = bigintInstance methods
add
(self, rhs: bigint) -> bigint throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 11105–11257
baml.ops.Add for bigint
Output = bigintInstance methods
add
(self, rhs: int) -> bigint throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 12126–12272
baml.ops.BitAnd for bigint
Output = bigintInstance methods
bit_and
(self, rhs: bigint) -> bigint throws neverSource:<builtin>/baml/ns_ops/bitwise.bamlbytes 9210–9369
baml.ops.BitAnd for bigint
Output = bigintInstance methods
bit_and
(self, rhs: int) -> bigint throws neverSource:<builtin>/baml/ns_ops/bitwise.bamlbytes 8398–8551
baml.ops.BitOr for bigint
Output = bigintInstance methods
bit_or
(self, rhs: bigint) -> bigint throws neverSource:<builtin>/baml/ns_ops/bitwise.bamlbytes 9371–9528
baml.ops.BitOr for bigint
Output = bigintInstance methods
bit_or
(self, rhs: int) -> bigint throws neverSource:<builtin>/baml/ns_ops/bitwise.bamlbytes 8553–8704
baml.ops.BitXor for bigint
Output = bigintInstance methods
bit_xor
(self, rhs: bigint) -> bigint throws neverSource:<builtin>/baml/ns_ops/bitwise.bamlbytes 9530–9689
baml.ops.BitXor for bigint
Output = bigintInstance methods
bit_xor
(self, rhs: int) -> bigint throws neverSource:<builtin>/baml/ns_ops/bitwise.bamlbytes 8706–8859
baml.ops.Compare for bigint
Instance methods
clamp
(self, min: Self, max: Self) -> Self throws neverClamps 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.
ge
(self, other: Self) -> bool throws neverWhether self orders after other or equals it. Defaults to
self.cmp(other) != Ordering.Less.
gt
(self, other: Self) -> bool throws neverWhether self orders strictly after other. Defaults to
self.cmp(other) == Ordering.Greater.
le
(self, other: Self) -> bool throws neverWhether self orders before other or equals it. Defaults to
self.cmp(other) != Ordering.Greater.
lt
(self, other: Self) -> bool throws neverWhether self orders strictly before other. Defaults to
self.cmp(other) == Ordering.Less.
max
(self, other: Self) -> Self throws neverTies return self, so max is stable when two values compare Equal
without being interchangeable.
min
(self, other: Self) -> Self throws neverTies return self, matching max.
Source:<builtin>/baml/ns_ops/comparison.bamlbytes 10364–10637
baml.ops.Divide for bigint
Output = bigintInstance methods
div
(self, rhs: bigint) -> bigint throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 11597–11790
baml.ops.Divide for bigint
Output = bigintInstance methods
div
(self, rhs: int) -> bigint throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 12600–12787
baml.ops.Equals for bigint
Instance methods
eq
(self, other: bigint) -> bool throws neverneq
(self, other: Self) -> bool throws neverWhether 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 = bigintInstance methods
mul
(self, rhs: bigint) -> bigint throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 11418–11595
baml.ops.Multiply for bigint
Output = bigintInstance methods
mul
(self, rhs: int) -> bigint throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 12427–12598
baml.ops.Negate for bigint
Output = bigintInstance methods
neg
(self) -> bigint throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 11990–12124
baml.ops.Remainder for bigint
Output = bigintInstance methods
rem
(self, rhs: bigint) -> bigint throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 11792–11988
baml.ops.Remainder for bigint
Output = bigintInstance methods
rem
(self, rhs: int) -> bigint throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 12789–12979
baml.ops.ShiftLeft for bigint
Output = bigintInstance methods
shl
(self, rhs: bigint) -> bigint throws neverSource:<builtin>/baml/ns_ops/bitwise.bamlbytes 9691–9869
baml.ops.ShiftLeft for bigint
Output = bigintInstance methods
shl
(self, rhs: int) -> bigint throws neverSource:<builtin>/baml/ns_ops/bitwise.bamlbytes 8861–9033
baml.ops.ShiftRight for bigint
Output = bigintInstance methods
shr
(self, rhs: bigint) -> bigint throws neverSource:<builtin>/baml/ns_ops/bitwise.bamlbytes 9871–10050
baml.ops.ShiftRight for bigint
Output = bigintInstance methods
shr
(self, rhs: int) -> bigint throws neverSource:<builtin>/baml/ns_ops/bitwise.bamlbytes 9035–9208
baml.ops.Subtract for bigint
Output = bigintInstance methods
sub
(self, rhs: bigint) -> bigint throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 11259–11416
baml.ops.Subtract for bigint
Output = bigintInstance methods
sub
(self, rhs: int) -> bigint throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 12274–12425
Related definitions
baml.ops.Comparebaml.ops.Comparebaml.ops.Comparebaml.ops.Comparebaml.ops.Comparebaml.ops.Comparebaml.ops.Comparebaml.ops.Equalsbaml.Boolbaml.Concretebaml.errors.InvalidArgumentbaml.errors.ParseErrorbaml.Intbaml.ops.Addbaml.ops.BitAndbaml.ops.BitOrbaml.ops.BitXorbaml.ops.Comparebaml.ops.Dividebaml.ops.Equalsbaml.ops.Multiplybaml.ops.Negatebaml.ops.Orderingbaml.ops.Remainderbaml.ops.ShiftLeftbaml.ops.ShiftRightbaml.ops.Subtractbaml.panics.AllocFailurebaml.panics.DivisionByZerobaml.panics.NegativeBitShiftbaml.random.Rngbaml.Stringbaml.Uint8Array