advanced29 min studyLesson 492 in Engineering

Cancellation can expose errors that happened before subtraction

Separate a sensitive small difference from its computation, then compare raw moments and centered statistics on an exactly specified batch.

On this page

What you’ll understand

  • Explain why nearly equal quantities can make a small difference sensitive to input perturbations.
  • Distinguish exact subtraction of rounded inputs from recovery of their lost information.
  • Compare raw-moment and centered variance calculations with an explicit reference.

Subtracting nearly equal quantities is often blamed for numerical trouble, but the subtraction itself can be exact for the represented operands. The deeper issue is that earlier rounding may already have perturbed those operands by amounts large relative to their small difference. Learning computations encounter this in variance, normalization, loss differences, and residual updates. Diagnose both the conditioning of the question and the stability of the formula before choosing a larger dtype or adding an arbitrary epsilon.

A small answer magnifies relative input uncertainty

For a nonzero difference a minus b, independent relative perturbations to a and b can produce an absolute output perturbation proportional to their magnitudes. When the true difference is tiny compared with those magnitudes, the relative output change can be large. This is a property of the problem's input-to-output relationship. It does not imply that every implementation must fail or that every subtract instruction rounds badly. At an exact zero result, use absolute error or another meaningful scale because relative error against zero is undefined.

A reformulation can avoid creating unnecessarily inaccurate large intermediate values. Centering data before accumulating squared deviations often avoids subtracting two large raw moments. Yet input information already lost to a low-precision cast cannot be restored by a stable formula afterward. Likewise, adding a small constant to a denominator changes the mathematical function and can be a deliberate model choice, but it is not a proof that the original computation became accurate. Track input representation, intermediate precision, and the intended statistical definition separately.

κdiff=∣a∣+∣b∣∣a−b∣(a≠b),∣Δ(a−b)∣≤∣Δa∣+∣Δb∣\kappa_{\rm diff}=\frac{|a|+|b|}{|a-b|}\quad(a\ne b),\qquad |\Delta(a-b)|\le|\Delta a|+|\Delta b|
For componentwise relative input perturbations, kappa_diff describes worst-case relative sensitivity of a nonzero difference. The absolute bound is valid without dividing by that small result. a and b must have compatible units; sensitivity is not a claim about one particular rounding instruction.↔ Scroll wide equations horizontally.

Variance has equivalent formulas with different intermediate risks

Population variance can be written as mean of squares minus square of the mean, or as the mean squared centered deviation. These formulas are algebraically equivalent in exact arithmetic. The raw-moment form can subtract large rounded quantities to obtain a small variance, while a centered or carefully designed online method can keep intermediate values closer to the scale of the desired answer. The chosen accumulation and mean precision still matter. A centered calculation with a badly rounded mean or mismatched normalization can remain inaccurate.

Distinguish population variance from an unbiased sample-variance estimator with another denominator. An experiment comparing formulas must compute the same mathematical quantity on the same represented inputs. Test a batch with a large common offset and small but representable deviations, then compare with a wider or exact reference. Check for negative computed variance before a square root, but do not simply clamp and declare success: clamping can hide a real error source. For normalization layers, examine the downstream output and gradient as well as the intermediate variance.

v=1N∑ixi2−μ2=1N∑i(xi−μ)2,μ=1N∑ixiv=\frac1N\sum_i x_i^2-\mu^2=\frac1N\sum_i(x_i-\mu)^2,\qquad \mu=\frac1N\sum_i x_i
These exact identities define population variance for a finite real batch with N positive. v has squared input units. Finite-precision implementations differ because mean, products, sums, and subtraction can round separately; neither identity alone specifies a stable algorithm.↔ Scroll wide equations horizontally.

Separate conditioning from an algorithm's observed error

Measure absolute and scaled errors against a defined reference, inspect the sizes of intermediate operands, and change one representation choice at a time. If widening inputs before a problematic operation helps, locate where the earlier path discarded useful information. If a mathematically sensitive problem remains sensitive in wider arithmetic, that is evidence for conditioning rather than permission to blame a particular accelerator. Reference precision itself needs checking near exceptional values, so use exact small fixtures where possible and increase reference precision to see whether the answer stabilizes.

