Skip to content
BIASED

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 −2.1×109-2.1\times 10^9 to 2.1×1092.1\times 10^9, spaced 1 apart. Science needs 10−3010^{-30} and 103010^{30} with a few significant digits, and fractions such as 0.10.1.

Notation. x=(−1)s⋅m⋅2ex=(-1)^s\cdot m\cdot 2^e with sign ss, significand mm, exponent ee. Stored fields: unsigned exponent EE, fraction ff, bias, precision pp (24 single, 53 double). fl(x)\mathrm{fl}(x) is xx rounded to a representable value; ulp⁡(x)\operatorname{ulp}(x) the local gap; ε=21−p\varepsilon=2^{1-p} machine epsilon; u=ε/2=2−pu=\varepsilon/2=2^{-p} unit roundoff.

Baseline: fixed point. Q16.16 reads a 32-bit two’s-complement integer qq as q/216q/2^{16}. Spacing 2−16≈1.5×10−52^{-16}\approx 1.5\times 10^{-5} everywhere; 0.10.1 becomes 6554/65536≈0.1000066554/65536\approx 0.100006 (0.006% error). Absolute error is fixed, so relative error explodes for tiny values — 10−510^{-5} is stored 53% too large, and anything below 2−172^{-17} 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 x=±1.f×2ex=\pm 1.f\times 2^e (normalized). The exponent moves the binary point; the same bits give comparable relative precision from 10−3810^{-38} to 103810^{38} in 32 bits. Any nonzero real can be written with 1≤m<21\le m<2: 6.5=1.1012×226.5=1.101_2\times 2^2, −12.5=−1.10012×23-12.5=-1.1001_2\times 2^3. 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 EE ff Bias pp
binary32 1 8 23 127 24
binary64 1 11 52 1023 53

For 0<E<Emax⁡0<E<E_{\max}: x=(−1)s×1.f×2E−biasx=(-1)^s\times 1.f\times 2^{E-\mathrm{bias}}, 1.f=1+f/2p−11.f=1+f/2^{p-1}. Bias (not two’s complement) makes positive floats sort like unsigned integers. Single range: smallest normal 2−126≈1.18×10−382^{-126}\approx 1.18\times 10^{-38}, largest finite ≈3.40×1038\approx 3.40\times 10^{38}; double: 2−1022≈2.23×10−3082^{-1022}\approx 2.23\times 10^{-308} to ≈1.80×10308\approx 1.80\times 10^{308}. Single: ε=2−23≈1.19⋅10−7\varepsilon=2^{-23}\approx 1.19\cdot 10^{-7} (~7 decimal digits); double: 2−52≈2.22⋅10−162^{-52}\approx 2.22\cdot 10^{-16} (~16 digits).

Decoding a float bit by bit

Worked example. 0xC1480000:

  1. Bits 1 10000010 1001000… — sign 1 (negative).
  2. E=13010E=130_{10}, so e=130−127=3e=130-127=3.
  3. Fraction starts 1001; with hidden bit, 1.10012=1+1/2+1/16=1.56251.1001_2=1+1/2+1/16=1.5625.
  4. Value (−1)1⋅1.5625⋅23=−12.5(-1)^1\cdot 1.5625\cdot 2^3=-12.5.

Encoding reverses the steps. 0.1=1.1001 1001…2×2−40.1=1.1001\,1001\ldots_2\times 2^{-4} is periodic because 10 has a factor 5 absent from base 2: the stored single is 0x3DCCCCCD =0.100000001490116119384765625≠0.1=0.100000001490116119384765625\neq 0.1 exactly. The nearest double is closer (0.10000000000000000555…0.10000000000000000555\ldots) but still not 0.10.1.

Rounding and machine epsilon

In [2e,2e+1)[2^e,2^{e+1}) a format of precision pp has 2p−12^{p-1} equally spaced numbers; spacing is ulp⁡(x)=2e−p+1\operatorname{ulp}(x)=2^{e-p+1}. Gaps double at every power of two; relative spacing stays between 2−p2^{-p} and 21−p2^{1-p}. Default rounding: round to nearest, ties to even.

Every IEEE arithmetic op is correctly rounded: fl(a∘b)=(a∘b)(1+δ)\mathrm{fl}(a\circ b)=(a\circ b)(1+\delta) with ∣δ∣≤u=ε/2|\delta|\le u=\varepsilon/2.

Above 224=16,777,2162^{24}=16{,}777{,}216, single ulp is 2. Adding 1 to a float counter ties and rounds back to 2242^{24} — 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 EE: 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 → ±∞\pm\infty (f=0f=0) or NaN (f≠0f\neq 0). Operations that have no real result produce NaN; any op with a NaN operand yields NaN. ∞\infty arises from overflow or division by zero of a nonzero; 0/00/0 and −1\sqrt{-1} 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, E=130E=130, 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 2−1262^{-126}: the smallest positive subnormal is 2−149≈1.4×10−452^{-149}\approx 1.4\times 10^{-45}. 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.

Full notes

Answers are saved on the server and tied to this browser until you sign in.