Floating-point arithmetic and stable formulas
Track representation, rounding, cancellation, and reformulation instead of assuming algebraic equality implies numerical equality.
On this page
The question for today
What you’ll understand
- Use the standard rounding model within its stated range.
- Identify avoidable cancellation in an otherwise well-conditioned expression.
- Explain how summation order and scaling affect a computation.
A floating-point number is a representable point on a nonuniform grid. Arithmetic normally rounds an exact intermediate result back onto that grid. The practical consequence is deeper than a few incorrect final digits: algebraically equivalent formulas can preserve very different amounts of information. This lesson follows one expression through rounding, then uses reformulation, scale, and residual checks to turn a vulnerable computation into a defensible one. We will distinguish a simplified teaching arithmetic from the behavior of any particular hardware implementation.
A relative model with boundaries
In a radix-based format, a finite normal number stores a sign, a limited significand, and an exponent. Near larger values, the absolute gap between representable numbers is larger. Under round-to-nearest, a basic operation is often modeled as its exact real result multiplied by one plus a small relative perturbation. Unit roundoff bounds that perturbation for normal results under the usual assumptions. It is an analysis tool, not an assertion that errors are random or independent.
Overflow, underflow, subnormal results, division by zero, and nonfinite inputs need separate handling. Relative-error bounds can fail near zero even though an absolute-error bound still makes sense. In common binary64 round-to-nearest arithmetic, unit roundoff is 2⁻⁵³, while the gap from one to the next larger representable number is 2⁻⁵². These quantities are often both informally called epsilon, so state the convention. Decimal display length does not reveal how many trustworthy digits survived the calculation.
Cancellation exposes earlier uncertainty
Subtracting nearby rounded quantities can leave an answer far smaller than either operand. The subtraction may itself be correctly rounded, even exact, while the earlier operand errors now dominate the answer. This distinction matters: cancellation is not proof that the final machine instruction malfunctioned. It is a warning that the chosen route to the answer carried uncertainties on a scale larger than the desired difference. Sometimes that sensitivity is inherent in measured inputs; sometimes algebra can avoid it.
Consider the difference between the square root of one plus a small positive x and one. Multiplying numerator and denominator by the conjugate gives an equal expression with no subtraction of nearby unit-sized numbers. The small quantity x is retained explicitly. This reformulation addresses algorithmic cancellation for the specified function; it does not improve genuinely uncertain input data. Related standard library functions such as log1p and expm1 exist because evaluating an elementary function first and subtracting afterward can discard useful information near zero.
Organize the whole computation
Floating-point addition is not associative. Adding many small terms to a large running total may lose contributions that would survive if grouped first. Pairwise summation reduces the depth over which rounding errors accumulate, while compensated methods track information lost during additions. Neither approach promises exact results for arbitrary ranges and cancellation patterns. A sum with a tiny true value compared with the sum of absolute terms is itself relatively sensitive, so algorithm choice and conditioning must be assessed together.
Scaling can prevent overflow in intermediate expressions whose final answer is representable. For example, robust norm routines avoid blindly squaring every large component before summing. Reproducibility across parallel execution orders also requires a deliberate reduction strategy, since changing grouping can change rounded results. Validate against an analytically tractable case, a higher-precision reference, or a backward-error estimate. A result that is unchanged after printing fewer decimals has not passed an accuracy test; the comparison must target the underlying quantity and its allowed error.
Work it through
The difference that rounding erased
Use a teaching model with five significant decimal digits and round-to-nearest after each operation. Evaluate sqrt(1+x)−1 for x=0.0001.
The addition gives 1.0001 exactly in this model. Its exact square root is approximately 1.00004999875, which rounds to 1.0000 at five significant digits.
The direct subtraction therefore returns zero, although the exact answer is about 0.00004999875. All useful relative information about this nonzero difference has disappeared.
Use the conjugate expression instead. The rounded denominator is 1.0000+1.0000=2.0000, and division gives 0.0001/2.0000=0.000050000, representable as 5.0000×10⁻⁵.
The reformulated absolute error is about 1.25×10⁻⁹ and its relative error about 2.5×10⁻⁵. This is a major improvement, despite using exactly the same precision. Actual binary64 rounding will have different intermediate values.
A stable algebraic route preserves the small numerator explicitly. Increasing displayed digits would not repair the zero returned by the direct route.
The living notebook
Explore the model
Interactive lab · function
Scale changes the answer, not arithmetic precision
Set the quadratic coefficient a near zero and vary position x. Compare the function value with its tangent slope: they shrink at different rates with x. The lab evaluates y=a x², its exact-form tangent, and its area; it has no precision control and does not simulate rounding, cancellation, or the square-root example.
- y at chosen x
- 0.608
- Tangent slope · 2ax
- 1.35
- Signed area · ax³/3
- 0.182
This lab uses only y = ax². Playback traces the curve within the fixed integration interval from 0 to x; it does not estimate numerical error or change the chosen point. 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 forUse this scale analogy to choose sensible absolute tolerances, then analyze rounding separately.
Pause & reason
Check your understanding
Your turn
Try it yourself
Rewrite to avoid subtracting nearby numbers for small x. Estimate the answer at x=0.001 radians.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Use a half-angle identity before making a small-angle approximation.
Compare with the solution
Use . At x=0.001, sin(0.0005)≈0.0005 gives an answer near 5×10⁻⁷. The identity avoids the final subtraction; the sine implementation and its argument still have finite precision. This is an approximation for the numerical value, while the identity itself is exact.
Keep the idea
Take it with you
- The rounding model has a domain; overflow and subnormal behavior need explicit treatment.
- Algebraically equal expressions can have very different rounding behavior.
- Summation order, scaling, and conditioning all affect reliable digits.
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.