Mathematical Foundations

VIII.1 · Numerical Computing

Which bits determine a machine number?

Edit a sign, exponent or fraction bit and inspect the value it creates. Compare four binary formats through exact stored rationals, neighbors and spacing before using their rounded displays.

Inspect and edit the stored bits

Interchange format (software rounding)

Sign (s)

Exponent (5 bits)

Fraction (10 bits; normal values have an implicit leading 1)

FP16 / binary16: normal, sign 0; Software-emulated FP16; not actual hardware operations. Direct nearest rounding, ties to even; all four formats retain subnormals. A formatted decimal is distinct from the bits and exact rational.

Stored classification
normal
Sign bit
0
Encoded exponent
11
Encoded fraction integer
614
Exact stored rational
819/8192
Formatted stored display
0.0999755859375
Spacing in the stored binade
1/16384
Exact signed representation error
-1/40960
Absolute representation error
1/40960
Relative representation error (nonzero input)
1/4096

= exact rationals or classifications · ≈ a formatted numerical display. Exceptional values retain their stated meaning.

x=(−1)s(1+F 2−p) 2E−b,0<E<2e−1x=(-1)^s(1+F\,2^{-p})\,2^{E-b},\quad 0<E<2^e-1
xsub=(−1)sF 21−b−p,E=0x_{\mathrm{sub}}=(-1)^sF\,2^{1-b-p},\quad E=0
Ea=∣x^−x∣,Er=Ea/∣x∣ (x≠0)E_a=|\widehat x-x|,\quad E_r=E_a/|x|\ (x\ne0)
Adjacent representable values; binade boundaries can have asymmetric gaps.
neighborexact valueformatted display
previous1637/16384≈ 0.09991455078125
stored819/8192≈ 0.0999755859375
next1639/16384≈ 0.10003662109375

Read the intended input

Decimal text enters as an exact rational; signed zero and special values retain their classification.

Inspect the recorded values\text{Inspect the recorded values}
Actual intermediate values; = exact rationals, ≈ binary64 displays.
quantityvalue
input1/10

Exact decimal text, at most 420 characters; exponent in [-400,400].

Edit bits, select an exact decimal and compare four software-emulated formats. The neighbors and errors decode the same stored state.

Intuition: a movable binary ruler

A floating-point format is a finite set of points on the real number line, together with encodings for exceptional values. Around one, a format with more fraction bits offers more points. Far from zero, its ruler stretches: the gap between adjacent normal values grows with the exponent. The ruler is therefore not a uniform decimal grid.

Start with FP16 and 0.1. Change the final fraction bit, then the first exponent bit. Both actions change the same stored bit pattern displayed in the inspector. The first usually makes a local precision change; the second can change the scale dramatically. The intended decimal remains visible as the comparison target. Reset returns to direct rounding of that target.

A sign bit distinguishes positive and negative values. Zero has two encodings, +0 and −0. They compare equal numerically in ordinary arithmetic, but operations such as a reciprocal can observe their sign. The chapter deliberately retains −0, even though the book normally suppresses it in ordinary rounded readouts.

Definition: fields and their interpretation

Let e be the number of exponent bits, p the number of stored fraction bits, b the exponent bias, s the sign bit, E the unsigned exponent field and F the unsigned fraction field. For a normal finite value,

x=(−1)s(1+F2−p)2E−b,0<E<2e−1.x=(-1)^s(1+F2^{-p})2^{E-b},\qquad 0<E<2^e-1.

The leading one is implicit, so normal precision is p+1 binary digits. With E=0 the implicit leading digit is zero:

x=(−1)sF21−b−p.x=(-1)^sF2^{1-b-p}.

Nonzero values in this second case are subnormal. F=0 produces signed zero. When E is all ones, F=0 encodes signed infinity and F≠0 encodes NaN. A NaN has no ordered real value and no real-number representation error. This explorer uses one canonical quiet NaN for direct decimal input; editing bits can reveal other payloads without claiming that their payload semantics are portable.

Mechanics: compare range and precision

All four formats here use a sign bit, but their budgets differ.

