std/int
Operations on the 64-bit signed Int type: total division/modulo gated on
the refinement types NonZeroInt/PositiveInt, conversion to Float,
wrapping (modular) arithmetic, bit operations, and the Int limits. The
arithmetic, Display/Debug/Equatable/Hashable/Comparable impls give Int its
string form, hashing, and total order.
Import with import std/int.
Exports
Section titled “Exports”type Int
Section titled “type Int”type Int
64-bit signed integer.
Int.to_non_zero
Section titled “Int.to_non_zero”fn to_non_zero(n: Int): Maybe<NonZeroInt>
type function on Int
Validates that n is non-zero and wraps it. Returns None when n == 0.
Interactive Tests
assert Int.to_non_zero(5) == Some(NonZeroInt(5))
assert Int.to_non_zero(-3) == Some(NonZeroInt(-3))
assert Int.to_non_zero(0) == None
Int.to_positive
Section titled “Int.to_positive”fn to_positive(n: Int): Maybe<PositiveInt>
type function on Int
Validates that n is positive (> 0) and wraps it. Returns None for
zero or negative values.
Interactive Tests
assert Int.to_positive(5) == Some(PositiveInt(5))
assert Int.to_positive(0) == None
assert Int.to_positive(-3) == None
Int.to_float
Section titled “Int.to_float”fn to_float(x: Int): Float
type function on Int
Converts an Int to a Float. Total — every Int64 maps to a Float64. Precision loss is possible for |x| > 2^53 (Float64 has a 52-bit mantissa); the result is the nearest representable Float.
Interactive Tests
assert Int.to_float(5) == 5.0
assert Int.to_float(-7) == -7.0
Int.modulo
Section titled “Int.modulo”fn modulo(x: Int, y: NonZeroInt): Int
type function on Int
Integer modulo. Total — same shape as Int.divide, requires
y: NonZeroInt.
Interactive Tests
nz = try Int.to_non_zero(3)
assert Int.modulo(10, nz) == 1
Int.wrapping_add
Section titled “Int.wrapping_add”fn wrapping_add(a: Int, b: Int): Int
type function on Int
Wrapping (modular, two’s-complement) addition. Unlike +, which traps on
overflow, this silently wraps — for hashing, checksums, and any algorithm
that deliberately wants modular arithmetic.
Interactive Tests
assert Int.wrapping_add(9223372036854775807, 1) == Int.min_value
Int.wrapping_sub
Section titled “Int.wrapping_sub”fn wrapping_sub(a: Int, b: Int): Int
type function on Int
Wrapping (modular, two’s-complement) subtraction. See Int.wrapping_add.
Int.wrapping_mul
Section titled “Int.wrapping_mul”fn wrapping_mul(a: Int, b: Int): Int
type function on Int
Wrapping (modular, two’s-complement) multiplication. See Int.wrapping_add.
Interactive Tests
assert Int.wrapping_mul(9223372036854775807, 2) == -2
Int.bitwise_and
Section titled “Int.bitwise_and”fn bitwise_and(a: Int, b: Int): Int
type function on Int
Bitwise AND of two Ints. Nomi has no bitwise operators; these functions are
the surface, as in Gleam (int.bitwise_*) and Elm (Bitwise.*).
Interactive Tests
assert Int.bitwise_and(12, 10) == 8
assert Int.bitwise_or(12, 10) == 14
assert Int.bitwise_xor(12, 10) == 6
assert Int.bitwise_not(0) == -1
Int.bitwise_or
Section titled “Int.bitwise_or”fn bitwise_or(a: Int, b: Int): Int
type function on Int
See Int.bitwise_and.
Int.bitwise_xor
Section titled “Int.bitwise_xor”fn bitwise_xor(a: Int, b: Int): Int
type function on Int
See Int.bitwise_and.
Int.bitwise_not
Section titled “Int.bitwise_not”fn bitwise_not(a: Int): Int
type function on Int
Bitwise NOT (one’s complement) of one Int. See Int.bitwise_and.
Int.shift_left
Section titled “Int.shift_left”fn shift_left(a: Int, n: Int): Int
type function on Int
Left shift a by n bit positions. n must be non-negative (a negative
count is a runtime error); a count >= 64 shifts every bit out, yielding 0.
Interactive Tests
assert Int.shift_left(1, 4) == 16
Int.shift_right
Section titled “Int.shift_right”fn shift_right(a: Int, n: Int): Int
type function on Int
Arithmetic (sign-extending) right shift of a by n bit positions. n
must be non-negative; a count >= 64 yields 0 for non-negative a and -1
for negative a.
Interactive Tests
assert Int.shift_right(-8, 1) == -4
Int.to_string
Section titled “Int.to_string”impl Display
fn to_string(n: Int): String
impl Display.to_string
Returns the canonical decimal string form of n, matching the runtime
stringifier used by IO.print and string interpolation.
Interactive Tests
assert Int.to_string(42) == "42"
assert Int.to_string(-7) == "-7"
Int.add
Section titled “Int.add”impl Add
fn add(lhs: Int, rhs: Int): Int
impl Add.add
Int.subtract
Section titled “Int.subtract”impl Subtract
fn subtract(lhs: Int, rhs: Int): Int
impl Subtract.subtract
Int.multiply
Section titled “Int.multiply”impl Multiply
fn multiply(lhs: Int, rhs: Int): Int
impl Multiply.multiply
Int.divide
Section titled “Int.divide”impl Divide
fn divide(lhs: Int, rhs: Int): Int
impl Divide.divide
Int.inspect
Section titled “Int.inspect”impl Debug
fn inspect(n: Int): String
impl Debug.inspect
Int.equal?
Section titled “Int.equal?”impl Equatable
fn equal?(a: Int, b: Int): Bool
impl Equatable.equal?
Int.hash
Section titled “Int.hash”impl Hashable
fn hash(n: Int): Int
impl Hashable.hash
Int.compare
Section titled “Int.compare”impl Comparable
fn compare(a: Int, b: Int): Ordering
impl Comparable.compare
Int.next
Section titled “Int.next”impl Discrete
fn next(n: Int): Maybe<Int>
impl Discrete.next
Int.steps_between
Section titled “Int.steps_between”impl Discrete
fn steps_between(start: Int, end: Int): Maybe<Int>
impl Discrete.steps_between
Int.step_by
Section titled “Int.step_by”impl Steppable
fn step_by(n: Int, by: Int): Maybe<Int>
impl Steppable.step_by
type NonZeroInt
Section titled “type NonZeroInt”type NonZeroInt Int
opaque — construction surface is private to its defining module
An Int proven to be non-zero. The smart constructor Int.to_non_zero is
the only way to obtain a value; once you have one, dividing or modding
by it cannot fail.
NonZeroInt.inspect
Section titled “NonZeroInt.inspect”impl Debug
fn inspect(value: NonZeroInt): String
impl Debug.inspect
NonZeroInt.to_string
Section titled “NonZeroInt.to_string”impl Display
fn to_string(value: NonZeroInt): String
impl Display.to_string
type PositiveInt
Section titled “type PositiveInt”type PositiveInt Int
opaque — construction surface is private to its defining module
An Int proven to be positive (> 0). Smart constructor: Int.to_positive.
Implies non-zero, so a PositiveInt can be passed wherever a
NonZeroInt is expected via PositiveInt.to_non_zero.
PositiveInt.to_non_zero
Section titled “PositiveInt.to_non_zero”fn to_non_zero(p: PositiveInt): NonZeroInt
type function on PositiveInt
Widens a PositiveInt to a NonZeroInt — every positive Int is non-zero,
so this is total (never fails). Lets a PositiveInt flow into a position
that requires NonZeroInt, such as Int.divide’s divisor.
Interactive Tests
p = try Int.to_positive(5)
assert Int.divide(20, PositiveInt.to_non_zero(p)) == 4
PositiveInt.inspect
Section titled “PositiveInt.inspect”impl Debug
fn inspect(value: PositiveInt): String
impl Debug.inspect
PositiveInt.to_string
Section titled “PositiveInt.to_string”impl Display
fn to_string(value: PositiveInt): String
impl Display.to_string