baml.Int

A 63-bit signed integer, from `-2^62` (`-4_611_686_018_427_387_904`) to `2^62 - 1` (`4_611_686_018_427_387_903`).

Reference version

Signature

class baml.Int

A 63-bit signed integer, from -2^62 (-4_611_686_018_427_387_904) to 2^62 - 1 (4_611_686_018_427_387_903).

The runtime reserves one bit of a 64-bit machine word for its pointer/value tag, so the usable width is 63 bits rather than 64. Unlike Python's int, an int does not grow to fit; use bigint for arbitrary precision.

Literals may be decimal (42), hex (0xFF), octal (0o755), or binary (0b1010), with _ digit separators allowed (1_000_000).

Bit operations such as leading_zeros and count_ones read self as a 64-bit two's-complement word. The value is stored sign-extended, so those counts include the high sign/tag bits: (1).leading_zeros() is 63, not 62.

Panics

Arithmetic panics rather than wrapping. These panics are catchable, but are not declared in throws clauses.

  • baml.panics.IntegerOverflow if +, -, *, /, or unary - leaves the int range.
  • baml.panics.DivisionByZero if the right operand of / or % is zero.
  • baml.panics.NegativeBitShift if the count given to << or >> is negative.

<< is the exception to the overflow rule: it truncates, discarding whatever is shifted past the 63-bit width. 1 << 62 is int.min_value() (bit 62 is the sign bit) and 1 << 63 is 0. >> cannot overflow either; a count of 63 or more saturates to the sign, giving 0 for a non-negative self and -1 for a negative one.

Source:<builtin>/baml/int.bamlbytes 1521–10960

Static methods

function

_random_in_range

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

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

function

max_value

() -> int throws never

Returns the largest representable int, 2^62 - 1 (4_611_686_018_427_387_903).

function

min_value

() -> int throws never

Returns the smallest representable int, -2^62 (-4_611_686_018_427_387_904). int.min_value().abs() panics.

function

parse

(text: string) -> int 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 underscores, no other numeric formats. The value must fit the 63-bit int range.

Throws

  • baml.errors.ParseError if text is empty, holds a non-digit character, or names a value outside the int range.

Examples

int.parse("42")       // 42
int.parse("-7")       // -7
int.parse("+0")       // 0
int.parse("")         // throws — empty
int.parse("12a")      // throws — non-digit
int.parse(" 5 ")      // throws — whitespace; trim first
int.parse("99999999999999999999")  // throws — out of range
function

random

