Week 1 - Floating Point#
Numerical problem solving#
- Understand the specification
- Analyse the problem
- Design an algorithm and tests
- Implement
- Test and iterate
Numerical modelling is an iterative design process.
Approaches#
| Top-down (stepwise refinement) | Bottom-up |
|---|---|
| Start from the full problem | Start from small components |
| Divide it into smaller parts | Combine components into a complete solution |
Practical solutions usually mix both approaches.
Types of error#
| Truncation | Round-off |
|---|---|
| Error caused by the numerical method | Error caused by finite-precision arithmetic |
Truncation error#
Replacing an infinite process with a finite approximation.
For the exponential function:
Using only the first n terms:
Taylor approximation of e^{-0.4}
Increasing n makes P_n(-0.4) approach e^{-0.4}.
For an alternating series, the error is bounded by the next omitted term.
Round-off error#
Error caused by storing numbers using finitely many digits.
- Accumulates during arithmetic
- Can dominate even when truncation error is small
- Severe when large values cancel
Computing e^{-30.4}
A direct Taylor sum creates large alternating terms which nearly cancel, producing a highly inaccurate result.
A safer form is:
Binary numbers#
| Decimal part | Conversion |
|---|---|
| Integer | Repeatedly divide by 2; read remainders upwards |
| Fraction | Repeatedly multiply by 2; read integer parts downwards |
Decimal to binary
Some decimal fractions repeat infinitely in binary.
Floating-point representation#
| Part | Meaning |
|---|---|
| \pm | Sign |
| d_1\ldots d_p | Mantissa/significand |
| B | Base |
| e | Exponent |
- Normalised numbers have d_1\neq0
- Computers normally use base 2
- Only finitely many real numbers can be represented exactly
Base-10 normalisation
IEEE 754#
Normalised binary form:
| Precision | Total bits | Sign | Exponent | Fraction |
|---|---|---|---|---|
| Single | 32 | 1 | 8 | 23 |
| Double | 64 | 1 | 11 | 52 |
| Extended | 80 | 1 | 15 | 64 |
For single precision:
Normal exponent range:
Binary normalisation
Machine Epsilon#
Distance between 1 and the next representable floating-point number.
For double precision:
Representation error:
Machine epsilon in Python
import sys
sys.float_info.epsilon
Chopping and Rounding#
| Chopping | Rounding |
|---|---|
| Discard extra digits | Choose the nearest representable value |
| Usually larger error | Usually smaller error |
Chopping vs rounding
| Chopping | Rounding |
|---|---|
| 0.139\times10^1 | 0.140\times10^1 |
Cancellation#
Subtracting nearly equal numbers can remove significant digits.
Loss of significance
Most of the original significant information is lost.
Floating-point comparisons#
Avoid exact comparisons:
x == 8.932
Use a tolerance:
abs(x - 8.932) < tolerance
Good practice
- Avoid exact floating-point equality
- Avoid subtracting nearly equal values
- Reformulate numerically unstable expressions
- Verify results using another method
- More iterations do not always mean greater accuracy
Comments
Powered by GitHub issues