Tutorials › Calculus II › Roundoff and Cancellation in Quadrature

Quadrature and Error Bounds · Tutorial 372 of 998

Roundoff and Cancellation in Quadrature

Learn to distinguish quadrature error from arithmetic error and to check whether a weighted sum is reliable.

Intermediate 12 min read

What You'll Learn

  • Identify cancellation among weighted quadrature contributions.
  • Use a condition number to assess sensitivity in a sum.
  • Explain how floating-point rounding can change a quadrature result.
  • Apply compensated summation to a weighted sum.
  • Check suspicious estimates by comparing arithmetic uncertainty with the reported difference.

Why arithmetic can affect a quadrature result

A quadrature rule replaces an integral with a weighted sum of function values. Even if the rule is appropriate and the values are available, a computer must still evaluate that sum using finite-precision arithmetic. Rounding can alter individual terms, and cancellation can make the final result much smaller than the terms being added.

These effects matter especially when the weighted contributions have different signs, or when a result is obtained by subtracting two similar quadrature estimates. A small reported change does not automatically indicate improved accuracy: it may be comparable to the arithmetic uncertainty in the estimates.

Definition: For a quadrature sum \(Q=\sum_{i=0}^{n}w_i f_i\), its weighted contributions are the terms \(q_i=w_i f_i\). Cancellation occurs when contributions with different signs nearly offset, leaving a sum small compared with the magnitudes of the individual contributions.

A floating-point model

A floating-point number stores only a finite number of significant binary digits. Under the standard rounding model, an arithmetic operation on floating-point numbers is represented as

$$ \operatorname{fl}(a\mathbin{\circ} b)=(a\mathbin{\circ} b)(1+\delta), \qquad |\delta|\leq u, $$

where \(\circ\) is an operation such as addition or multiplication, \(u\) is the unit roundoff, and \(\operatorname{fl}\) denotes the computed result. This model assumes the exact result does not overflow or fall into the subnormal range, and that the operation is rounded to the nearest representable number. For binary64 arithmetic with round-to-nearest, \(u=2^{-53}\). The model describes the rounding of an operation; it does not account for errors already present in the input values or in evaluations of the integrand.

A computed quadrature result can therefore be affected by several distinct sources: the quadrature rule's approximation error, errors in evaluating or measuring the function values, rounding in forming the weighted terms, and rounding while adding those terms. Improving the summation addresses only the last sources; it cannot remove the rule's approximation error.

How sensitive is a weighted sum?

A useful diagnostic for a nonzero sum \(S=\sum_{i=0}^{n}q_i\) is its summation condition number

$$ \kappa_{\mathrm{sum}} = \frac{\sum_{i=0}^{n}|q_i|}{|S|}. $$

If all contributions have the same sign, the numerator and denominator are similar, so the condition number is near one. If large positive and negative contributions nearly cancel, the numerator can be much larger than the denominator. Then small relative changes in the contributions can produce a large relative change in the sum. If \(S=0\), this relative condition number is undefined; the sum may still be assessed using an absolute error bound.

For sequential floating-point addition of \(n+1\) terms, under the model above and provided \(nu<1\), a standard bound is

$$ |\widehat S-S| \leq \gamma_n \sum_{i=0}^{n}|q_i|, \qquad \gamma_n=\frac{nu}{1-nu}, $$

where \(\widehat S\) is the computed sum. Dividing by \(|S|\), when \(S\ne0\), gives a relative error bound no larger than \(\gamma_n\kappa_{\mathrm{sum}}\). Thus a small unit roundoff alone does not guarantee a small relative error: a large condition number can magnify the effect.

Why the summation bound holds

Each floating-point addition introduces a relative perturbation of at most \(u\). Repeatedly applying the model to a sum of terms means each original term is multiplied by a product of factors \(1+\delta_j\), with at most \(n\) such factors. For any such product, the distance from one is bounded by \(\gamma_n\), provided \(nu<1\). To see the bound, expand the product and use \(|\delta_j|\leq u\); its total deviation is at most \((1+u)^n-1\), which is bounded by \(nu/(1-nu)\) when \(nu<1\). Consequently, each term's contribution to the absolute summation error is bounded by \(\gamma_n|q_i|\). Adding these bounds gives the displayed inequality.

