Floating-point numbers
IEEE 754: sign, exponent and fraction fields, decoding $-12.5$ bit by bit, machine epsilon, why $0.1+0.2\neq 0.3$, specials and narrow ML formats.
Why not fixed point
Integers are exact on a short, evenly spaced range: 32 bits hold every integer from about to , spaced 1 apart. Science needs and with a few significant digits, and fractions such as .
Notation. with sign , significand , exponent . Stored fields: unsigned exponent , fraction , bias, precision (24 single, 53 double). is rounded to a representable value; the local gap; machine epsilon; unit roundoff.
Baseline: fixed point. Q16.16 reads a 32-bit two’s-complement integer as . Spacing everywhere; becomes (0.006% error). Absolute error is fixed, so relative error explodes for tiny values — is stored 53% too large, and anything below becomes 0. The range stops near 32,768, so a product of two moderate numbers overflows. Fixed point survives when the range is known (DSP, quantized nets). For general work the scale must travel with the number.
IEEE 754 formats
Key idea. Write (normalized). The exponent moves the binary point; the same bits give comparable relative precision from to in 32 bits. Any nonzero real can be written with : , . The leading 1 of a normalized significand is hidden — one free bit of precision.
IEEE 754 (1985; revised 2008, 2019) fixed the layout so conforming machines agree:
| Format | Sign | Bias | |||
|---|---|---|---|---|---|
| binary32 | 1 | 8 | 23 | 127 | 24 |
| binary64 | 1 | 11 | 52 | 1023 | 53 |
For : , . Bias (not two’s complement) makes positive floats sort like unsigned integers. Single range: smallest normal , largest finite ; double: to . Single: (~7 decimal digits); double: (~16 digits).
Decoding a float bit by bit
Worked example. 0xC1480000:
- Bits
1 10000010 1001000…— sign 1 (negative). - , so .
- Fraction starts
1001; with hidden bit, . - Value .
Encoding reverses the steps. is periodic because 10 has a factor 5 absent from base 2: the stored single is 0x3DCCCCCD exactly. The nearest double is closer () but still not .
Rounding and machine epsilon
In a format of precision has equally spaced numbers; spacing is . Gaps double at every power of two; relative spacing stays between and . Default rounding: round to nearest, ties to even.
Every IEEE arithmetic op is correctly rounded: with .
Above , single ulp is 2. Adding 1 to a float counter ties and rounds back to — the counter sticks. Prefer int or double for counters that climb that high.
Why 0.1 + 0.2 ≠ 0.3
In double, 0.1 + 0.2 == 0.3 is false: each literal rounds, then the sum rounds (including a tie that rounds to even). Printing with few decimals hides the difference; comparing with == does not.
Addition is not associative. Summing many positive numbers from large to small can lose the small ones entirely once they fall below half an ulp of the running total; summing small-to-large, or using compensated (Kahan) summation, keeps more bits. Prefer higher precision or a compensated algorithm when those trailing bits matter — scientific codes often accumulate in double even when the data are float.
Specials and narrow formats
Extreme : all zeros → ±0 and subnormals (gradual underflow, no hidden 1 — denser spacing near zero at the cost of slower hardware on some chips); all ones → () or NaN (). Operations that have no real result produce NaN; any op with a NaN operand yields NaN. arises from overflow or division by zero of a nonzero; and are NaN.
Narrow ML formats: float16 (5-bit exponent, 10-bit fraction), bfloat16 (8-bit exponent like float, 7-bit fraction — keeps range, loses precision), fp8. Training often keeps master weights in float32 while compute runs in bf16. RISC-V F/D extensions add f0–f31 and ops such as fadd.s, fmul.s, fused fmadd.s (one rounding instead of two — critical for accurate inner products).
Foundations are complete: bits of integers and reals. The next topic, The processor and the instruction set, asks which operations a program may rely on — the ISA as a contract — and introduces RISC-V’s registers.
Encoding +12.5 mirrors the worked −12.5: sign 0, , fraction starting 1001, word 0x41480000. Comparing the two patterns as unsigned integers preserves order among positive floats — a property used by sorting networks and some GPU paths that treat float bits as ints for key compares when signs match. Subnormals fill the gap under : the smallest positive subnormal is . Flushing them to zero (FTZ/DAZ modes on some ISAs) speeds codes that do not need gradual underflow, at the cost of breaking IEEE default semantics.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Go deeper — optional
Code and labs do not count toward the main percentage or the certificate.
- Decode FP32 bit fieldsoptional
Split 32 bits into sign/exponent/mantissa; rebuild ±1.f×2^e and match numpy.float32.
~20 min · easier on a computer · The matching course topic has been read.
Self-check in the notebook — not part of mastery or the certificate.
Answers are saved on the server and tied to this browser until you sign in.