# Lecture 2. Computer algebra and floating-point numbers

**Date and room:** see [Harmonogram](https://bnaskrecki.faculty.wmi.amu.edu.pl/math-comp/book/harmonogram.html) (in Polish).

```{admonition} Lecture materials
:class: tip
* [Slides (reveal.js)](https://bnaskrecki.faculty.wmi.amu.edu.pl/math-comp/en/slides_w02.html) — the `S` key opens
  the speaker notes, `Esc` the slide overview. Slides marked “🤿 deep dive” only announce a topic and point to
  the deep dive.
* {doc}`IEEE 754: floating-point numbers from the inside <../ieee754/ieee754>` — a deep dive with proofs, details
  of the standard and many additional examples. This chapter goes through the most important ideas and in every
  section tells you where to find the rest in the deep dive.
* {doc}`Lab 3 <../laboratoria/lab03_ieee754>` — computer exercises: rounding, gradual underflow, the exact error
  of addition, Muller’s sequence.
* [Applet: anatomy of a floating-point number](https://bnaskrecki.faculty.wmi.amu.edu.pl/math-comp/en/applets/float-explorer.html) —
  the bits of a `float`, neighboring machine numbers and a toy system with adjustable precision.
* Lecture 1, {doc}`§1.3 <w01_3_jak_dziala_cas>` — how a computer algebra system works (the first part of this
  lecture), and {doc}`§1.2 <w01_2_python_matematyka>` and {doc}`§1.4 <w01_4_obliczenie_sprawdzenie_dowod>` —
  a first encounter with the `float` type: `0.1 + 0.2`, Rump’s example, interval arithmetic.
```

```{rubric} What this lecture is about
```

The lecture has four parts. The first answers the question of how a computer algebra system works: an
expression is a tree, simplification is either automatic or on request, polynomials and rational functions
have a canonical form, and the general zero problem is undecidable. In the book, this part is covered by
{doc}`§1.3 <w01_3_jak_dziala_cas>` in the chapter of Lecture 1. The other three parts — storing numbers,
rounding and arithmetic operations, errors and guarantees — are sections 2.1–2.3 of this chapter. Section 2.4
develops the last two topics of the lecture, interval arithmetic and the pyramid of trust, on a new example.

A computer has finitely many numbers at its disposal, while mathematics needs all the real numbers. We have
already seen the consequences of this mismatch: `0.1 + 0.2` is not equal to `0.3`, and Rump’s expression
evaluated in `float` has the wrong sign and the wrong order of magnitude (in the book: Lecture 1, §1.2 and
§1.4). Now we turn to the question behind these examples: **what exactly does floating-point arithmetic
guarantee, and how can we use this guarantee to predict the error of a whole computation?**

The answer has three levels. The first is how numbers are stored: we compare fixed-width integers,
fixed-point and floating-point representations, and then describe the *binary64* format in detail. The
second is a single rule of the IEEE 754 standard — the result of an operation is the correctly rounded exact
result — and its consequences: the error model, the failure of associativity, Sterbenz’s lemma. The third
is error analysis: the conditioning of a problem and the stability of an algorithm explain when a computation
loses digits and what can be done about it. At the end we return to the guiding thread of the course,
“computation, verification, proof”: we look at one computation in `float`, in exact arithmetic and with a
guarantee.

How this lecture relates to the syllabus of the formal course:
[Sylabus w skrócie](https://bnaskrecki.faculty.wmi.amu.edu.pl/math-comp/book/sylabus.html) (in Polish).

```{rubric} Outline
```

1. {doc}`w02_1_zapis_liczb` — integers of arbitrary length and of fixed width, fixed-point and floating-point
   representations, the *binary64* format, $\ulp$, $\varepsilon$ and $u$, the range, subnormal numbers.
2. {doc}`w02_2_zaokraglanie_i_dzialania` — rounding to nearest, the correct-rounding rule and its experimental
   verification, the error model, the laws of arithmetic that survive and those that do not, the order of
   summation, Sterbenz’s lemma.
3. {doc}`w02_3_bledy` — absolute and relative error, the condition number, catastrophic cancellation, the
   stability of an algorithm (roots of a quadratic equation, $(1+1/n)^n$ and `log1p`), the error of summation,
   Muller’s sequence.
4. {doc}`w02_4_trzy_perspektywy` — the sign of the polynomial $(x-1)^7$ near its root: computation in `float`,
   verification with fractions, the error theorem for Horner’s scheme and interval arithmetic.

```{rubric} After this lecture you will be able to
```

* compare fixed-width integer, fixed-point and floating-point representations in terms of range, absolute
  error and relative error;
* determine the parameters of a floating-point system, compute $\ulp(x)$, $\varepsilon$ and $u$ for the
  *binary64* format, and describe the role of subnormal numbers;
* state the correct-rounding rule, derive from it the model $\fl(x \circ y) = (x \circ y)(1 + \delta)$,
  $|\delta| \le u$, and verify it experimentally with `fractions.Fraction`;
* point out the laws of arithmetic that hold in machine arithmetic and those that fail, and prove
  Sterbenz’s lemma;
* compute the condition number of a problem, distinguish an ill-conditioned problem from an unstable
  algorithm, and transform a formula so as to avoid catastrophic cancellation;
* bound the error of summation and of Horner’s scheme, and obtain a guaranteed result with interval
  arithmetic.

```{rubric} How to work with this chapter
```

Every section is a Jupyter notebook, and all results come from executing the code when the book is built.
You need only the Python standard library (`math`, `fractions`, `random`), NumPy, SymPy, mpmath and
Matplotlib, that is, the environment from Lab 1. A few results depend on the Python version: since version
3.12 the built-in function `sum` adds `float` numbers with error compensation, which we point out in the text.
Every section ends with exercises; the marks ★, ★★ and ★★★ mean the same as in the lab problem sets.
