Skip to content

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.

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_bits the bit pattern of x in a float format, shown in hex
  • from_float_bits the float with the given bit pattern, the reverse of float_bits
  • float_parts the sign, exponent and mantissa fields of x as stored, as a map
  • nearest_float x rounded to the nearest number a float format can store
  • float_error how far x is from the nearest number a float format can store, as nearest minus x
1
# to fp(32): rounded to the nearest 32-bit float
2
0.1 to fp(32)0.1
Example outputCtrl+Enter to run
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”

1
float_bits(52.345, 32)0x42516148
2
float_bits(1, 16)0x3c00
3
float_bits(-2.5)0xc004000000000000
Example outputCtrl+Enter to run

See also from_float_bits, float_parts

from_float_bits(bits: int, format?: int|str) -> float

the float with the given bit pattern, the reverse of float_bits

1
from_float_bits(0x42516148, 32)52.345
2
from_float_bits(0x3c00, 16)1
Example outputCtrl+Enter to run

See also float_bits

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

1
float_parts(0.1){sign: 0, exponent: 1019, mantissa: 0x999999999999a}
2
float_parts(1.5, 32){sign: 0, exponent: 127, mantissa: 0x400000}
Example outputCtrl+Enter to run

See also float_bits

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)

1
nearest_float(0.1, 32)0.1
2
nearest_float(100000, 16)∞
3
0.1 to fp(16)0.0999756
Example outputCtrl+Enter to run

See also 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

1
float_error(0.1, 32)1.49012e-9
2
float_error(0.5, 16)0
Example outputCtrl+Enter to run

See also nearest_float

1
# the bits of a 32-bit float
2
float_bits(52.345, 32)0x42516148
3
# the parts of a double
4
float_parts(0.1){sign: 0, exponent: 1019, mantissa: 0x999999999999a}
5
# what 0.1 becomes in 32 bits
6
nearest_float(0.1, 32)0.1
7
# and how far off that is
8
float_error(0.1, 32)1.49012e-9
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.