← All examples

Bin widths

A bin16/bin32 holds what its width holds: a value is rounded to it where it lands (DECISIONS 2026-09-14).

bin_widths.zg
# A bin16/bin32 holds what its width holds: a value is rounded to it where it lands
# (DECISIONS 2026-09-14). `print` shows what is stored, `print.useful` the shortest text that
# gives the value back at its own width.
MAIN {
    let 'f': bin32 = 0.1;
    let 'h': bin16 = 0.1;
    let 'd': bin64 = 0.1;
    std::print.stored['f', newln];     # 0.100000001490116119384765625
    std::print.stored['h', newln];     # 0.0999755859375
    std::print.stored['d', newln];
    std::print.useful['f', " ", 'h', " ", 'd', newln];     # all three: 0.1
    let 'big': bin16 = 65519.0;                          # rounds down to bin16's largest, 65504
    let 'g' := to.bin32["0.1"] otherwise 0.0;            # a conversion lands at bin32 too
    std::print.stored['big', " ", 'g', newln];
    # bin32 math rounds after every step, as the hardware does (owner, 2026-09-24: a):
    # 16777216 plus 1 rounds back to 16777216 at bin32, while bin64 holds 16777218
    let 'x': bin32 = 16777216.0;
    let 'y': bin32 = 'x' plus 1.0 plus 1.0;
    let 'x64': bin64 = 16777216.0;
    let 'y64': bin64 = 'x64' plus 1.0 plus 1.0;
    std::print.useful['y', " ", 'y64', newln];
    # a literal is rounded once, from its digits, straight to its width (never to 64 bits first)
    let 'top': bin32 = 340282356779733661637539395458142568447.0;   # just under the limit: bin32's largest
    let 'near': bin32 = 1.00000005960464477539062500001;            # just past a halfway point: rounds up
    std::print.stored['top', " ", 'near', newln];
    # text is rounded once from its digits too
    let 't16' := to.bin16["65519.99999999999999999"] otherwise -1.0;    # just under the limit: 65504
    let 't32' := to.bin32["1.00000005960464477539062500001"] otherwise -1.0;
    let 'tiny' := to.bin16["2.98023223876953126e-8"] otherwise -1.0;   # just past half the smallest step
    std::print.stored['t16', " ", 't32', " ", 'tiny', newln];
}
Output
0.100000001490116119384765625
0.0999755859375
0.1000000000000000055511151231257827021181583404541015625
0.1 0.1 0.1
65504 0.100000001490116119384765625
16777216 16777218
340282346638528859811704183484516925440 1.00000011920928955078125
65504 1.00000011920928955078125 0.000000059604644775390625