This is a worst-case bound, not a prediction that every sum loses that many digits. It does explain why cancellation is a warning sign: the bound scales with the sum of the absolute contributions, not with the size of the final answer.

Worked Example: Cancellation in a composite trapezoidal sum

Worked Example: Cancellation in a composite trapezoidal sum

Consider prescribed sample values at \(x=0,1,2\): \(y_0=2^{54}\), \(y_1=1\), and \(y_2=-2^{54}\). These are synthetic data for this calculation; they are not asserted to be values of a particular function. With two panels of width \(h=1\), the composite trapezoidal rule is

$$ T=\frac{h}{2}(y_0+2y_1+y_2). $$

In exact arithmetic, the weighted contributions are

$$ \frac12y_0=2^{53},\qquad y_1=1,\qquad \frac12y_2=-2^{53}. $$

The exact sum is \(2^{53}+1-2^{53}=1\). Equivalently, the rule gives \(T=\frac12(2^{54}+2-2^{54})=1\).

Now evaluate the expression \(y_0+2y_1+y_2\) in binary64, rounding to nearest with ties to even. The product \(2y_1=2\) is exact. At magnitude \(2^{54}\), the spacing between adjacent representable numbers is \(4\). Thus \(2^{54}+2\) lies halfway between \(2^{54}\) and \(2^{54}+4\), and rounds to \(2^{54}\). Adding \(-2^{54}\) then gives zero, so this evaluation returns \(T=0\), not \(1\).

A compensated summation method can retain the small contribution. For example, Neumaier summation tracks a correction \(c\) alongside a running sum \(s\). For each term \(x\), set \(t=s+x\); if \(|s|\geq |x|\), add \((s-t)+x\) to \(c\), and otherwise add \((x-t)+s\) to \(c\). Then set \(s=t\) and return \(s+c\). For the weighted terms \(2^{53},1,-2^{53}\), the first term sets \(s=2^{53}\), \(c=0\). Adding \(1\) rounds \(t\) to \(2^{53}\), but adds \((2^{53}-2^{53})+1=1\) to \(c\). Adding \(-2^{53}\) makes \(s=0\) and adds zero to \(c\). The result is \(s+c=1\).

Check: The compensated result matches the exact weighted sum, \(2^{53}+1-2^{53}=1\). This example also shows why the order and method of addition can matter.

Interpret cancellation as sensitivity, not automatic failure

A large condition number signals that a result is sensitive to changes in its inputs. It does not prove that a particular computation is wrong: exact arithmetic, favorable ordering, or special structure may still produce an accurate answer. Conversely, a small condition number does not protect against errors in the function values or an unsuitable quadrature rule.

Worked Example: A sensitive trapezoidal result

Let \(f\) be the affine function whose endpoint values on \([0,1]\) are \(f(0)=-2^{40}\) and \(f(1)=2^{40}+1\). Since the trapezoidal rule is exact for affine functions, its value is the integral. With one panel, the contributions are

$$ q_0=-2^{39}, \qquad q_1=2^{39}+\frac12. $$

The sum is \(S=\frac12\). Direct integration confirms it: the affine function is \(f(x)=-2^{40}+(2^{41}+1)x\), so

$$ \int_0^1 f(x)\,dx =-2^{40}+\frac{2^{41}+1}{2} =\frac12. $$

The summation condition number is

$$ \kappa_{\mathrm{sum}} = \frac{2^{39}+(2^{39}+\frac12)}{\frac12} =2^{41}+1. $$

The exact binary values in this example can be added to obtain \(\frac12\), so the large condition number does not force an error in this particular calculation. It does show sensitivity. If each endpoint value were uncertain by at most \(\frac14\), the trapezoidal result could be uncertain by as much as \(\frac12(\frac14+\frac14)=\frac14\), which is half the size of the answer.

