Integer Overflow

Integer arithmetic that overflows the representable range of its result type MUST wrap around modulo 2^N, where N is the bit width of the type. The result is the unique value in the type's range that is congruent to the mathematical result modulo 2^N.

Integer overflow does not cause a runtime panic and does not abort the program.

The following operations wrap on overflow:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Negation (- unary)
fn main() -> i32 {
    2147483647 + 1  // wraps to -2147483648
}
fn main() -> i32 {
    -2147483648 - 1  // wraps to 2147483647
}

Future versions of Gruel may provide checked or saturating arithmetic operations as alternatives to the default wrapping semantics.