math.floats
how computers store decimals: the bits of 16, 32, 64, 80 and 128-bit floats, and rounding to a smaller format
How computers store numbers with decimals. A float is a sign, an exponent and a mantissa packed into 16, 32, 64, 80 or 128 bits, and most decimals like 0.1 can only be stored approximately.
good to know
Section titled “good to know”Name a format by its width: 16 (half), 32 (single), 64 (double, what zil uses), 80 (x87 extended) or 128 (quad), or "bf16" for the 16-bit brain float that machine learning uses. zil’s own numbers are 64-bit, so 80 and 128-bit values are rounded to 64 bits when read back.
Tags: binary, numbers
float_bitsthe bit pattern of x in a float format, shown in hexfrom_float_bitsthe float with the given bit pattern, the reverse of float_bitsfloat_partsthe sign, exponent and mantissa fields of x as stored, as a mapnearest_floatx rounded to the nearest number a float format can storefloat_errorhow far x is from the nearest number a float format can store, as nearest minus x
syntax
Section titled “syntax”1# to fp(32): rounded to the nearest 32-bit float20.1 to fp(32)0.1Functions
Section titled “Functions”float_bits
Section titled “float_bits”float_bits(x: num, format?: int|str) -> any
the bit pattern of x in a float format, shown in hex
The format is a width: 16, 32, 64, 80 or 128, or “bf16”
1float_bits(52.345, 32)0x425161482float_bits(1, 16)0x3c003float_bits(-2.5)0xc004000000000000See also from_float_bits, float_parts
from_float_bits
Section titled “from_float_bits”from_float_bits(bits: int, format?: int|str) -> float
the float with the given bit pattern, the reverse of float_bits
1from_float_bits(0x42516148, 32)52.3452from_float_bits(0x3c00, 16)1See also float_bits
float_parts
Section titled “float_parts”float_parts(x: num, format?: int|str) -> map
the sign, exponent and mantissa fields of x as stored, as a map
The exponent is the stored (biased) one, and the mantissa excludes the hidden leading 1
1float_parts(0.1){sign: 0, exponent: 1019, mantissa: 0x999999999999a}2float_parts(1.5, 32){sign: 0, exponent: 127, mantissa: 0x400000}See also float_bits
nearest_float
Section titled “nearest_float”nearest_float(x: num, format: int|str) -> float
x rounded to the nearest number a float format can store
Same as x to fp(bits)
1nearest_float(0.1, 32)0.12nearest_float(100000, 16)∞30.1 to fp(16)0.0999756See also float_error
float_error
Section titled “float_error”float_error(x: num, format: int|str) -> float
how far x is from the nearest number a float format can store, as nearest minus x
1float_error(0.1, 32)1.49012e-92float_error(0.5, 16)0See also nearest_float
More examples
Section titled “More examples”floats
Section titled “floats”1# the bits of a 32-bit float2float_bits(52.345, 32)0x425161483# the parts of a double4float_parts(0.1){sign: 0, exponent: 1019, mantissa: 0x999999999999a}5# what 0.1 becomes in 32 bits6nearest_float(0.1, 32)0.17# and how far off that is8float_error(0.1, 32)1.49012e-9Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.