Skip to main content

RT001: Integer division or modulo by zero

Code: RT001
Category: Runtime / arithmetic
Since: v0.51.1 (before that, / crashed with a Go panic and % reported [RT_DIV0])

What this error means​

An integer / or % ran with a divisor of 0. The program stops with exit code 1 and names the expression that divided:

Error: execution failed: RT001: integer division by zero at dz.ail:3:39

modulo replaces division for %. The bytecode VM (ailang run --bytecode) reports the same code and text with a file:line position.

Float division is not affected: it follows IEEE 754, so 1.0 / 0.0 is +Inf and 5.5 % 0.0 is NaN.

MinInt / -1 is not an error either: it wraps to MinInt (and MinInt % -1 is 0), as in Go.

Example​

module dz

export func boom(n: int) -> bool = 10 / n > 0

ailang run --entry boom --args-json 0 dz.ail fails with RT001.

How to fix​

Guard the divisor, or return an Option/Result when zero is a legitimate input:

module dz
import std/option (Option, Some, None)

export func safeDiv(n: int, d: int) -> Option[int] =
if d == 0 then None else Some(n / d)

In hosts​

  • REPL: the error is printed and the session continues.
  • serve-api: the request gets HTTP 500 with the message in error and error_detail.code = "RT001".
  • Go embedding (internal/embed): the call returns an error; errors.As(err, &dz) with dz *errors.DivByZeroError gives the operation and position.