The matrix lab offers a related sensitivity example, not a variance or cancellation simulation. Reset, set both diagonal scale a=1 and shear b=0, predict condition number one, then Play or Step. Reset and re-enter both a=0.25 and b=0, predict condition number four. Its supported family is only A=[[a,b],[0,1]] with positive a, and readouts describe the selected final matrix while playback interpolates geometry. This illustrates sensitivity changing with a problem, but does not evaluate batch variance, emulate binary16, or measure rounding error.

Rounded moments lose a representable batch variance

Two exactly represented inputs are 32 and 32.03125. Define population variance. First calculate moments exactly, then round each moment E[x²] and μ² separately to binary16 before subtracting. Compare with centered deviations using an exact or sufficiently wide mean.

  1. The exact mean is 32.015625. Deviations are ±0.015625, so exact population variance is 0.000244140625, or 2⁻¹².

  2. The exact moments are E[x²]=1,025.00048828125 and μ²=1,025.000244140625. Binary16 spacing there is one, so both round to 1,025 and their represented subtraction gives zero.

  3. Using the exact mean, each centered squared deviation is 2⁻¹², representable in binary16. The difference between formulas arose from separately rounded large moments; widening only the final subtraction cannot reconstruct their discarded difference.

The result

The zero raw-moment result is not proof the batch is constant. The inputs and desired variance were representable, while the chosen intermediate representation erased the difference.

Explore the model

Interactive lab

Interactive lab · matrix

Sensitivity belongs to the represented problem

Reset, set both a=1 and b=0, predict condition one, then Play or Step. Reset and re-enter a=0.25 and b=0 for condition four. The fixed triangular two-dimensional family stays invertible; readouts describe the selected final matrix during geometric interpolation. It does not compute variance, simulate cancellation, or measure floating-point error.

Sensitivity belongs to the represented problemThe selected matrix is A = [[a,b],[0,1]]. Playback interpolates geometry between the identity and A; readouts always describe A. Its determinant is a and its singular values come from AᵀA. Positive a makes every supported matrix invertible. This two-dimensional family is not a general eigensolver or a learned network. Current readings: Area scale · det A: 1.4; Singular values · σmax, σmin: 1.597, 0.876; Condition number · κ₂(A): 1.823. All parameters are adjustable with the labeled controls below.Matrix A with first row 1.4, 0.6 and second row zero, one; display interpolates between identity and A while final determinant remains 1.4.−4−2024−2−1012x · equal geometric scaleyA = [ 1.4 0.6 ; 0 1 ]Display blend: 0% · final values below
Interpolated geometryOriginal unit squareSelected final transform
Area scale · det A
1.4
Singular values · σmax, σmin
1.597, 0.876
Condition number · κ₂(A)
1.823
t = 0 s

The selected matrix is A = [[a,b],[0,1]]. Playback interpolates geometry between the identity and A; readouts always describe A. Its determinant is a and its singular values come from AᵀA. Positive a makes every supported matrix invertible. This two-dimensional family is not a general eigensolver or a learned network. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forA sensitive input-to-output relationship and an unstable implementation are different diagnoses that can interact.

Check your understanding

Two large exact moments are rounded to the same stored number before subtraction. What can a wider subtraction of those stored numbers recover?

Try it yourself

Two compatible statistics are a=100.25 and b=100.125. Each can have an independent absolute perturbation of at most 0.01. Bound the absolute perturbation of their difference and its relative size against the exact nonzero difference.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Add the two absolute uncertainty bounds, then divide by a−b rather than by a.

Compare with the solution

The exact difference is 0.125. Its absolute perturbation is at most 0.01 + 0.01 = 0.02, giving a relative bound 0.02/0.125 = 0.16, or sixteen percent. This is an input-sensitivity bound under the stated perturbations, not a claim that every floating-point subtraction incurs that error.

Take it with you

  • Cancellation can amplify previous operand errors even when the represented subtraction is exact.
  • Stable reformulations and earlier precision choices address different sources of lost information.
  • Use nonzero-denominator sensitivity measures carefully and inspect downstream learning behavior.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.