std/bigint

March 14, 2026 ยท View on GitHub

BigInt provides arbitrary-precision integer arithmetic for Zen-C. It allows for calculations with integers that exceed the capacity of standard numeric types like u64.

Overview

  • Arbitrary Precision: Numbers are limited only by available memory.
  • Decimal-based: Currently uses a simple base-10 representation for simplicity.
  • RAII: Implements the Drop trait for automatic memory management of internal digit storage.
  • Convenient: Supports operator overloading for intuitive arithmetic.

Usage

import "std/bigint.zc"

fn main() {
    let a = BigInt::from_int(1_000_000_000_000_000);
    let b = BigInt::from_int(2_000_000_000_000_000);
    
    // Uses operator overloading
    let sum = a + b; 
    
    let s = sum.to_string();
    println "Sum: {s}";
    free(s);
} // sum, a, and b are freed automatically here

Struct Definition

struct BigInt {
    digits: Vec<u8>*;
}

Methods

Construction

MethodSignatureDescription
newBigInt::new() -> BigIntCreates a new BigInt initialized to 0.
from_intBigInt::from_int(val: u64) -> BigIntCreates a new BigInt from a 64-bit integer.

Modification

MethodSignatureDescription
add_in_placeadd_in_place(self, other: BigInt)Adds other to self by mutating the internal state.

Utility

MethodSignatureDescription
cloneclone(self) -> BigIntReturns a deep copy of the BigInt.
to_stringto_string(self) -> char*Returns a heap-allocated string representation.

Operators

OperatorMethodDescription
+addReturns a new BigInt containing the sum of two values.
{}to_stringAutomatically enables interpolation in formatted strings.

Memory Management

MethodSignatureDescription
free_memfree_mem(self)Manually frees the underlying Vec and BigInt storage.
Traitimpl Drop for BigIntAutomatically calls free_mem() when out of scope.