(
lower: int,
upper: int,
rng: baml.random.Rng = …

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. Even the widest representable half-open range works. Rejection sampling avoids modulo bias, so one result may consume several values from rng.

Throws

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

Examples

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

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

Instance methods

function

abs

(self) -> int throws never
function

count_ones

(self) -> int throws never

Returns the total number of one bits, also known as the population count or "popcount".

Examples

(0).count_ones()   // 0
(7).count_ones()   // 3
(-1).count_ones()  // 64
function

count_zeros

(self) -> int throws never

Returns the total number of zero bits.

Examples

(0).count_zeros()   // 64
(-1).count_zeros()  // 0
function

ilog

(self, base: int) -> int 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

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

isqrt

(self) -> int 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

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

leading_ones

(self) -> int throws never

Returns the number of leading one bits.

Examples

(0).leading_ones()   // 0
(-1).leading_ones()  // 64
function

leading_zeros

(self) -> int throws never

Returns the number of leading zero bits.

Examples

(0).leading_zeros()  // 64
(1).leading_zeros()  // 63
(-1).leading_zeros() // 0   (all ones)
function

pow

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

Returns self ** exp, saturating instead of panicking on overflow: int.max_value() for positive overflow, int.min_value() for negative.

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

Examples

(2).pow(10)      // 1024
(2).pow(0)       // 1
(0).pow(0)       // 1   (convention)
(2).pow(-1)      // 0
(-2).pow(3)      // -8
(10).pow(100)    // saturates to int.max_value()
function

trailing_ones

(self) -> int throws never

Returns the number of trailing one bits.

Examples

(7).trailing_ones()   // 3   (binary 111)
(8).trailing_ones()   // 0
(-1).trailing_ones()  // 64
function

trailing_zeros

(self) -> int throws never

Returns the number of trailing zero bits.

Examples

(0).trailing_zeros()  // 64
(8).trailing_zeros()  // 3   (binary 1000)
(1).trailing_zeros()  // 0

Implementations

baml.Concrete for T

Source:<builtin>/baml/core.bamlbytes 747–779

baml.ops.Add for int

Output = bigint

Instance methods

function

add

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

Source:<builtin>/baml/ns_ops/math.bamlbytes 12981–13130

baml.ops.Add for int

Output = float

Instance methods

function

add

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

Source:<builtin>/baml/ns_ops/math.bamlbytes 10293–10438

baml.ops.Add for int

Output = int

Instance methods

function

add

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

Source:<builtin>/baml/ns_ops/math.bamlbytes 7596–7753

baml.ops.BitAnd for int

Output = bigint

Instance methods

function

bit_and

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

Source:<builtin>/baml/ns_ops/bitwise.bamlbytes 7551–7707

baml.ops.BitAnd for int

Output = int

Instance methods

function

bit_and

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

Source:<builtin>/baml/ns_ops/bitwise.bamlbytes 6784–6928

baml.ops.BitOr for int

Output = bigint

Instance methods

function

bit_or

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

Source:<builtin>/baml/ns_ops/bitwise.bamlbytes 7709–7863

baml.ops.BitOr for int

Output = int

Instance methods

function

bit_or

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

Source:<builtin>/baml/ns_ops/bitwise.bamlbytes 6930–7072

baml.ops.BitXor for int

Output = bigint

Instance methods

function

bit_xor

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

Source:<builtin>/baml/ns_ops/bitwise.bamlbytes 7865–8021

baml.ops.BitXor for int

Output = int

Instance methods

function

bit_xor

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

Source:<builtin>/baml/ns_ops/bitwise.bamlbytes 7074–7218

baml.ops.Compare for int

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 9948–10218

baml.ops.Divide for int

Output = bigint

Instance methods

function

div

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

Source:<builtin>/baml/ns_ops/math.bamlbytes 13464–13654

baml.ops.Divide for int

Output = float

Instance methods

function

div

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

Source:<builtin>/baml/ns_ops/math.bamlbytes 10744–10930

baml.ops.Divide for int

Output = int

Instance methods

function

div

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

Source:<builtin>/baml/ns_ops/math.bamlbytes 8083–8243

baml.ops.Equals for int

Instance methods

function

eq

(self, other: int) -> 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 9831–9946

baml.ops.Multiply for int

Output = bigint

Instance methods

function

mul

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

Source:<builtin>/baml/ns_ops/math.bamlbytes 13288–13462

baml.ops.Multiply for int

Output = float

Instance methods

function

mul

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

Source:<builtin>/baml/ns_ops/math.bamlbytes 10592–10742

baml.ops.Multiply for int

Output = int

Instance methods

function

mul

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

Source:<builtin>/baml/ns_ops/math.bamlbytes 7919–8081

baml.ops.Negate for int

Output = int

Instance methods

function

neg

(self) -> int throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 8410–8555

baml.ops.Remainder for int

Output = bigint

Instance methods

function

rem

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

Source:<builtin>/baml/ns_ops/math.bamlbytes 13656–13849

baml.ops.Remainder for int

Output = float

Instance methods

function

rem

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

Source:<builtin>/baml/ns_ops/math.bamlbytes 10932–11083

baml.ops.Remainder for int

Output = int

Instance methods

function

rem

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

Source:<builtin>/baml/ns_ops/math.bamlbytes 8245–8408

baml.ops.ShiftLeft for int

Output = bigint

Instance methods

function

shl

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

Source:<builtin>/baml/ns_ops/bitwise.bamlbytes 8023–8198

baml.ops.ShiftLeft for int

Output = int

Instance methods

function

shl

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

Source:<builtin>/baml/ns_ops/bitwise.bamlbytes 7220–7383

baml.ops.ShiftRight for int

Output = bigint

Instance methods

function

shr

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

Source:<builtin>/baml/ns_ops/bitwise.bamlbytes 8200–8376

baml.ops.ShiftRight for int

Output = int

Instance methods

function

shr

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

Source:<builtin>/baml/ns_ops/bitwise.bamlbytes 7385–7549

baml.ops.Subtract for int

Output = bigint

Instance methods

function

sub

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

Source:<builtin>/baml/ns_ops/math.bamlbytes 13132–13286

baml.ops.Subtract for int

Output = float

Instance methods

function

sub

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

Source:<builtin>/baml/ns_ops/math.bamlbytes 10440–10590

baml.ops.Subtract for int

Output = int

Instance methods

function

sub

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

Source:<builtin>/baml/ns_ops/math.bamlbytes 7755–7917