Core

std.math

Pure fixed-width integer helpers and small number-theory routines.

When To Use std.math

In Zerolang, use std.math for pure fixed-width integer helpers, checked/saturating arithmetic, and small number-theory routines.

Runnable today:

APIReturnNotes
std.math.minU32(left, right)u32Returns the smaller unsigned value.
std.math.minI32(left, right)i32Returns the smaller signed 32-bit value.
std.math.minUsize(left, right)usizeReturns the smaller pointer-width unsigned value.
std.math.minI64(left, right)i64Returns the smaller signed 64-bit value.
std.math.minU64(left, right)u64Returns the smaller unsigned 64-bit value.
std.math.maxU32(left, right)u32Returns the larger unsigned value.
std.math.maxI32(left, right)i32Returns the larger signed 32-bit value.
std.math.maxUsize(left, right)usizeReturns the larger pointer-width unsigned value.
std.math.maxI64(left, right)i64Returns the larger signed 64-bit value.
std.math.maxU64(left, right)u64Returns the larger unsigned 64-bit value.
std.math.clampU32(value, low, high)u32Clamps between the two bounds; swapped bounds are normalized.
std.math.clampI32(value, low, high)i32Clamps a signed 32-bit value; swapped bounds are normalized.
std.math.clampUsize(value, low, high)usizeClamps a pointer-width unsigned value; swapped bounds are normalized.
std.math.clampI64(value, low, high)i64Clamps a signed 64-bit value; swapped bounds are normalized.
std.math.clampU64(value, low, high)u64Clamps an unsigned 64-bit value; swapped bounds are normalized.
std.math.absI32(value)u32Returns the unsigned magnitude of a signed 32-bit value.
std.math.absI64(value)u64Returns the unsigned magnitude of a signed 64-bit value.
std.math.checkedAddU32(left, right)Maybe<u32>Adds only when the result fits in u32.
std.math.checkedSubU32(left, right)Maybe<u32>Subtracts only when the result fits in u32.
std.math.checkedMulU32(left, right)Maybe<u32>Multiplies only when the result fits in u32.
std.math.checkedAddUsize(left, right)Maybe<usize>Adds only when the result fits in usize.
std.math.checkedSubUsize(left, right)Maybe<usize>Subtracts only when the result fits in usize.
std.math.checkedMulUsize(left, right)Maybe<usize>Multiplies only when the result fits in usize.
std.math.checkedAddI32(left, right)Maybe<i32>Adds only when the result fits in i32.
std.math.checkedSubI32(left, right)Maybe<i32>Subtracts only when the result fits in i32.
std.math.checkedMulI32(left, right)Maybe<i32>Multiplies only when the result fits in i32.
std.math.saturatingAddU32(left, right)u32Adds and clamps overflow to u32 max.
std.math.saturatingSubU32(left, right)u32Subtracts and clamps underflow to 0.
std.math.saturatingMulU32(left, right)u32Multiplies and clamps overflow to u32 max.
std.math.saturatingAddUsize(left, right)usizeAdds and clamps overflow to usize max.
std.math.saturatingSubUsize(left, right)usizeSubtracts and clamps underflow to 0.
std.math.saturatingMulUsize(left, right)usizeMultiplies and clamps overflow to usize max.
std.math.saturatingAddI32(left, right)i32Adds and clamps overflow to the nearest i32 bound.
std.math.saturatingSubI32(left, right)i32Subtracts and clamps overflow to the nearest i32 bound.
std.math.saturatingMulI32(left, right)i32Multiplies and clamps overflow to the nearest i32 bound.
std.math.gcdU32(left, right)u32Euclidean greatest common divisor.
std.math.lcmU32(left, right)u32Least common multiple; returns 0 when either input is 0.
std.math.checkedLcmU32(left, right)Maybe<u32>Least common multiple only when the result fits in u32.
std.math.powU32(base, exponent)u32Fixed-width exponentiation by squaring.
std.math.checkedPowU32(base, exponent)Maybe<u32>Exponentiation only when the result fits in u32.
std.math.modPowU32(base, exponent, modulus)u32Modular exponentiation; returns 0 for modulus 0.
std.math.isPrimeU32(value)BoolTrial division primality for unsigned integers.
std.math.isEvenU32(value)BoolReports whether a u32 value is even.
std.math.isOddU32(value)BoolReports whether a u32 value is odd.
std.math.sqrtFloorU32(value)u32Integer square root rounded down.
std.math.factorialU32(value)Maybe<u32>Factorial only when the result fits in u32.
std.math.binomialU32(n, k)Maybe<u32>Binomial coefficient only when the result fits in u32.
std.math.divisorCountU32(value)u32Counts positive divisors; returns 0 for 0.
std.math.properDivisorSumU32(value)u32Sums positive divisors smaller than value.

Current scope:

  • Helpers are pure, target-neutral fixed-width integer operations.
  • Checked helpers return Maybe<T> instead of wrapping or trapping.
  • Saturating helpers clamp to documented integer bounds.
  • The module does not provide floating-point math, big integers, or arbitrary-precision number theory.

Example

pub fn main(world: World) -> Void raises {    if std.math.gcdU32(84, 30) == 6 && std.math.isPrimeU32(31) {        check world.out.write("math helper ok\n")    }}

Design Notes

std.math does not allocate and does not require a hosted runtime capability. Names include the integer width because Zero does not overload standard-library helpers by argument type.

Number-theory helpers are intentionally simple and deterministic. They are suitable for small fixed-width tasks, examples, and compiler-portable checks. Large-number algorithms should be added as explicit APIs with documented bounds instead of hidden heap allocation or implicit widening.