Lecture 2. Computer algebra and floating-point numbers

2. Lecture 2. Computer algebra and floating-point numbers#

Date and room: see Harmonogram (in Polish).

Lecture materials

  • Slides (reveal.js) — 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.

  • IEEE 754: floating-point numbers from the inside — 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.

  • Lab 3 — computer exercises: rounding, gradual underflow, the exact error of addition, Muller’s sequence.

  • Applet: anatomy of a floating-point number — the bits of a float, neighboring machine numbers and a toy system with adjustable precision.

  • Lecture 1, §1.3 — how a computer algebra system works (the first part of this lecture), and §1.2 and §1.4 — a first encounter with the float type: 0.1 + 0.2, Rump’s example, interval arithmetic.

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 §1.3 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 (in Polish).

Outline

  1. Representing numbers in a computer — 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. Rounding and arithmetic — 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. Errors: problem conditioning and algorithm stability — 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. One computation, three perspectives — 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.

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.

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.