FormatExponent bitsFraction bitsNormal precisionNormal exponent range
binary64115253−1022 to 1023
binary3282324−126 to 127
FP16 / binary1651011−14 to 15
BF16 / bfloat16878−126 to 127

BF16 spends more of its 16 bits on exponent range; FP16 spends more on local precision. BF16 and binary32 share exponent field sizes but do not share identical maximum finite values, minimum subnormals or accuracy. Neither 16-bit format is a smaller binary32 with unchanged numerical behavior.

The bit explorer emulates all format conversions in software. JavaScript evaluates the later arithmetic examples in binary64. A selected FP16 or BF16 conversion is software emulation, not evidence about the physical CPU, GPU, accelerator, runtime, or its handling of subnormals.

Derivation: spacing and one rounding decision

Incrementing F within one normal binade increases x by

ulp⁡(x)=2E−b−p.\operatorname{ulp}(x)=2^{E-b-p}.

At an exact power of two, the previous binade has half the forward spacing. The neighbor table therefore shows the actual neighbors rather than assuming symmetric gaps. All subnormals have constant spacing 21−b−p2^{1-b-p}. Their relative precision declines as they approach zero.

Direct conversion first represents the intended decimal as a rational, locates the binary scale, then rounds its scaled significand to the nearest integer. If the exact value is halfway, choose the even integer. A carry can reach the next binade, turn the largest subnormal into the smallest normal, or overflow the finite range. No binary64 or binary32 intermediate is used to decide a low-precision conversion.

Worked example: a halfway decision

Near one, FP16 spacing is 2−10=1/10242^{-10}=1/1024. The decimal 1.00048828125 is exactly 1+2−111+2^{-11}, halfway between 1 and 1.0009765625. The first has an even trailing significand bit, so ties-to-even chooses 1.

The decimal 1.000488281250000000000000000001 lies strictly above that midpoint. Direct FP16 rounding chooses 1.0009765625. Converting this text to binary64 first can erase the tiny excess, after which a second rounding incorrectly chooses 1 for a claimed direct conversion. Use the above-halfway preset to see that double-rounding trap.

For 0.1, the intended exact value is 1/101/10. Its binary64 encoding instead stores 3602879701896397/360287970189639683602879701896397/36028797018963968. The neighboring values, its encoded fields and its exact error all come from that same bit pattern.

Worked example: Inspect and edit the stored bits

Sign (s)

0

Exponent (5 bits)

01011

Fraction (10 bits; normal values have an implicit leading 1)

1001100110

FP16 / binary16: normal, sign 0; Software-emulated FP16; not actual hardware operations. Direct nearest rounding, ties to even; all four formats retain subnormals. A formatted decimal is distinct from the bits and exact rational.

Stored classification
normal
Sign bit
0
Encoded exponent
11
Encoded fraction integer
614
Exact stored rational
819/8192
Formatted stored display
0.0999755859375
Spacing in the stored binade
1/16384
Exact signed representation error
-1/40960
Absolute representation error
1/40960
Relative representation error (nonzero input)
1/4096

= exact rationals or classifications · ≈ a formatted numerical display. Exceptional values retain their stated meaning.

x=(−1)s(1+F 2−p) 2E−b,0<E<2e−1x=(-1)^s(1+F\,2^{-p})\,2^{E-b},\quad 0<E<2^e-1
xsub=(−1)sF 21−b−p,E=0x_{\mathrm{sub}}=(-1)^sF\,2^{1-b-p},\quad E=0
Ea=∣x^−x∣,Er=Ea/∣x∣ (x≠0)E_a=|\widehat x-x|,\quad E_r=E_a/|x|\ (x\ne0)
Adjacent representable values; binade boundaries can have asymmetric gaps.
neighborexact valueformatted display
previous1637/16384≈ 0.09991455078125
stored819/8192≈ 0.0999755859375
next1639/16384≈ 0.10003662109375

Read the intended input

Decimal text enters as an exact rational; signed zero and special values retain their classification.

Inspect the recorded values\text{Inspect the recorded values}
Actual intermediate values; = exact rationals, ≈ binary64 displays.
quantityvalue
input1/10

Select the recorded intermediate values. Previous, next and direct replay navigation preserve immutable numerical history.

Executable reference code