Check: The integral and trapezoidal estimate are both \(\frac12\). The condition number comes from the large magnitudes of the two contributions, not from a discrepancy between the exact integral and the rule.

Checking a small difference between quadrature estimates

Quadrature calculations often compare estimates from different grids or refinement levels. Subtracting two close numbers is another form of cancellation. A useful diagnostic for \(A-B\), when the difference is nonzero, is \((|A|+|B|)/|A-B|\). A large value means that uncertainty in either estimate can be magnified in the relative accuracy of the difference.

Worked Example: Is a refinement difference trustworthy?

Suppose two computed estimates are \(A=1+2^{-52}\) and \(B=1\), both representable in binary64. Their reported difference is \(D=A-B=2^{-52}\). The subtraction condition number is

$$ \kappa_{\mathrm{sub}} = \frac{|A|+|B|}{|A-B|} = \frac{2+2^{-52}}{2^{-52}} =2^{53}+1. $$

Since \(u=2^{-53}\), we have \(u\kappa_{\mathrm{sub}}\) approximately equal to \(1\). Suppose, for illustration, that each computed estimate may have an absolute arithmetic error as large as \(u\). Then the difference can have an absolute error as large as \(u+u=2u=2^{-52}\), which equals the reported difference itself.

The subtraction of the two stored binary64 numbers is not necessarily inaccurate; in this example the difference is exactly representable. The concern is that earlier rounding in the estimates may be comparable to the difference. The reported change alone therefore does not establish that refinement has changed the underlying quadrature estimate by a reliably resolved amount.

Check: The computed difference is \(2^{-52}\), and the assumed uncertainty bound is also \(2^{-52}\). A higher-precision recomputation or a more stable accumulation can help determine whether the difference is meaningful.

Practical checks and common pitfalls

When a quadrature result looks suspicious, first examine its weighted contributions, rather than only the final total. The sum of their absolute values compared with the magnitude of the result indicates how much cancellation is present. Then check whether the displayed precision is justified by the arithmetic and data uncertainty.

  • Use a suitable summation method. Pairwise or compensated summation can reduce the loss from adding terms of very different sizes. Neither method repairs inaccurate function evaluations or removes quadrature approximation error.
  • Compare at higher precision. Recompute the weighted sum with more digits when the estimated arithmetic uncertainty is comparable to the answer or to a difference being interpreted.
  • Compare different accumulation orders. A substantial change after rearranging the additions is evidence that roundoff may matter. Agreement is reassuring but is not a proof of accuracy.
  • Keep absolute and relative errors distinct. Relative error is not informative when the exact result is zero or extremely small; an absolute error scale is then more appropriate.

A common mistake is to treat every small difference between successive quadrature estimates as a reliable error estimate. For instance, if two estimates are near \(1\) and differ by about \(10^{-16}\), that difference may be at the scale of floating-point rounding. It should not be interpreted as meaningful merely because it was printed with many digits. The correction is to compare the difference with a justified arithmetic uncertainty, check the conditioning of the subtraction, and, if needed, recompute with higher precision or stable summation.

Key takeaway: A quadrature sum can be mathematically well defined and still be numerically sensitive. Inspect the weighted contributions, assess cancellation, and compare arithmetic uncertainty with the size of the reported result or difference.

Check Your Understanding

Use the definitions and checks in this tutorial to answer the following questions.

  1. What does a large value of \(\kappa_{\mathrm{sum}}\) indicate about a weighted quadrature sum?
  2. Under what assumptions does the floating-point addition model apply?
  3. For the contributions \(10^8, 1, -10^8\), what is the exact sum, and why might ordinary sequential addition fail to preserve it?
  4. Why does compensated summation not correct quadrature truncation error?
  5. If two quadrature estimates differ by less than their estimated arithmetic uncertainty, what conclusion can you draw about that difference?