Mathematical Foundations

VIII.6 · Numerical Computing

Which evaluation keeps the information you need?

Compare selected naive and stable numerical methods through their actual intermediate arithmetic. Explore scaled norms, small-input functions, compensated sums, shifted softmax and bounded loss scaling.

Replay selected stable numerical algorithms

Emulated gradient-storage format (scaling mode only)

hypot: naive ≈ ∞, stable ≈ 4.9999999999999995×10²⁰⁰. Exact intended inputs supply the real reference enclosure. A stable result still has finite-precision error. Replay shows intermediate values; the internal steps of built-in functions are not shown.

Selected method
hypot
Naive computed result
∞
Stable computed result
4.9999999999999995×10²⁰⁰
Mathematical reference (rounded enclosure midpoint)
5.0000000000000002×10²⁰⁰

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

m=max⁡(∣x∣,∣y∣),∥v∥2=m(x/m)2+(y/m)2 (m>0)m=\max(|x|,|y|),\quad\|v\|_2=m\sqrt{(x/m)^2+(y/m)^2}\ (m>0)
Exact intended inputs and their represented binary64 values.
zero-based indexintended exact valuestored binary64
0300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000≈ 2.9999999999999999×10²⁰⁰
1400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000≈ 3.9999999999999999×10²⁰⁰
Exact rational enclosure and signed computed-minus-real error bounds.
quantitylower boundupper bound
true result500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
naive errorundefinedundefined
stable error-49126270434017913486396393581630480335852750779778308842586654467154846139071012275966262582894804870768006408447934672558984152404537063932854160685727640615349292266934330373756157952-49126270434017913486396393581630480335852750779778308842586654467154846139071012275966262582894804870768006408447934672558984152404537063932854160685727640615349292266934330373756157952

Naive squares

The individual products may overflow or underflow before their finite mathematical norm is formed.

Inspect the recorded values\text{Inspect the recorded values}
Actual intermediate values; = exact rationals, ≈ binary64 displays.
quantityvalue
x≈ 2.9999999999999999×10²⁰⁰
y≈ 3.9999999999999999×10²⁰⁰
xx≈ ∞
yy≈ ∞
sum≈ ∞
result≈ ∞

At most 12 entries within ±1e300; norm needs two, small-input functions one with |x| ≤ 0.1, softmax at most six logits within ±2000.

from 0 to 24, whole numbers only

Choose a bounded method and replay the naive and stable arithmetic. FP16/BF16 storage and loss scaling are explicitly software emulations.

Intuition: order the arithmetic around its range

A correct real formula can ask a computer to form an unnecessary enormous or tiny intermediate. Squaring two large components can overflow even when their norm is representable. Adding a tiny x to one can erase x before a logarithm sees it. A sum can discard a small term before a later large cancellation makes that term important.

The stable method changes the sequence of operations so the needed information has a better chance of surviving. It still uses finite arithmetic. Compare the actual intermediate values, the intended mathematical reference where available and the remaining error; “stable” is not a promise of exactness.

Definition: the supported comparisons

The explorer deliberately supports a small collection: a two-component norm, log1p(x), expm1(x), Neumaier compensated summation, shifted softmax/log-sum-exp, and a software loss-scaling storage simulation. Lists contain at most 12 entries, softmax at most six logits within ±2000, small-input functions require |x|≤0.1, and magnitude bounds prevent unrestricted numerical workloads.

For a norm, let m=max(|x|,|y|). If m=0, return zero explicitly; otherwise compute

x2+y2=m(x/m)2+(y/m)2.\sqrt{x^2+y^2}=m\sqrt{(x/m)^2+(y/m)^2}.

The scaled ratios are bounded by one. Specialized log1p and expm1 primitives evaluate log⁡(1+x)\log(1+x) and ex−1e^x-1 without first exposing their small difference to the naive one-plus-x or exp-minus-one sequence.

Mechanics: observable values and honest references

The norm replay exposes both raw squares and the scaled ratios, scaled square sum, root and restored result. The summation replay records each input, previous accumulator, tentative sum, correction and running compensation. These are actual binary64 operations, not an ideal arithmetic animation.

For log1p/expm1, the replay shows the naive observable intermediate and the specialized primitive’s input/result. The runtime’s internal implementation is platform-dependent; the lesson does not invent internal operations for it. Independent mathematical references use exact rational series bounds, checked by high-precision Python logarithms and exponentials.

Softmax shows shifted logits, weights, their common sum, probabilities and log probabilities. It connects conceptually to Part VII without importing its independently developed code.

Derivation: retain corrections and shift a common scale

For Neumaier summation with accumulator s and next input x, form t=s+x. If |s|≥|x|, add (s−t)+x(s-t)+x to the correction; otherwise add (x−t)+s(x-t)+s. Replace s by t and finally return s+c. The two branches choose the larger-magnitude operand when recovering the local rounding loss. The compensation itself still rounds.

For logits z and m=max z,

LSE⁡(z)=m+log⁡∑iezi−m,pi=ezi−m∑jezj−m.\operatorname{LSE}(z)=m+\log\sum_i e^{z_i-m},\qquad p_i=\frac{e^{z_i-m}}{\sum_j e^{z_j-m}}.

