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.

Solutions · 0. Floating-point numbers, cancellation and compensated summation

School of Engineering, Nazarbayev University

Worked solutions to the three exercises of Lecture 0. Each solution is computed here and checked with assert statements against an exact identity or an independent calculation. The first code cell sets up the notebook and runs Demonstration 1 of the lecture without showing its output, because some of the solutions use its variables.

Exercise 1

Problem. Prove via fractions.Fraction(0.1) that IEEE-754 0.1 exceeds 110\frac{1}{10} by exactly +2−55/5+2^{-55}/5, and compare cumulative clock error over 100 hours (3,600,0003{,}600{,}000 ticks of 0.1 s0.1\,\mathrm{s}) across Patriot’s 24-bit signed fixed-point register (1 sign bit + 23 fractional bits), float64 t += 0.1 accumulation, and integer tick counting k * 0.1.


Exercise 1: Exact binary excess of 0.1 and Patriot 100-hour range-gate drift
============================================================================
Fraction(0.1)          = 3602879701896397 / 2^55
Exact excess over 1/10 = 1/180143985094819840 == +1 / (5 * 2^55) (+5.551115e-18 s)

After 100 hours (3,600,000 ticks of 0.1 s = 360,000.0 s):
  (a) Patriot 24-bit chopped drift : 0.343323 s -> Scud miss @ 1676 m/s = 575.4 m
  (b) float64 loop `t += 0.1` err  : 1.201301e-05 s
  (c) Integer tick `k * 0.1` err   : 0.000000e+00 s

Explanation. Because 10=2×510 = 2 \times 5 has prime factor 5≠25 \neq 2, 110=0.00011‾2\frac{1}{10} = 0.0\overline{0011}_2 is an infinite repeating binary fraction. Chopping 1/101/10 after 23 fractional bits loses 2−20/10≈9.537×10−8 s2^{-20}/10 \approx 9.537 \times 10^{-8}\,\mathrm{s} every tick, growing linearly over 3.6×1063.6 \times 10^6 ticks to 0.3433 s0.3433\,\mathrm{s} (a Scud travels 575 m575\,\mathrm{m} in that time). Storing time as an integer tick counter k and converting k * dt on demand eliminates cumulative drift completely.


Exercise 2

Problem. Quantify catastrophic digit loss in the textbook quadratic root (−b−b2−4ac)/(2a)(-b - \sqrt{b^2 - 4ac})/(2a) for x2+bx+1=0x^2 + b x + 1 = 0 across b∈[−104,−109]b \in [-10^4, -10^9] relative to Vieta’s Citardauq invariant x2=c/(ax1)x_2 = c / (a x_1), and audit naive np.log(np.sum(np.exp(z))) at ±800\pm 800.


Exercise 2: Catastrophic quadratic root cancellation and naive LogSumExp failure
================================================================================
          b |        Textbook x2 |     Vieta c/(a*x1) | Textbook RelErr |  Vieta RelErr
--------------------------------------------------------------------------------------
   -1.0e+04 |   1.0000000111e-04 |   1.0000000100e-04 |        1.12e-09 |      1.00e-17
   -1.0e+05 |   1.0000003385e-05 |   1.0000000001e-05 |        3.38e-07 |      1.69e-16
   -1.0e+06 |   1.0000076145e-06 |   1.0000000000e-06 |        7.61e-06 |      1.00e-17
   -1.0e+07 |   9.9651515484e-08 |   1.0000000000e-07 |        3.48e-03 |      1.00e-17
   -1.0e+08 |   7.4505805969e-09 |   1.0000000000e-08 |        2.55e-01 |      1.00e-17
   -1.0e+09 |   0.0000000000e+00 |   1.0000000000e-09 |        1.00e+00 |      1.00e-17

Naive LSE(+800..802) = inf vs Stable = 802.407606
Naive LSE(-800..-798) = -inf vs Stable = -797.592394

Explanation. At b=−109b = -10^9, b2=1018b^2 = 10^{18} has ulp(1018)=128>4\mathrm{ulp}(10^{18}) = 128 > 4, so b2−4=109\sqrt{b^2 - 4} = 10^9 identically and the textbook numerator −b−b2−4-b - \sqrt{b^2 - 4} collapses to 0.0 (100%100\% relative error). Computing the non-cancelling root x1=(−b+sign(−b)b2−4ac)/(2a)x_1 = (-b + \mathrm{sign}(-b)\sqrt{b^2 - 4ac})/(2a) first and recovering x2=c/(ax1)x_2 = c/(a x_1) via Vieta’s invariant maintains full 16-digit accuracy across every scale.


Exercise 3

Problem. Implement compensated_dot_and_pairwise_sum(a, b) returning np_dot, pairwise_dot, dot2_compensated, and exact_dot using recursive pairwise product summation and Ogita–Rump–Oishi’s error-free Dot2 transformation (two_prod via Veltkamp 227+12^{27}+1 splitting and Knuth two_sum). Verify on an ill-conditioned dot product with condition number κ>1013\kappa > 10^{13}.


Exercise 3: Implement it yourself — Ogita-Rump-Oishi Dot2 and pairwise summation
================================================================================
Condition number kappa = 1.04e+15
Exact rational dot     = +1.047532390137765
np.dot                 = +1.041015625000000 (rel err = 6.22e-03)
Pairwise product sum   = +1.046875000000000 (rel err = 6.28e-04)
Ogita-Rump-Oishi Dot2  = +1.047532390137765 (rel err = 0.00e+00)

Summary. Pairwise summation improves summation error growth from O(Nε)\mathcal{O}(N\varepsilon) to O(εlog⁡2N)\mathcal{O}(\varepsilon \log_2 N), yet it cannot recover low-order product bits already lost inside each rounded a[i] * b[i]. Ogita–Rump–Oishi’s Dot2 captures both product rounding (_two_prod) and addition rounding (_two_sum) into a single scalar accumulator s, achieving simulated quadruple-precision (ε2∼10−31\varepsilon^2 \sim 10^{-31}) accuracy in pure float64.