Connect symbols to numbersLesson 45 of 48 · 9 min

Rewrite a cancellation-prone calculation

Compare equivalent formulas whose floating-point behaviour differs.

Maths refresher: Rationalising a difference

Multiplying by a conjugate can replace a small difference with a product identity. Denominators must remain valid.

x+1−x=1x+1+x(x>0)\sqrt{x+1}-\sqrt{x}=\frac1{\sqrt{x+1}+\sqrt{x}}\quad(x>0)
Worked example

The numerator after multiplying by the conjugate is (x+1)−x=1(x+1)-x=1. The new denominator adds positive quantities instead of subtracting nearly equal ones.

Maths prerequisite map ↗

Subtracting nearly equal numbers can lose significant digits in floating-point arithmetic. Two exactly equivalent symbolic formulas can therefore behave very differently when evaluated numerically. Simplification for readability and reformulation for numerical stability are related but distinct tasks.

Avoid subtracting nearly equal roots
  1. Direct expression
    x+1−x\sqrt{x+1}-\sqrt{x}
  2. Equivalent for x > 0
    1x+1+x\frac1{\sqrt{x+1}+\sqrt{x}}
  3. At x = 10¹⁶
    reference≈5×10−9\text{reference}\approx5\times10^{-9}
Starter reference. The second form avoids subtracting two rounded square roots. Inspect the actual scalar calculations below; exact equivalence does not guarantee identical floating-point behaviour.

For x+1−x\sqrt{x+1}-\sqrt{x} at large positive x, the two square roots are close. Multiplying by the conjugate gives 1x+1+x\frac1{\sqrt{x+1}+\sqrt{x}}, which avoids subtracting those nearly equal values. The real-positive domain justifies the manipulation here.

Compare both ordinary scalar implementations with a high-precision reference built from an exact integer input. A successful example demonstrates this specific instability; it is not a general guarantee that every algebraic rewrite improves every calculation. Retain the identity and the numerical check together.

Can two mathematically equal formulas produce different ordinary floating-point answers?

Write a prediction (optional)

Exact identity and floating-point comparison

Select Run Python with the code unchanged.

Tools
About Python

Python and the course packages can load while you read. Each run starts with fresh user variables; imported modules stay loaded. Stop replaces the worker. Runtime details ↗

Ctrl+Enter or ⌘+Enter runs Python.

Compare with the worked example

Executed from the published Python during the site build. Your own run appears above. Build environment ↗

Exact input
1000000000000000010000000000000000
Stable form
1x+x+1\frac{1}{\sqrt{x} + \sqrt{x + 1}}
Symbolic difference check
00
Direct float evaluation
0.00.0
Rewritten float evaluation
5.0⋅10−95.0 \cdot 10^{-9}
40-digit reference
0.000000004999999999999999875000000000000006250.00000000499999999999999987500000000000000625
Worked text output
Exact input: 10000000000000000
Stable form: 1/(sqrt(x) + sqrt(x + 1))
Symbolic difference check: 0
Direct float evaluation: 0.0
Rewritten float evaluation: 5e-09
40-digit reference: 4.999999999999999875000000000000006250000E-9

Read the starter result

Compare both float evaluations with the 40-digit reference, which uses the exact input. The direct subtraction loses the small difference entirely at the starter’s input size.

Edit and compare

Replace sp.Integer(10)**16 on the exact_input line with sp.Integer(10)**4 and select Run Python. Then use exponent 8 and select Run Python again. Compare Direct float evaluation and Rewritten float evaluation with 40-digit reference for all three input sizes.

Return to the editor

Scope. The rewrite is justified on this positive domain and addresses one cancellation mechanism; general numerical stability needs broader analysis.

Try a fresh case

A symbolic identity is established. What remains to check before choosing a numerical implementation?

Choose an answer
Need a hint?
Hint 1

Inspect operations that subtract close values.

Hint 2

Compare with a suitable higher-precision or exact reference.

Your notesNotes save in this browser when storage is available.