On this page

0012. Integer arithmetic: truncating division, traps on overflow

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

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.