The pedagogical program reads decimal strings through Fraction and rounds only once to the requested format. The NumPy program inspects the actual array bits of ordinary binary64, binary32 and binary16 inputs; it explicitly keeps BF16 emulated. NumPy’s dtype conversion provides a comparison for these ordinary inputs; use the Fraction-based program for the direct-decimal halfway counterexample. Keep the files in the same directory, because later examples import the first program.

code/python/floating_point_representation.py
"""Direct decimal → emulated binary formats: nearest, ties to even; no float intermediate."""
from fractions import Fraction
FORMATS = {'binary64': (11, 52, 1023), 'binary32': (8, 23, 127),
           'fp16': (5, 10, 15), 'bf16': (8, 7, 127)}
def power2(e):
    return Fraction(2**e) if e >= 0 else Fraction(1, 2**-e)
def convert(text, name):
    e, p, bias = FORMATS[name]
    raw = text.strip(); sign = raw.startswith('-'); all_exponents = 2**e-1
    if raw.lower().lstrip('+-') == 'infinity':
        return (int(sign) << (e+p)) | (all_exponents << p)
    if raw.lower() == 'nan':
        return (all_exponents << p) | (1 << (p-1))
    x = abs(Fraction(raw)); prefix = int(sign) << (e+p)
    if x == 0:
        return prefix
    exponent = x.numerator.bit_length() - x.denominator.bit_length()
    if x < power2(exponent):
        exponent -= 1
    exponent = max(exponent, 1-bias)
    if exponent > bias:
        return prefix | (all_exponents << p)
    scaled = x / power2(exponent-p)
    significand = round(scaled)  # Fraction.__round__: nearest, ties even.
    if significand == 2**(p+1):
        significand //= 2; exponent += 1
    if exponent > bias:
        return prefix | (all_exponents << p)
    normal = significand >= 2**p
    return prefix | ((exponent+bias if normal else 0) << p) | (significand-2**p if normal else significand)
def exact(bits, name):
    e, p, bias = FORMATS[name]
    sign = bits >> (e+p); exponent = (bits >> p) & (2**e-1); fraction = bits & (2**p-1)
    if exponent == 2**e-1:
        return None  # infinity or NaN has no rational value.
    return (-1 if sign else 1) * (fraction + (2**p if exponent else 0)) * power2((exponent-bias if exponent else 1-bias)-p)
if __name__ == '__main__':
    assert exact(convert('0.1', 'binary64'), 'binary64') == Fraction(3602879701896397, 36028797018963968)
    assert convert('1.00048828125', 'fp16') == convert('1', 'fp16')
    assert convert('1.000488281250000000000000000001', 'fp16') != convert('1', 'fp16')
    assert convert('-0', 'fp16') == 32768
    for name in FORMATS:
        bits = convert('0.1', name); print(name, 'emulated bits', hex(bits), 'exact', exact(bits, name))
code/python/floating_point_representation_numpy.py
"""NumPy native dtypes for ordinary inputs; BF16 remains explicitly software emulated."""
import numpy as np
from floating_point_representation import convert, exact
if __name__ == '__main__':
    for dtype, name in [(np.float64, 'binary64'), (np.float32, 'binary32'), (np.float16, 'fp16')]:
        a = np.array([0.1, -0.0, np.inf, np.nan], dtype=dtype)
        unsigned = {8: np.uint64, 4: np.uint32, 2: np.uint16}[a.dtype.itemsize]
        bits = a.view(unsigned)
        assert int(bits[0]) == convert('0.1', name)
        assert np.signbit(a[1]) and np.isinf(a[2]) and np.isnan(a[3])
        print(name, a, bits)
    print('BF16 software emulation:', exact(convert('0.1', 'bf16'), 'bf16'))
    # Decimal → low precision is handled by convert, not by a binary64 intermediate.

Explore: find the edges of a format

Select subnormal, negative zero, infinity and NaN. Explain which exponent/fraction patterns distinguish them. Change the format before changing the decimal: the same exact decimal can become normal, subnormal or zero. For a normal value at one, flip a low fraction bit and inspect the neighbor spacing.

