Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Practice analyzing errors, stability, and conditioning.

Self-Assessment Questions

Test your understanding with these conceptual questions:

  1. Machine Epsilon: What happens when you add a number smaller than εmach\varepsilon_{\text{mach}} to 1.0?

  2. Conditioning: What makes evaluating tan(x)\tan(x) near x=π/2x = \pi/2 ill-conditioned? Can any algorithm fix this?

  3. Stability: Why is computing x+1x\sqrt{x+1} - \sqrt{x} directly unstable for large xx? What’s a stable alternative?

  4. Machine Epsilon: What is the approximate machine epsilon for single and double precision? What does it tell us about the accuracy of floating point computations?

  5. Subtractive Cancellation: When subtracting two numbers aa and bb, under what conditions is the result reliable?

  6. Trade-offs: In finite differences, why can’t we just use an extremely small hh to get arbitrary accuracy?

Floating-Point Arithmetic


Q2.1: Machine Epsilon


Q2.2: Floating-Point Surprises


Q2.3: Summation Order

Consider summing s=k=1n1ks = \sum_{k=1}^{n} \frac{1}{k} for n=107n = 10^7.


Q2.4: Kahan Summation

Kahan (1965) proposed a compensated summation algorithm that tracks roundoff error:

def kahan_sum(xs):
    s = 0.0  # Running sum
    c = 0.0  # Compensation for lost low-order bits

    for x in xs:
        y = x - c        # Compensate for previous error
        t = s + y        # Add to sum
        c = (t - s) - y  # Recover the roundoff error
        s = t

    return s

Condition Numbers


Q2.8: Condition Number Computation

Compute the condition number κ=xf(x)/f(x)\kappa = |xf'(x)/f(x)| for:

Which evaluations are well-conditioned? Which are ill-conditioned?

For f(x)=exf(x) = e^x, we have f(x)=exf'(x) = e^x.

K=xf(x)f(x)=xexex=xK = \left|\frac{x f'(x)}{f(x)}\right| = \left|\frac{x e^x}{e^x}\right| = |x|

At x=1x = 1: K=1K = 1. This is well-conditioned.


Q2.9: Subtractive Cancellation

The expression x+1x\sqrt{x+1} - \sqrt{x} suffers from cancellation for large xx.


Q2.10: Trigonometric Reformulation

For small xx, the expression 1cos(x)1 - \cos(x) loses accuracy.


Stability Analysis


Q2.11: Stable Reformulation

For each of the following expressions, identify the potential numerical issue and propose a stable reformulation:

Problem: Subtractive cancellation when x2+1x\sqrt{x^2+1} \approx x.

Stable form: Multiply by x2+1+xx2+1+x\frac{\sqrt{x^2+1}+x}{\sqrt{x^2+1}+x}:

x2+1x=(x2+1)x2x2+1+x=1x2+1+x\sqrt{x^2+1} - x = \frac{(x^2+1) - x^2}{\sqrt{x^2+1}+x} = \frac{1}{\sqrt{x^2+1}+x}

Computational Exercises


Q2.12: Trapezoidal Rule with Round-off

Consider computing the integral:

I=12kdxx=2(2k/21)I = \int_1^{2^k} \frac{dx}{\sqrt{x}} = 2(2^{k/2} - 1)

using the trapezoidal rule with k=22k = 22. The approximation is:

IΔx(i=1N1f(xi)+f(x0)+f(xN)2)I \approx \Delta x \left(\sum_{i=1}^{N-1} f(x_i) + \frac{f(x_0) + f(x_N)}{2}\right)

where f(x)=x1/2f(x) = x^{-1/2} and Δx=2k1N\Delta x = \frac{2^k - 1}{N}.

To force single precision in NumPy:

s = np.asarray(0.0, dtype=np.float32)

Q2.13: Fast Inverse Square Root Accuracy

Refer to the fast inverse square root code.