The shifted exponents are nonpositive, so their weights do not overflow. Compute log probabilities as (zi−m)−log⁡∑jezj−m(z_i-m)-\log\sum_j e^{z_j-m} directly. A tiny positive weight can still underflow; its finite log probability need not be lost with the probability display.

For the small-input functions, 48 exact series terms and a documented remainder bound enclose the real result. The log series remainder is bounded by the next absolute term divided by 1−|x|. For expm1, twice the next absolute term bounds the remaining tail on |x|≤0.1. These reference calculations are not the production runtime implementation.

Worked examples: three recovered signals

With components 3e200 and 4e200, naive squares overflow, but scaling by 4e200 produces ratios 0.75 and 1. Their square sum is 1.5625, its root is 1.25, and the restored norm is near 5e200. A similarly scaled construction checks the very small case 3e-200, 4e-200, where naive squares underflow.

For the represented sequence [1e16,1,−1e16], ordinary left-to-right summation returns zero. The compensation retains the lost one and returns one. The exact sum of represented operands is one; exact intended-decimal sums are shown separately when conversion itself introduces a difference.

For logits [1000,1001,999], unshifted exponentials overflow. Shift by 1001 to get [−1,0,−2], bounded weights and a finite log-sum-exp. For [0,−1000], the tail probability may underflow to zero while its direct log probability stays near −1000. That numerical zero is not a mathematically zero probability.

Worked example: Replay selected stable numerical algorithms

hypot: naive ≈ ∞, stable ≈ 4.9999999999999995×10²⁰⁰. Exact intended inputs supply the real reference enclosure. A stable result still has finite-precision error. Replay shows intermediate values; the internal steps of built-in functions are not shown.

Selected method
hypot
Naive computed result
∞
Stable computed result
4.9999999999999995×10²⁰⁰
Mathematical reference (rounded enclosure midpoint)
5.0000000000000002×10²⁰⁰

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

m=max⁡(∣x∣,∣y∣),∥v∥2=m(x/m)2+(y/m)2 (m>0)m=\max(|x|,|y|),\quad\|v\|_2=m\sqrt{(x/m)^2+(y/m)^2}\ (m>0)
Exact intended inputs and their represented binary64 values.
zero-based indexintended exact valuestored binary64
0300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000≈ 2.9999999999999999×10²⁰⁰
1400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000≈ 3.9999999999999999×10²⁰⁰
Exact rational enclosure and signed computed-minus-real error bounds.
quantitylower boundupper bound
true result500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
naive errorundefinedundefined
stable error-49126270434017913486396393581630480335852750779778308842586654467154846139071012275966262582894804870768006408447934672558984152404537063932854160685727640615349292266934330373756157952-49126270434017913486396393581630480335852750779778308842586654467154846139071012275966262582894804870768006408447934672558984152404537063932854160685727640615349292266934330373756157952

Naive squares

The individual products may overflow or underflow before their finite mathematical norm is formed.

Inspect the recorded values\text{Inspect the recorded values}
Actual intermediate values; = exact rationals, ≈ binary64 displays.
quantityvalue
x≈ 2.9999999999999999×10²⁰⁰
y≈ 3.9999999999999999×10²⁰⁰
xx≈ ∞
yy≈ ∞
sum≈ ∞
result≈ ∞

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

Executable reference code

The pedagogical program cross-checks scaled norms, math.fsum, specialized elementary functions and shifted probabilities using Decimal and exact Fraction values. The NumPy version performs actual array operations and shows their numerical outcomes. Neither file promises identical elementary-function bits on arbitrary platforms. When comparing numerical results, use a small actual-value ULP allowance for approximate values and exact equality for classifications and rational quantities.

code/python/stable_algorithms.py
from decimal import Decimal, localcontext
from fractions import Fraction
from math import exp,expm1,fsum,hypot,log,log1p,sqrt
from floating_point_representation import convert,exact
if __name__ == '__main__':
    x,y=3e200,4e200;m=max(abs(x),abs(y))
    scaled=m*sqrt((x/m)**2+(y/m)**2)
    assert abs(scaled/5e200-1)<1e-15
    assert x*x==float('inf')
    values=[1e16,1.,-1e16]
    naive_sum=0.0
    for value in values:naive_sum+=value
    assert naive_sum==0 and fsum(values)==1
    assert log(1+1e-16)==0 and log1p(1e-16)>0
    assert exp(1e-16)-1==0 and expm1(1e-16)>0
    z=[1000.,1001.,999.];maximum=max(z);weights=[exp(v-maximum) for v in z];total=fsum(weights)
    probabilities=[v/total for v in weights];logs=[v-maximum-log(total) for v in z]
    assert abs(fsum(probabilities)-1)<1e-15
    with localcontext() as ctx:
        ctx.prec=100;x=Decimal('1e-16')
        assert abs(Decimal.from_float(log1p(float(x)))/(1+x).ln()-1)<Decimal('1e-15')
        assert abs(Decimal.from_float(expm1(float(x)))/(x.exp()-1)-1)<Decimal('1e-15')
    gradient=Fraction('1e-8');scale=2**16
    direct=exact(convert('1e-8','fp16'),'fp16')
    scaled_input=gradient*scale
    scaled_text=str(Decimal(scaled_input.numerator)/Decimal(scaled_input.denominator))
    stored=exact(convert(scaled_text,'fp16'),'fp16')
    assert direct==0 and stored>0
    assert convert('655360000','fp16')==0x7c00
    print('norm',scaled,'sum',fsum(values),'probabilities',probabilities,'log probabilities',logs,'emulated scaled gradient',stored/scale)
