Moving the point
Scientists write 6250 as 6.25 × 10³: a few digits, times a power of ten. The power says where the decimal point goes. A floating-point number works the same way, but in base 2. Our example is 6.25.
In binary, places left of the point are worth 4, 2 and 1. Places right of it are halves: ½, ¼, ⅛. Take out each place that fits: 6.25 = 4 + 2 + ¼. So 6.25 is 110.01 in binary.
Now slide the point left until one 1 stands in front of it: 110.01 becomes 1.1001. Each move halves the value, so we multiply by 2 to keep the value: 6.25 = 1.1001 × 2². This is called normalising.
Packing the bits
A float packs three fields into a row of bits. We use the 16-bit half-precision format: 1 sign bit, 5 exponent bits and 10 fraction bits. The sign bit is 0 for plus and 1 for minus. 6.25 is positive: 0.
The exponent 2 is stored with a bias of 15 added, so it never needs a minus sign: 2 + 15 = 17. In 5 bits, 17 is 10001. With this bias, normal numbers use exponents −14 to +15.
After normalising, the digit before the point is always 1, so it is not stored: it is the hidden bit. The 10 fraction bits keep the digits after the point, 1001, and zeros fill the rest: 1001000000.
To read the bits, undo each step. Sign 0: plus. Exponent 17 − 15 = 2. Fraction: put the hidden 1 back, 1.1001 = 1 + ½ + 1/16 = 1.5625. Then 1.5625 × 2² = 6.25. Python's struct agrees: hex 4640.
Rounding
Now try 0.1. In binary it is 0.000110011001100…, and the 1100 pattern never ends, just as 1/3 = 0.333… never ends in decimal. Normalised, 0.1 = 1.1001 1001 1001… × 2⁻⁴.
Store 0.1 the same way: sign 0, exponent −4 + 15 = 11. Only the first 10 fraction bits fit: 1001100110. The rest is rounded away, so the half float holds 0.0999755859375 (hex 2e66), a little less than 0.1.
In Python
Python's float uses the 64-bit double format: 1 sign bit, 11 exponent bits and 52 fraction bits. That is far more precise, but 0.1 is still not exact, so 0.1 + 0.2 gives 0.30000000000000004.
(6.25).hex() writes a double as hex digits times a power of two: '0x1.9000000000000p+2'. The 1 is the hidden bit, hex 9 is binary 1001, and p+2 means × 2². A double's bias is 1023, so it stores 2 + 1023 = 1025.
Try it in the Python prompt
;; step 1/11 · Scientific notation >>> 6.25e3 # 6.25 × 10**3 6250.0 >>> 6250 / 10**3 6.25
IEEE 754 defines the common formats. Half precision (binary16) has 1 sign, 5 exponent and 10 fraction bits, bias 15. Single (binary32) has 1, 8 and 23, bias 127. Double (binary64), Python's float, has 1, 11 and 52, bias 1023. Two exponent patterns are reserved. All 0s means zero or a tiny "subnormal" number with no hidden 1; all 1s means infinity or NaN (not a number). That is why half-precision exponents run only from −14 to +15.