Type the halfway and just-above-halfway values yourself. The entry is exact decimal text, with a bounded optional decimal exponent, so the input widget does not round away your evidence. Invalid syntax or oversized input is rejected with a message. Bit edits intentionally alter the encoding; applying new decimal text returns to direct conversion.

Common mistakes and counterexamples

A display reading 0.1 does not certify that the stored rational is 1/10. More exponent bits expand scale coverage; they do not supply more fraction precision. A subnormal is a finite nonzero value, not a NaN and not automatically zero.

Infinity can encode an explicitly entered infinity or a finite input that overflowed. Those origins have different mathematical meanings. A NaN is not a very large number: real ordering, ordinary real error and a real neighbor are undefined.

Numerical considerations

This model implements nearest, ties-to-even with gradual underflow. Hardware can use different modes or flush subnormals; that would be a different numerical policy. The emulation is not a timing benchmark and does not assert physical hardware support.

Compare bit patterns and rational errors exactly. An independent way to locate the nearest representable value is to search ordered bit patterns, rather than round a scaled significand. Include ties, signs, subnormal boundaries, overflow and special values when comparing conversion methods. A displayed decimal is rounded only for reading; it never drives the bit model.

Engineering and ML connections

Tensor storage formats trade range, precision and memory. Activations with a broad scale distribution can demand exponent range, while closely spaced parameter values demand significand precision. Mixed precision adds decisions about accumulation and conversion points. First identify which values are stored and which operations run in which format; the format name alone does not describe an entire training algorithm.

Inspect the corresponding representation and error policy before interpreting a computed value.

Exercises

Predict first, then calculate or test the stated claim. Every answer includes corrective feedback and a worked solution.

calculation

FP16 spacing

At x=1, FP16 has 10 fraction bits. Give the spacing as an exact fraction.

calculation

Normal significand

Use s=1, E−b=2 and a normal significand 1.5. What exact value is encoded?

calculation

Halfway at one

Round 1+1/2048 directly to FP16, nearest/ties-even.

explanation

Trade range for precision

Which comparison between the two 16-bit formats is correct?

Choose one answer

explanation

Exceptional meaning

What does the all-ones exponent with nonzero fraction encode?

Choose one answer

Summary and checkpoints

Read sign, exponent and fraction as one encoding. Explain normal versus subnormal spacing, preserve signed-zero and exceptional-value meanings, and compare FP16’s local precision with BF16’s scale range. Reproduce the halfway decision and identify why a binary64 intermediate can invalidate a direct-conversion claim.

The map of the book

97 of 97 chapters written · eight Parts

demo Foundation demo Try the live vector example Open as a page

Part I

Mathematical Language

The notation and mental objects everything else uses.

9 of 9 chapters written

  1. I.1 Scalars, quantities, units, powers and roots available
  2. I.2 Coordinates and points available
  3. I.3 Vectors: components, magnitude, and direction reference chapter available
  4. I.4 Algebraic expressions, equations and inequalities available
  5. I.5 Functions as input → transformation → output available
  6. I.6 Essential function families available
  7. I.7 Sets, intervals, and relations available
  8. I.8 Indexed quantities and summation available
  9. I.9 The notation map for ML available

Part II

Linear Algebra

Matrices as transformations: the strongest foundation in the book.

19 of 19 chapters written

  1. II.1 Vector addition and subtraction available
  2. II.2 Scalar multiplication and vector scaling available
  3. II.3 Norms, magnitude, and normalization available
  4. II.4 Dot product, angle, and orthogonality available
  5. II.5 Projection and distance to a direction available
  6. II.6 Linear combinations and span available
  7. II.7 Linear independence, basis, and coordinates available
  8. II.8 Matrices as linear transformations available
  9. II.9 Matrix × vector available
  10. II.10 Matrix × matrix as composition available
  11. II.11 Transpose and orthogonality available
  12. II.12 Determinant: orientation and area/volume scaling available
  13. II.13 Linear systems as intersecting constraints available
  14. II.14 Gaussian elimination and row operations available
  15. II.15 Rank and invertibility available
  16. II.16 Inverse matrices available
  17. II.17 Eigenvalues and eigenvectors available
  18. II.18 Orthogonal bases and least-squares geometry available
  19. II.19 Singular Value Decomposition available

