std/bigint

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

Usage

import "std/bigint.zc"
import "std/io.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(f"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.
from_stringBigInt::from_string(s: char*) -> Result<BigInt>Parses a decimal string (digits only) into a BigInt.

Arithmetic

MethodSignatureDescription
addadd(self, other: BigInt*) -> BigIntReturns the sum of two values.
subsub(self, other: BigInt*) -> BigIntSubtracts other from self. Panics if the result would be negative.
mulmul(self, other: BigInt*) -> BigIntReturns the product of two values.
divdiv(self, divisor: BigInt*) -> BigIntReturns the integer quotient of self / divisor.
remrem(self, divisor: BigInt*) -> BigIntReturns the remainder of self / divisor.

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.
comparecompare(self, other: BigInt*) -> intReturns -1, 0, or 1 when self is less than, equal to, or greater than other.
is_zerois_zero(self) -> boolReturns true when the value is zero.
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
freefree(self)Manually frees the underlying Vec and BigInt storage.
free_memfree_mem(self)Deprecated alias for free.
Traitimpl Drop for BigIntAutomatically calls free() when out of scope.