Checked Arithmetic
Pyro is implemented in C — a language where signed integer overflow is classified as undefined behaviour.
Overflow happens if the result of an integer operation is outside the range of the integer type — e.g. the addition operation i64_max + 1
will overflow because the sum of the two numbers is outside the range of the i64
type.
Pyro's standard arithmetic operators don't check for integer overflow. Instead, Pyro has a set of builtin functions for performing checked arithmetic.
-
$add(a: any, b: any) -> any
-
Checked addition. Returns
a + b
.If
a
andb
are bothi64
values, this function will panic if the result of the operation would overflow. -
$mul(a: any, b: any) -> any
-
Checked multiplication. Returns
a * b
.If
a
andb
are bothi64
values, this function will panic if the result of the operation would overflow. -
$sub(a: any, b: any) -> any
-
Checked subtraction. Returns
a - b
.If
a
andb
are bothi64
values, this function will panic if the result of the operation would overflow.
Pyro doesn't have a dedicated function for checked division. The only case of integer division that can overflow is i64_min // -1
— the truncating-division operator //
automatically checks for this case, along with division by zero.