This library is in flux. APIs may change without notice. Generated from source with koruc 0.1.7 on 11/4/2026.

Num

import std/num

std/num — arbitrary-precision decimal numbers, taint-sourced.

num.kz · 32 tors

std/num — arbitrary-precision decimal numbers, taint-sourced. · 31 more lines
std/num — arbitrary-precision decimal numbers, taint-sourced. Koru has no numeric tower: bare literals are raw expression text handed to the host. This module is the tower. A `num` is a CANONICAL decimal string — `[-]d[.ddd]e[-]E`, one nonzero leading digit, no trailing zeros, zero is `0e0` — so value-equivalence is string equality (`num:equiv` is the honest form of that law: `314e0` ≡ `3.14e2` ≡ `0.314e3`). Taint (the 330_089 mechanism applied to numbers — "tainted for real"): <external!> issued by `ingest`/`ingest-owned` on outside text. Marks the value as unvalidated input; nothing but `parse` may consume it. `ingest-owned` additionally discharges <allocated!> — the buffer becomes num-owned. <exact!> issued by `parse`/`lit` and every op. A REAL obligation: ops consume <!exact> linearly (each operand dies into the result — `dup` is the lawful fork), `free` discharges by deallocating, `render` swaps it to <allocated!> for print. A bare `string` cannot reach `add` — it must pass the gate. The marks are phantoms: zero runtime cost, all checking at compile time. At runtime a num is just a []const u8 the module owns; every num-owned buffer is allocator-freed exactly once, at consume time. Arithmetic: mantissa is an arbitrary-precision integer (std.math.big), the exponent is decimal. Exact for +,-,*,pow; `div`/`sqrt` take an explicit significant-digit `prec` because a decimal expansion need not terminate. Port source: isa-physics `plex/lib/symbolic/arbitrary_precision.py` (sign + decimal-position + digit nibbles) and the ISAR QuantityKernel MetricExpr exact-rational layer; the quantity/dimension half lives in std/quantity.

Phantom lifecycles

Derived from the phantom labels in the declarations below — state! issues an obligation the compiler will chase, !state discharges it, a bare state holds it without moving it. Nothing here is hand-drawn.

string 4 states external!exact!allocated!

ingest

<external!> koru_std/num.kz:1004

ingest-owned

<!allocated><external!> koru_std/num.kz:1013

parse

<!external><exact!><std.io:allocated!> koru_std/num.kz:1027

add

<!exact><exact!> koru_std/num.kz:1062

sub

<!exact><exact!> koru_std/num.kz:1076

mul

<!exact><exact!> koru_std/num.kz:1092

div

<!exact><exact!> koru_std/num.kz:1108

neg

<!exact><exact!> koru_std/num.kz:1127

abs

<!exact><exact!> koru_std/num.kz:1137

pow

<!exact><exact!> koru_std/num.kz:1149

qadd

<!exact><exact!> koru_std/num.kz:1177

sqrt

<!exact><exact!> koru_std/num.kz:1198

equiv

<exact> koru_std/num.kz:1238

is-zero

<exact> koru_std/num.kz:1253

free

<!exact> koru_std/num.kz:1272

drop

<!external> koru_std/num.kz:1280

from-int

<exact!> koru_std/num.kz:1287

render

<!exact><std.io:allocated!> koru_std/num.kz:1300

exp

<!exact><exact!> koru_std/num.kz:1314

ln

<!exact><exact!> koru_std/num.kz:1330

sin

<!exact><exact!> koru_std/num.kz:1344

cos

<!exact><exact!> koru_std/num.kz:1360

atan

<!exact><exact!> koru_std/num.kz:1376

powf

<!exact><exact!> koru_std/num.kz:1389

mod

<!exact><exact!> koru_std/num.kz:1448

length

<exact> koru_std/num.kz:1466

scale

<exact> koru_std/num.kz:1478

to-base

<!exact><exact!> koru_std/num.kz:1488

from-base

<exact!> koru_std/num.kz:1519