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`).
Signature
class baml.IntA 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.IntegerOverflowif+,-,*,/, or unary-leaves theintrange.baml.panics.DivisionByZeroif the right operand of/or%is zero.baml.panics.NegativeBitShiftif 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
_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.
max_value
() -> int throws neverReturns the largest representable int, 2^62 - 1
(4_611_686_018_427_387_903).
min_value
() -> int throws neverReturns the smallest representable int, -2^62
(-4_611_686_018_427_387_904). int.min_value().abs() panics.
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.ParseErroriftextis empty, holds a non-digit character, or names a value outside theintrange.
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
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. 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.InvalidArgumentiflower >= 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
abs
(self) -> int throws neverReturns the absolute value of self.
Panics
baml.panics.IntegerOverflowforint.min_value(), whose absolute value2^62is one pastint.max_value().
Examples
(-7).abs() // 7
(3).abs() // 3
int.min_value().abs() // panics
count_ones
(self) -> int throws neverReturns 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
count_zeros
(self) -> int throws neverReturns the total number of zero bits.
Examples
(0).count_zeros() // 64
(-1).count_zeros() // 0
Returns the largest n such that base ** n <= self: the logarithm of
self in base, rounded down.
Throws
baml.errors.InvalidArgumentifself <= 0orbase < 2.
Examples
(1000).ilog(10) // 3
(1024).ilog(2) // 10
(1).ilog(10) // 0
(0).ilog(10) // throws
(10).ilog(1) // throws
Returns the largest r such that r * r <= self: the integer square
root of self.
Throws
baml.errors.InvalidArgumentifselfis negative.
Examples
(10).isqrt() // 3
(16).isqrt() // 4
(-1).isqrt() // throws
leading_ones
(self) -> int throws neverReturns the number of leading one bits.
Examples
(0).leading_ones() // 0
(-1).leading_ones() // 64
leading_zeros
(self) -> int throws neverReturns the number of leading zero bits.
Examples
(0).leading_zeros() // 64
(1).leading_zeros() // 63
(-1).leading_zeros() // 0 (all ones)
pow
(self, exp: int) -> int throws neverReturns 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()
trailing_ones
(self) -> int throws neverReturns the number of trailing one bits.
Examples
(7).trailing_ones() // 3 (binary 111)
(8).trailing_ones() // 0
(-1).trailing_ones() // 64
trailing_zeros
(self) -> int throws neverReturns 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 763–795
baml.ops.Add for int
Output = bigintInstance methods
add
(self, rhs: bigint) -> bigint throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 12981–13130
baml.ops.Add for int
Output = floatInstance methods
add
(self, rhs: float) -> float throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 10293–10438
baml.ops.Add for int
Output = intInstance methods
add
(self, rhs: int) -> int throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 7596–7753
baml.ops.BitAnd for int
Output = bigintInstance methods
bit_and
(self, rhs: bigint) -> bigint throws neverSource:<builtin>/baml/ns_ops/bitwise.bamlbytes 7551–7707
baml.ops.BitAnd for int
Output = intInstance methods
bit_and
(self, rhs: int) -> int throws neverSource:<builtin>/baml/ns_ops/bitwise.bamlbytes 6784–6928
baml.ops.BitOr for int
Output = bigintInstance methods
bit_or
(self, rhs: bigint) -> bigint throws neverSource:<builtin>/baml/ns_ops/bitwise.bamlbytes 7709–7863
baml.ops.BitOr for int
Output = intInstance methods
bit_or
(self, rhs: int) -> int throws neverSource:<builtin>/baml/ns_ops/bitwise.bamlbytes 6930–7072
baml.ops.BitXor for int
Output = bigintInstance methods
bit_xor
(self, rhs: bigint) -> bigint throws neverSource:<builtin>/baml/ns_ops/bitwise.bamlbytes 7865–8021
baml.ops.BitXor for int
Output = intInstance methods
bit_xor
(self, rhs: int) -> int throws neverSource:<builtin>/baml/ns_ops/bitwise.bamlbytes 7074–7218
baml.ops.Compare for int
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 9972–10242
baml.ops.Divide for int
Output = bigintInstance methods
div
(self, rhs: bigint) -> bigint throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 13464–13654
baml.ops.Divide for int
Output = floatInstance methods
div
(self, rhs: float) -> float throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 10744–10930
baml.ops.Divide for int
Output = intInstance methods
div
(self, rhs: int) -> int throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 8083–8243
baml.ops.Equals for int
Instance methods
eq
(self, other: int) -> 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 9855–9970
baml.ops.Multiply for int
Output = bigintInstance methods
mul
(self, rhs: bigint) -> bigint throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 13288–13462
baml.ops.Multiply for int
Output = floatInstance methods
mul
(self, rhs: float) -> float throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 10592–10742
baml.ops.Multiply for int
Output = intInstance methods
mul
(self, rhs: int) -> int throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 7919–8081
baml.ops.Negate for int
Output = intInstance methods
neg
(self) -> int throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 8410–8555
baml.ops.Remainder for int
Output = bigintInstance methods
rem
(self, rhs: bigint) -> bigint throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 13656–13849
baml.ops.Remainder for int
Output = floatInstance methods
rem
(self, rhs: float) -> float throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 10932–11083
baml.ops.Remainder for int
Output = intInstance methods
rem
(self, rhs: int) -> int throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 8245–8408
baml.ops.ShiftLeft for int
Output = bigintInstance methods
shl
(self, rhs: bigint) -> bigint throws neverSource:<builtin>/baml/ns_ops/bitwise.bamlbytes 8023–8198
baml.ops.ShiftLeft for int
Output = intInstance methods
shl
(self, rhs: int) -> int throws neverSource:<builtin>/baml/ns_ops/bitwise.bamlbytes 7220–7383
baml.ops.ShiftRight for int
Output = bigintInstance methods
shr
(self, rhs: bigint) -> bigint throws neverSource:<builtin>/baml/ns_ops/bitwise.bamlbytes 8200–8376
baml.ops.ShiftRight for int
Output = intInstance methods
shr
(self, rhs: int) -> int throws neverSource:<builtin>/baml/ns_ops/bitwise.bamlbytes 7385–7549
baml.ops.Subtract for int
Output = bigintInstance methods
sub
(self, rhs: bigint) -> bigint throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 13132–13286
baml.ops.Subtract for int
Output = floatInstance methods
sub
(self, rhs: float) -> float throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 10440–10590
baml.ops.Subtract for int
Output = intInstance methods
sub
(self, rhs: int) -> int throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 7755–7917
Related definitions
baml.ops.Comparebaml.ops.Comparebaml.ops.Comparebaml.ops.Comparebaml.ops.Comparebaml.ops.Comparebaml.ops.Comparebaml.ops.Equalsbaml.Bigintbaml.Boolbaml.Concretebaml.errors.InvalidArgumentbaml.errors.ParseErrorbaml.Floatbaml.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.DivisionByZerobaml.panics.IntegerOverflowbaml.panics.NegativeBitShiftbaml.random.Rngbaml.String