Part III

Calculus

Change, local approximation, and sensitivity made visible.

15 of 15 chapters written

  1. III.1 Limits through approach available
  2. III.2 Continuity and local behavior available
  3. III.3 Derivative as secant → tangent available
  4. III.4 Derivative as rate of change and local linear model available
  5. III.5 Derivative rules available
  6. III.6 Chain rule available
  7. III.7 Multivariable functions and partial derivatives available
  8. III.8 Gradient and directional derivatives available
  9. III.9 Jacobian available
  10. III.10 Computational graphs and automatic differentiation available
  11. III.11 Hessian and curvature available
  12. III.12 Integrals as accumulation available
  13. III.13 Fundamental derivative/integral connection available
  14. III.14 Numerical differentiation available
  15. III.15 Numerical integration available

Part IV

Probability

Uncertainty through sample spaces, evidence, and repetition.

16 of 16 chapters written

  1. IV.1 Random experiments, outcomes, and sample spaces available
  2. IV.2 Events and probability mass available
  3. IV.3 Counting and finite probability where needed available
  4. IV.4 Conditional probability as restricted sample space available
  5. IV.5 Independence available
  6. IV.6 Bayes' theorem as probability-mass update available
  7. IV.7 Random variables available
  8. IV.8 PMF, PDF, and CDF available
  9. IV.9 Bernoulli and Binomial distributions available
  10. IV.10 Uniform and Gaussian distributions available
  11. IV.11 Expectation available
  12. IV.12 Variance and standard deviation available
  13. IV.13 Joint, marginal, and conditional distributions available
  14. IV.14 Covariance and correlation available
  15. IV.15 Law of Large Numbers available
  16. IV.16 Central Limit Theorem available

Part V

Statistics

From observed data to estimates, intervals, and likelihood.

10 of 10 chapters written

  1. V.1 Data summaries: mean, median, mode available
  2. V.2 Spread: variance, standard deviation, robust interpretation available
  3. V.3 Population, sample, and sampling variability available
  4. V.4 Estimators and estimation available
  5. V.5 Confidence intervals through repeated sampling available
  6. V.6 Covariance matrices and multivariate spread available
  7. V.7 Regression fundamentals available
  8. V.8 Likelihood available
  9. V.9 Maximum Likelihood Estimation available
  10. V.10 Maximum A Posteriori estimation available

Part VI

Optimization

Learning as repeated parameter updates governed by an objective.

10 of 10 chapters written

  1. VI.1 Objective functions and optimization landscapes available
  2. VI.2 Local vs global optima and stationary points available
  3. VI.3 Convexity available
  4. VI.4 Gradient descent flagship chapter available
  5. VI.5 Learning rate and convergence behavior available
  6. VI.6 Momentum available
  7. VI.7 Stochastic and mini-batch gradient descent available
  8. VI.8 Adam intuition available
  9. VI.9 Constraints and projected/penalized intuition available
  10. VI.10 Numerical stopping and optimization diagnostics available

Part VII

Information Theory

Logarithms, entropy, and the loss behind token prediction.

8 of 8 chapters written

  1. VII.1 Logarithms and log probabilities in computation available
  2. VII.2 Self-information available
  3. VII.3 Entropy available
  4. VII.4 Cross entropy and negative log-likelihood available
  5. VII.5 KL divergence available
  6. VII.6 Logits and softmax available
  7. VII.7 Numerical stability: log-sum-exp and stable softmax available
  8. VII.8 From token logits to language-model loss available

Part VIII

Numerical Computing

Why equivalent expressions behave differently on real machines.

10 of 10 chapters written

  1. VIII.1 Floating-point representation available
  2. VIII.2 Rounding and representation error available
  3. VIII.3 Error propagation and conditioning available
  4. VIII.4 Catastrophic cancellation available
  5. VIII.5 Overflow and underflow available
  6. VIII.6 Stable algorithms available
  7. VIII.7 Numerical linear algebra realities available
  8. VIII.8 Sparse matrices and sparse structure available
  9. VIII.9 Vectorized computation available
  10. VIII.10 Reproducibility, random seeds, and nondeterminism available