Back to the on-screen lesson ·

Floating point representation

How a number is stored as a significand and an exponent, why the gaps between stored numbers grow with the number, and which decimals are exact.

Paper packet. Every task here also exists on screen, where it is checked automatically; answers written on paper are not assessed by Nydus. When you are back at a device, enter your answers there.

1. What you will learn

By the end of this lesson you will be able to say which values a binary floating-point format stores exactly and which it does not, work out the gap between one stored number and the next at any size, and state what machine epsilon is and what it is not.

2. What you already know

You can write a whole number in binary, where the columns are $\ldots, 8, 4, 2, 1$, and you know that a fraction in base ten terminates exactly when its denominator in lowest terms is built only from twos and fives. Both facts are about to decide what a computer can and cannot hold.

3. The words this lesson uses

The significand is the digits of a number, and the exponent is where the point sits; together they are the number. A format is normalised when the significand starts with a single non-zero digit, so that each number is written only one way. Machine epsilon is the gap from $1$ to the next number the format can hold. A value is exact in the format when it is one of the numbers the format holds, and rounded when it is not.

4. Floating point representation

A floating-point number is $\pm\, 1.b_1 b_2 \cdots b_p \times 2^{e}$: a sign, a fixed number $p$ of binary fraction digits, and an exponent. The leading $1$ is not stored, because a normalised number always has one. What follows from that shape is the whole of this unit. First, the representable numbers are evenly spaced within each power-of-two interval and the spacing doubles at every power of two — the grid is relative, not absolute. Second, the gap from $1$ to the next number, machine epsilon $\varepsilon = 2^{-p}$, bounds the relative error of storing any number: every value is at worst $\varepsilon/2$ away, relatively, from the one the format keeps. Third, a value is exact only when its denominator in lowest terms is a power of two — so $\tfrac{1}{2}$, $\tfrac{3}{4}$ and $\tfrac{7}{8}$ are exact and $0.1$ is not, in any number of bits, for the same reason $\tfrac13$ never terminates in decimal.

Another way: steps

  1. Write the value as a fraction in lowest terms.
  2. If the denominator is a power of two, it is exact; otherwise it is not, whatever the precision.
  3. To find the spacing near a number, find the power of two below it and multiply that by $\varepsilon$.
  4. To bound the relative error of storing it, use $\varepsilon/2$.

Another way: example

With $p = 3$ fraction bits the numbers between $1$ and $2$ are $1, \tfrac98, \tfrac{10}{8}, \ldots, \tfrac{15}{8}$ — eight of them, one eighth apart. Between $2$ and $4$ there are eight again, a quarter apart; between $\tfrac12$ and $1$, eight more, one sixteenth apart. Same count everywhere, different width.

5. Two beliefs to give up now

The first is that $0.1 + 0.2 = 0.3$ and a computer that disagrees is broken. None of those three values is stored exactly, the sum of the first two rounds to something a hair above the third, and every one of those steps is correct. Comparing two floating-point results with $=$ is asking a question the format cannot answer. The second is that machine epsilon is the smallest number the format can hold. It is not: it is a gap at $1$, and the smallest positive number is set by the exponent range, which is a different field entirely.

6. Is three tenths exact?

  1. In lowest terms $0.3 = \dfrac{3}{10}$, and $10 = 2 \times 5$.

    Look at the denominator, not the digits.

  2. The factor of $5$ is not a power of two, so in binary the digits repeat for ever and the format cuts them off.

    Not exact, at any precision.

  3. So the stored value is a little above or below $0.3$, by at most $\varepsilon/2$ relatively — small, and not zero.

    The error is bounded, not absent.

7. How wide is the gap near a thousand?

  1. $1024 = 2^{10}$, so a number near a thousand sits in the interval from $2^{9}$ to $2^{10}$ or the one just above it.

    Find the power of two first.

  2. The spacing there is $2^{9}\varepsilon$ or $2^{10}\varepsilon$: about a thousand times the spacing at $1$.

    The grid stretches with the number.

  3. So adding $1$ to a number near $2^{53}$ in double precision changes nothing at all, because $1$ is less than half a gap.

    This is where sums quietly stop growing.

8. Your turn: is seven eighths exact, and what is the gap above it?

  1. In lowest terms it is $\dfrac{7}{8}$, and $8 = 2^{3}$ is a power of two.

    So it is exact.

  2. It lies between $\tfrac12$ and $1$, so the spacing there is $\tfrac12 \varepsilon$.

  3. Your turn: work this step out. Its working is at the end of the packet.

    Half the spacing at $1$, because the whole interval is half as wide and holds the same count.

9. Guided practice

The value $\dfrac{9}{16}$ is stored exactly, because $16$ is a power of two. Write its four binary fraction digits.

HalvesQuartersEighthsSixteenths
Binary digit

10. Guided practice

A binary format keeps $12$ fraction bits. Which number is its machine epsilon?

11. Practice

A format stores a sign, an exponent and $13$ fraction bits. Match each part to what it decides.

Which way the number pointsHow large the number is, and how wide the gaps around it areWhich number of that size this one isNothing, because it is always a one and is never stored
The sign bit
The exponent field
The fraction field
The leading digit of the significand

12. Practice

A format with three fraction bits stores, between $1$ and $2$, exactly the numbers $1 + \dfrac{m}{8}$. Place the one with $m = 1$.

1 |——————————| 2

Mark the position with a cross, then write the value:

13. Somewhere new

A format keeps $8$ fraction bits, so a stored number is $1.b_1 \ldots b_{8}$ times a power of two. What is the largest whole number $N$ such that every whole number from $0$ to $N$ is stored exactly?

Answer:

14. Lesson test

Lesson test: one question per skill, one attempt each, no hints. Your answers are checked when you submit.

15. Test question

A format keeps $11$ fraction bits. Between $1$ and $2$ its numbers are $2^{-k}$ apart, and there are $c$ of them counting $1$ and not counting $2$. Give $k$ and $c$.

$k = $ k and $c = $ c

16. What you can do now

You can decide whether a value is exact, find the spacing of the stored numbers near it, and say what machine epsilon measures. In your own words, why does the gap between neighbouring stored numbers grow as the numbers grow?

Working for the steps left to you

8. Your turn: is seven eighths exact, and what is the gap above it?, step 3