Sooner or later a product overflows. Mica’s integers unit answers in two
sizes: wide fixed widths — int128, uint128, int256, uint256,
published names over a parameterized family int[N]/uint[N] that admits
any width above 64 — and the unbounded bigint, whose size is whatever
the value needs. The first stays in registers and ordinary operators; the
second trades operators for verbs and never runs out.
The example is examples/BigIntegers
in the tutorial repository.
Getting the file
make -C examples/BigIntegers runBigIntegers
the wide family
4e18 * 3 held exactly in int128: 1
the unbounded integer
25! = 15511210043330985984000000
2^100 = 1267650600228229401496703205376
25! div 1e9 = 15511210043330985, rest 984000000
the parsed text equals the computed factorial: 1The example, walked
The wide widths
p := (a as int128) * (b as int128);Four quintillion times three does not fit an int64; in int128 it is just
arithmetic. The casts are explicit because widening a value into a wider
computation is a decision the source should show — part 3’s rule, unchanged
at 128 bits. The wide names are ordinary types: declare variables, compute,
compare. Two boundaries worth knowing: wide integers are numeric, not
ordinal — they compute, but they do not index arrays or drive a case
(ordinals owns that line) — and they print through
verdicts or bigint’s Text, not through a format specifier.
The unbounded integer
f := FromInteger(1);
for k := 1 to 25 do
f := Multiply(f, FromInteger(k));bigint is a value under the value rule — assignment copies, no hidden
sharing — spoken to through verbs: FromInteger and Parse bring values
in, Text renders, Add, Subtract, Multiply, Divide, Modulo,
Pow compute, Compare orders, and DivMod answers a division’s both
results in one call, the remainder through a lent address exactly as
part 5 taught. Twenty-five factorial is six digits
past int64’s ceiling and prints whole; two to the hundredth is
thirty-one digits and does not care.
Try it
1. Find int64’s edge. Compute 21! with plain int64 multiplication
in a scratch program. At the default build the product wraps to a wrong
number; rebuild with --optimize checked and the trap names its line:
Mica runtime failure: reason=signed_arithmetic_overflow (2)
Mica runtime context: file=Fact21.mica, line=9, column=16
Mica runtime source: f := f * k;Then compute it in bigint and print it. The behaviors are one policy in
three tiers: the checked build refuses to lie, and the unbounded integer
removes the ceiling entirely.
2. Parse something enormous. Parse a hundred-digit number, Multiply
it by itself, print the Text. The digit count doubles; nothing else
changes.
Next
The math unit for the scalar verbs beside these types, and Vectors and matrices for values with shape. Every example lives in the tutorial repository.