← All examples

Overflow sites

Plain math (plus minus times pow) is exact, and checked where the value lands: a typed declaration, a change, an argument, a return.

overflow_sites.zg
# Plain math (plus minus times pow) is exact, and checked where the value lands: a typed declaration,
# a `change`, an argument, a return (owner, 2026-09-23). In between, the one limit is
# the machine's 64 bits. Wrapping (plus.wrap minus.wrap times.wrap pow.wrap) wraps at the size of its values.
# tests/reject/overflow_lands.zg and overflow_past_64_bits.zg are the two that fail.
MAIN {
    let 'small': int16 = 5;
    let 'big': int64 = (20000 plus 20000) plus 'small';    # 40005: the true answer, and it fits int64
    let 's': int16 = 30000;
    let 'twice': int64 = 's' plus 's';                   # 60000, though no int16 holds it
    let 'tiny': int8 = (100 plus 100) minus 150;             # 50: the 200 in between is fine
    let 'v': int8 = 112;
    std::print.useful['big', " ", 'twice', " ", 'tiny', " ", 'v' > ('v' plus 'v'), newln];   # 224 compared as 224
    let 'u': uint8 = 5;
    let 'below' := 'u' minus 10;                          # -5: `:=` takes a type that holds it
    let 'x': uint32 = 4000000000;
    let 'h': uint64 = 'x' plus.wrap 'x';                     # wraps at uint32, 'x''s size: 3705032704
    let 'a': int8 = 100;
    let 'b': int16 = 1000;
    let 'w' := to.int16['a' plus 'b'] plus.wrap 1;            # a mix of sizes wraps where you say: 1101
    std::print.useful['below', " ", 'h', " ", 'w', newln];
}
Output
40005 60000 50 false
-5 3705032704 1101