Skip to content

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.

type Int

64-bit signed integer.

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
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
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
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
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
fn wrapping_sub(a: Int, b: Int): Int

type function on Int

Wrapping (modular, two’s-complement) subtraction. See Int.wrapping_add.

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
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
fn bitwise_or(a: Int, b: Int): Int

type function on Int

See Int.bitwise_and.

fn bitwise_xor(a: Int, b: Int): Int

type function on Int

See Int.bitwise_and.

fn bitwise_not(a: Int): Int

type function on Int

Bitwise NOT (one’s complement) of one Int. See Int.bitwise_and.

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
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

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"

impl Add

fn add(lhs: Int, rhs: Int): Int

impl Add.add

impl Subtract

fn subtract(lhs: Int, rhs: Int): Int

impl Subtract.subtract

impl Multiply

fn multiply(lhs: Int, rhs: Int): Int

impl Multiply.multiply

impl Divide

fn divide(lhs: Int, rhs: Int): Int

impl Divide.divide

impl Debug

fn inspect(n: Int): String

impl Debug.inspect

impl Equatable

fn equal?(a: Int, b: Int): Bool

impl Equatable.equal?

impl Hashable

fn hash(n: Int): Int

impl Hashable.hash

impl Comparable

fn compare(a: Int, b: Int): Ordering

impl Comparable.compare

impl Discrete

fn next(n: Int): Maybe<Int>

impl Discrete.next

impl Discrete

fn steps_between(start: Int, end: Int): Maybe<Int>

impl Discrete.steps_between

impl Steppable

fn step_by(n: Int, by: Int): Maybe<Int>

impl Steppable.step_by

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.

impl Debug

fn inspect(value: NonZeroInt): String

impl Debug.inspect

impl Display

fn to_string(value: NonZeroInt): String

impl Display.to_string

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.

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

impl Debug

fn inspect(value: PositiveInt): String

impl Debug.inspect

impl Display

fn to_string(value: PositiveInt): String

impl Display.to_string