code/python/stable_algorithms_numpy.py
import numpy as np
from math import fsum
from decimal import Decimal,localcontext
if __name__ == '__main__':
    a=np.array([3e200,4e200]);m=np.max(np.abs(a))
    with np.errstate(over='ignore',under='ignore',invalid='ignore'):
        naive=np.sqrt(np.sum(a*a));scaled=m*np.sqrt(np.sum((a/m)**2))
        assert np.isinf(naive) and np.isfinite(scaled)
        x=np.array([1e-16,-1e-16]);assert np.all(np.log1p(x)!=0)
        z=np.array([1000.,1001.,999.]);shifted=z-z.max();weights=np.exp(shifted);p=weights/weights.sum();lp=shifted-np.log(weights.sum())
        assert abs(p.sum()-1)<1e-15
        g=np.float16(1e-8);scaled_g=np.float16(1e-8*2**16)
        assert g==0 and scaled_g!=0
    with localcontext() as ctx:
        ctx.prec=100
        for value,result in zip(x,np.log1p(x)):
            v=Decimal.from_float(float(value));reference=(1+v).ln()
            assert abs(Decimal.from_float(float(result))/reference-1)<Decimal('1e-15')
    print('norm',scaled,'compensated stdlib sum',fsum([1e16,1.,-1e16]),'probabilities',p,'logs',lp,'actual NumPy FP16 scaled/unscaled',scaled_g,float(scaled_g)/2**16)

Explore: mixed precision and a scaling boundary

Try the large and tiny norm presets, both small-input functions, the cancellation-prone sum and the overflowing softmax. Predict which naive intermediate fails before opening replay. Alter the comma-separated inputs while respecting each example’s stated bounds.

In loss-scaling mode, store the exact intended gradient directly in emulated FP16, then multiply it by 2k2^k before that storage and unscale the stored result in binary64. A gradient 1e-8 rounds to FP16 zero directly but can survive with k=16. A gradient 10000 with the same scale overflows instead. Change k and inspect both stored encodings.

This bounded simulation represents one storage conversion and one unscale. In real mixed-precision training, scaling a loss also scales its gradient by the chain rule, but intermediate gradient operations and accumulation choices matter. The simulation does not establish that an arbitrary training computation is correct.

Common mistakes and counterexamples

Compensation cannot restore an operand rounded away before the sum, and it cannot repair an accumulator already overflowed to infinity. Rescaling a norm avoids unnecessary square overflow here but cannot represent a true result beyond the output format’s range. Specialized log1p still requires its mathematical domain x>−1; this lesson uses a smaller bounded domain for its independent series reference.

A softmax probability displayed as zero can conceal a finite log probability. Taking log of the rounded probability would discard that information. A common seed, an algorithm name or the word “stable” does not supply a universal numerical proof.

Numerical considerations

Exact intended inputs and exact represented operands are distinct references. Irrational norm values and elementary functions use enclosures or independent high-precision checks; compensated sums have exact rational references. The observable runtime elementary results may differ by a few ULPs across environments.

The emulated FP16/BF16 scaling experiment uses direct nearest/ties-even conversion with subnormals. It is not actual low-precision hardware arithmetic. Scaling helps only if it keeps useful intermediates away from both ends of the range, and unscaling has its own final rounding. Validation retains overflowing and underflowing failure probes without widening budgets.

Engineering and ML connections

Norms, log probabilities, reductions and mixed-precision gradients recur throughout learning systems. Choose algorithms from the required input range and accuracy, record their conversion/accumulation points and check a meaningful mathematical reference. A performance claim requires an actual workload measurement; these bounded explorer calculations teach numerical mechanisms, not a timing contest.

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

A scaled norm

For x=3,y=4, scale m=4. Give x/m, then the exact norm.

calculation

A retained small term

For represented [1e16,1,−1e16], give ordinary left-to-right binary64 sum, then the exact sum.

calculation

Power-of-two scaling

A gradient is exactly 1/65536. Multiply by 2^4. Give the exact scaled value.

explanation

A stable tail

A shifted softmax probability underflows to zero. Which calculation preserves its finite log probability when available?

Choose one answer

explanation

Scaling limitation

Why does loss scaling require a bounded operating range?

Choose one answer

Summary and checkpoints

Replay a scaled norm, one compensated addition and one shifted softmax. Explain why log1p/expm1 preserve small changes, distinguish true from underflowed zero probabilities, and identify both a loss-scaling success and its overflow counterexample. Each improvement has a domain, range and remaining rounding policy.

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