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.
The numerator after multiplying by the conjugate is . The new denominator adds positive quantities instead of subtracting nearly equal ones.
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.
- Direct expression
- Equivalent for x > 0
- At x = 10¹⁶
For at large positive x, the two square roots are close. Multiplying by the conjugate gives , 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.
Your saved draft is open. To use the published code, select Restore example in Tools. Use Recover draft to bring back your code.
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.
Execution error
Technical error details
Your result
Python text output
Compare with the worked example
Executed from the published Python during the site build. Your own run appears above. Build environment ↗
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.
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?
Need a hint?
Hint 1
Inspect operations that subtract close values.
Hint 2
Compare with a suitable higher-precision or exact reference.