0012. Integer arithmetic: truncating division, traps on overflow
- Status: Accepted
- Date: 2026-10-29
- Plan: M1.8, M3.4, M5.7
- Review: Language semantics
Context
Checked i64 is the default (ADR-0007). C signed overflow,
division by zero, and both INT64_MIN / -1 and
INT64_MIN % -1 have undefined behaviour. Harness
needs deterministic results and traps before its interpreter
arrives.
Decision
Division truncates toward zero; remainder takes the dividend's
sign. INT64_MIN % -1 returns 0, its exact
mathematical remainder, while INT64_MIN / -1 traps
because its quotient cannot fit in i64.
Addition, subtraction, multiplication and negation trap on
integer overflow. Division and remainder trap when the divisor is
zero. The only trap kinds are integer overflow and
division by zero. A trap flushes stdout, prints
trap: <kind> at <file>:<line>:<col>
on stderr and exits 101. The location is the operator token, and
evaluation (including traps) is left to right.
Alternatives considered
- Wrap overflow — hides mistakes and conflicts with checked i64 by default.
- Trap on
INT64_MIN % -1— the mathematical remainder fits; exposing C's division instruction limitation would make the language less predictable. - Floor division — differs from Rust, C and Go and from existing goldens.
Consequences
The C runtime checks overflow with gcc/clang builtins and guards division and remainder before executing them. M3.4 must reproduce these results and trap messages exactly. Explicit wrapping arithmetic remains a separate M5.7 feature.