Floating Point (IEEE 754)
How floats are encoded, why they're inexact, rounding, special values (NaN, infinity, subnormals) and how to write numerically safe code.
Floating point represents real numbers in binary scientific notation. It covers an enormous range (about 10⁻³⁰⁸ to 10³⁰⁸ for doubles) with about 16 significant decimal digits. In exchange, most values are only approximations, and arithmetic doesn't follow the usual algebra rules exactly.
The format
| Format | Sign | Exponent | Fraction | Bias | Decimal digits |
|---|---|---|---|---|---|
| half (fp16) | 1 | 5 | 10 | 15 | ~3 |
| bfloat16 | 1 | 8 | 7 | 127 | ~2 |
| single (fp32) | 1 | 8 | 23 | 127 | ~7 |
| double (fp64) | 1 | 11 | 52 | 1023 | ~16 |
Worked example: −5.5 as a float
- Sign: negative → S = 1.
- Binary: 5.5 = 101.1₂ = 1.011₂ × 2².
- Exponent: 2 + 127 = 129 =
10000001. - Fraction: the bits after the leading 1 →
011followed by zeros. The leading 1 is implicit and not stored, which gains one bit of precision.
Precision and spacing
Floats are dense near zero and sparse far from it. The gap between adjacent doubles is about value × 2⁻⁵². Above 2⁵³, consecutive doubles are 2 apart, so integers can no longer all be represented (that's JavaScript's Number.MAX_SAFE_INTEGER).
Machine epsilon (ε ≈ 2.2 × 10⁻¹⁶ for doubles) is the gap between 1.0 and the next representable number. Any single operation's relative error is at most ε/2.
Special values
| Exponent bits | Fraction | Meaning |
|---|---|---|
| all 0 | 0 | ±0 (there is a −0.0) |
| all 0 | ≠ 0 | Subnormal: very small numbers with reduced precision |
| all 1 | 0 | ±Infinity (e.g. 1.0 / 0.0) |
| all 1 | ≠ 0 | NaN (e.g. 0.0 / 0.0, √−1) |
Subnormals fill the gap between 0 and the smallest normal number so that x - y == 0 only when x == y (gradual underflow). On some CPUs they're 10–100× slower, which is why audio and ML code sometimes enables flush-to-zero.
Rounding
IEEE 754 requires each basic operation (+, −, ×, ÷, √) to be correctly rounded: computed as if exactly, then rounded. The default mode is round to nearest, ties to even (banker's rounding), which avoids a systematic upward bias.
System.out.println(0.1 + 0.2); // 0.30000000000000004
System.out.println(Math.round(2.5)); // 3 (Math.round rounds half up)
System.out.println(Math.rint(2.5)); // 2.0 (ties to even)
new BigDecimal("2.675").setScale(2, RoundingMode.HALF_UP); // 2.68 exactly0.1 + 0.2 # 0.30000000000000004
round(2.5) # 2 (ties to even)
round(2.675, 2) # 2.67 — 2.675 is really 2.67499999...
from decimal import Decimal, ROUND_HALF_UP
Decimal("2.675").quantize(Decimal("0.01"), ROUND_HALF_UP) # 2.680.1 + 0.2; // 0.30000000000000004
Math.round(2.5); // 3
(2.675).toFixed(2); // "2.67" (binary value is slightly below 2.675)Arithmetic is not associative
(a + b) + c can differ from a + (b + c):
(1e16 + 1) - 1e16 # 0.0 (the 1 was lost when added to 1e16)
1e16 + (1 - 1e16) # 2.0Consequences:
- Catastrophic cancellation: subtracting nearly equal numbers wipes out significant digits. Rewrite formulas to avoid it (for example, the quadratic formula has a stable variant).
- Summation order matters: summing many small numbers into a large one loses them. Use Kahan summation or pairwise summation (NumPy does pairwise; Python has
math.fsum). - Compilers can't reorder float operations without "fast-math" flags, and parallel reductions on GPUs give slightly different results run to run.
Comparing floats
import math
math.isclose(a, b, rel_tol=1e-9, abs_tol=1e-12)Use a relative tolerance for large values and an absolute one near zero. A fixed abs(a - b) < 1e-9 is wrong for numbers around 10¹².
Floats in machine learning
Training and inference increasingly use 16-bit and even 8-bit floats (fp16, bfloat16, fp8) to halve memory and double throughput. bfloat16 keeps float32's 8-bit exponent (the same range) and sacrifices precision, which deep learning tolerates well. Mixed precision keeps a float32 "master" copy of the weights for accumulation.
- A float is sign × 1.fraction × 2^(exponent − bias); the leading 1 is implicit.
- Doubles give ~16 significant digits; spacing grows with magnitude.
- Special values: ±0, subnormals, ±∞, NaN (never equal to itself).
- Each operation is correctly rounded (ties to even), but errors accumulate; addition isn't associative.
- Compare with relative and absolute tolerances; use decimal or integer types for money.
QWhy is 0.1 + 0.2 != 0.3?easy
0.1, 0.2 and 0.3 all have infinite binary expansions, so each is stored rounded. The rounded 0.1 plus the rounded 0.2 rounds to a different double than the rounded 0.3 (they differ in the last bit).
QWhen would you use float instead of double?medium
When memory or bandwidth dominates and ~7 digits are enough: graphics, ML weights, large arrays, GPU work. SIMD processes twice as many floats per instruction. Use double by default for general numeric code.