Lesson

How Computers Store Numbers

How a computer stores numbers: binary fractions, the floating-point format and the IEEE 754 standard.

Last updated:


What does a computer print for 0.1 + 0.2?

In Python, print(...) shows on the screen the value of what is inside the parentheses, and == asks whether its two sides are equal and answers True or False.
Run these two lines. Before you do, predict what you expect to see, and why.

print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)
Is this only Python?

No. The surprise comes from how the number is stored, not from the language.
C and Java also store a double (the usual 64-bit number type) in binary, so 0.1 and 0.2 are approximate there too, and the sum can be 0.30000000000000004.
What differs is how the program prints it. C’s printf("%f", 0.1 + 0.2) shows only six places after the point, 0.300000, which hides the difference, while printf("%.17g", 0.1 + 0.2) shows it.
Java’s System.out.println(0.1 + 0.2) prints the long value.

Sources: the Java Language Specification, floating-point types, the Java Double.toString documentation, and the C printf family reference, which gives six places as the default for %f.

You were probably surprised by the results. The computer is not malfunctioning:
it follows exact rules, and those rules explain a great deal about what you can and cannot trust in a numerical result.
Every method in this course produces approximate numbers, so we start by asking how a computer holds a number in the first place.

The computer does not compare the exact sum 0.1+0.20.1+0.2 with the exact number 0.30.3⁠, because it cannot store any of the three exactly.
It works in three stages:

  1. it stores a nearby number for 0.10.1 and another for 0.20.2⁠;
  2. it adds the two stored numbers, and the result has to be stored too;
  3. it compares that result with the number it stored, separately, for 0.30.3⁠.

So what does the computer actually store for 0.10.1⁠, 0.20.2 and 0.30.3⁠?

In this lesson we learn to:

  • write a short decimal number in binary, and say in advance whether its binary expansion ends;
  • read the sign, exponent and fraction parts of a stored floating-point number;
  • say what machine epsilon measures: the gap between 1 and the next stored number.

Positional notation: from decimal to binary

In the decimal system the digits of a number are coefficients of powers of 10:

257.76=2⋅102+5⋅101+7⋅100+7⋅10−1+6⋅10−2257.76 = 2\cdot10^{2}+5\cdot10^{1}+7\cdot10^{0}+7\cdot10^{-1}+6\cdot10^{-2}

A computer uses the same idea with powers of 2, so every digit is 0 or 1.
Such a digit is a bit, and a number written with bits is in binary notation.
For example:

1011.00112=1⋅23+0⋅22+1⋅21+1⋅20+0⋅2−1+0⋅2−2+1⋅2−3+1⋅2−4=11.1875\begin{aligned} 1011.0011_2 &= 1\cdot2^{3}+0\cdot2^{2}+1\cdot2^{1}+1\cdot2^{0}\\ &\quad+0\cdot2^{-1}+0\cdot2^{-2}+1\cdot2^{-3}+1\cdot2^{-4}\\ &= 11.1875 \end{aligned}

The small subscript 2 means “written in base 2”. Two procedures convert a decimal number to binary, and they are worked out step by step in the block below.

Try it before you open the block: write 0.18750.1875 as a sum of negative powers of 2, and from that in binary.

Hint

The place values after the point are 2−1=0.52^{-1}=0.5⁠, 2−2=0.252^{-2}=0.25⁠, 2−3=0.1252^{-3}=0.125 and 2−4=0.06252^{-4}=0.0625⁠.
Which of them is the largest that fits in 0.18750.1875⁠, and what is left after it?

Worked conversions: integer part and fractional part

Integer part: divide by 2 repeatedly and read the remainders from bottom to top. For 1111⁠:

DivisionAs a fractionQuotientRemainder
11 ÷ 2112=5+12\tfrac{11}{2}=5+\tfrac{1}{2}51
5 ÷ 252=2+12\tfrac{5}{2}=2+\tfrac{1}{2}21
2 ÷ 222=1+0\tfrac{2}{2}=1+010
1 ÷ 212=0+12\tfrac{1}{2}=0+\tfrac{1}{2}01

Reading the remainders upward gives 1110=1011211_{10}=1011_2⁠.

Fractional part: multiply by 2 repeatedly; each time, the digit before the decimal point is the next bit. For 0.18750.1875⁠:

ValueAs a fraction× 2BitRemaining fraction
0.1875316\tfrac{3}{16}0.37500.375
0.37538\tfrac{3}{8}0.7500.75
0.7534\tfrac{3}{4}1.510.5
0.512\tfrac{1}{2}1.010

The fraction reached 0, so the process ends: 0.187510=0.001120.1875_{10}=0.0011_2⁠. Together, 11.187510=1011.0011211.1875_{10}=1011.0011_2⁠.

Work it out: which of the numbers 0.50.5⁠, 0.18750.1875⁠, 0.10.1 and 0.30.3 have a binary expansion that ends?
Apply the doubling procedure above and decide before reading on.

Check your answers
DecimalFractionBinaryDoes it end?
0.512\tfrac{1}{2}0.120.1_2yes
0.1875316\tfrac{3}{16}0.001120.0011_2yes
0.1110\tfrac{1}{10}0.00011‾20.0\overline{0011}_2no
0.3310\tfrac{3}{10}0.010011‾20.01\overline{0011}_2no

Word a rule: when does a fraction have a binary expansion that ends?
Write one sentence from the table, and only then open the candidate wordings.

Candidate wordings
  1. “When it has few digits after the decimal point.” Wrong: 0.10.1 has one digit, and its expansion never ends.
  2. “When its denominator is even.” Wrong: the denominator of 110\tfrac{1}{10} is even, and the expansion never ends.
  3. “When the denominator of the fraction in lowest terms is a power of 2.” Check it on the four rows of the table.
The rule

The repeated doubling we applied to each number gives the rule.
A fraction in lowest terms ends in binary exactly when its denominator is a power of 2, because each doubling removes one factor of 2 from the denominator.
Since 0.1=110=12⋅50.1=\tfrac{1}{10}=\tfrac{1}{2\cdot5}⁠, the factor 5 never disappears and the expansion never ends.

Where does the repeating sequence come from?

Write 0.1 in binary place by place. Place number kk after the point is worth 2−k2^{-k}⁠:
12\tfrac12⁠, 14\tfrac14⁠, 18\tfrac18⁠, 116\tfrac1{16} and so on. At each place ask whether 2−k2^{-k} fits in what is left of 0.1:
if it does, the digit is 1 and we subtract it; if not, the digit is 0.

Place kk2−k2^{-k}Fits? DigitWhat is left
112=0.5\tfrac{1}{2}=0.5no, 0110=0.1\tfrac{1}{10}=0.1
214=0.25\tfrac{1}{4}=0.25no, 0110=0.1\tfrac{1}{10}=0.1
318=0.125\tfrac{1}{8}=0.125no, 0110=0.1\tfrac{1}{10}=0.1
4116=0.0625\tfrac{1}{16}=0.0625yes, 1380=0.0375\tfrac{3}{80}=0.0375
5132=0.03125\tfrac{1}{32}=0.03125yes, 11160=0.00625\tfrac{1}{160}=0.00625
6164=0.015625\tfrac{1}{64}=0.015625no, 01160=0.00625\tfrac{1}{160}=0.00625
71128=0.0078125\tfrac{1}{128}=0.0078125no, 01160=0.00625\tfrac{1}{160}=0.00625
81256=0.00390625\tfrac{1}{256}=0.00390625yes, 131280=0.00234375\tfrac{3}{1280}=0.00234375
91512=0.001953125\tfrac{1}{512}=0.001953125yes, 112560=0.000390625\tfrac{1}{2560}=0.000390625

The number 0.10.1 is smaller than 2−3=0.1252^{-3}=0.125 and larger than 2−4=0.06252^{-4}=0.0625⁠, so the first three digits are 0 and the fourth is 1.
After place 5 what is left is 1160=110⋅16\tfrac{1}{160}=\tfrac{1}{10\cdot16}⁠: the same situation as at the start (110\tfrac{1}{10}⁠), only 16 times smaller, that is, shifted by four places.
After place 9 what is left is 12560=110⋅162\tfrac{1}{2560}=\tfrac{1}{10\cdot16^{2}}⁠: again the same situation, another 16 times smaller.
So the digits 0,0,1,10,0,1,1 come again at places 6 to 9, and so on every four places.
That is why the sequence 00110011 repeats forever.

A bar marks a sequence of digits that repeats, and in short, with the sum of the rounds (each round adds 2−4+2−52^{-4}+2^{-5}⁠, made 16 times smaller each time):

0.1=0.00011‾2=(2−4+2−5)∑n=0∞2−4n0.1=0.0\overline{0011}_2=(2^{-4}+2^{-5})\sum_{n=0}^{\infty}2^{-4n}

In the sum above, the first round is 2−4+2−5=116+132=232+132=3322^{-4}+2^{-5}=\tfrac1{16}+\tfrac1{32}=\tfrac{2}{32}+\tfrac1{32}=\tfrac{3}{32}⁠, and each next round is 16 times smaller than the one before.
So, with the general term first and the first terms after it:

0.1=∑j=0∞332⋅116j=∑j=0∞324j+5=332+3512+38192+⋯0.1=\sum_{j=0}^{\infty}\frac{3}{32}\cdot\frac1{16^{j}}=\sum_{j=0}^{\infty}\frac{3}{2^{4j+5}}=\frac{3}{32}+\frac{3}{512}+\frac{3}{8192}+\cdots

This is a geometric series, the kind of infinite series you meet when studying convergence in Calculus 2:
each term is the previous one times the ratio q=116q=\tfrac1{16}⁠, and because ∣q∣<1|q|<1 the series converges, here to

a11−q=3321−116=110,a1=332\frac{a_1}{1-q}=\frac{\frac3{32}}{1-\frac1{16}}=\frac1{10},\qquad a_1=\frac3{32}

But a computer has a fixed, finite number of bits, so it cannot keep an infinite expansion like this.
It stores the closest number it can.

From bits to a floating-point number

The significant digits of a number are the digits that carry its value:
they start at the first nonzero digit and run to the last digit we keep.
So 256.78256.78 has five significant digits, and 0.0036780.003678 has four, because the zeros in front only say where the decimal point is.
We count them because they tell us how good an approximation is relative to the size of the number.
Rounding to a given number of significant digits has an error of at most half a unit in the last digit we kept.
For example, 3.14159…3.14159\ldots rounded to three significant digits is 3.143.14⁠:
the last digit is the hundredths digit, half a unit there is 0.0050.005⁠, and the error is 0.001590.00159⁠, smaller than that.
The same rounding of 314159314159 is 314000314000⁠: half a unit in the thousands digit is 500500⁠, and the error is 159159⁠.
The errors differ greatly in size, but in both cases they are smaller than half a unit in the last digit we kept, that is, a small and roughly fixed fraction of the number itself, whether the number is large or small.

Scientific notation writes a number as a string of significant digits followed by a power of ten that says where the decimal point is.
For example, 256.78 is written 2.5678×1022.5678\times10^{2}⁠. A computer writes every ordinary nonzero number in binary in a similar form:

x=(−1)s×2e×1.f1f2⋯fp⏟significandx=(-1)^{s}\times 2^{e}\times\underbrace{1.f_1f_2\cdots f_p}_{\text{significand}}
  • ss is the sign: 0 for positive, 1 for negative;
  • ee is an integer exponent;
  • f1,…,fpf_1,\dots,f_p are the fraction bits, and pp is their number: 3 in the tiny format we meet next, and 52 in binary64. The letter pp comes from precision: the more fraction bits, the denser the stored numbers.

The three fields are stored in memory in this order: sign, exponent, fraction.
The part 1.f1f2⋯fp1.f_1f_2\cdots f_p holds the significant digits and is called the significand (also called the mantissa).
In these numbers its first digit is always 1, so it is not stored, which saves space.
(In the IEEE 754 standard the letter pp also counts the leading 1, so there p=53p=53 for binary64; here pp counts only the stored fraction bits.) A way of storing numbers like this, with a fixed number of bits, is called a floating-point format:
the point “floats” with the exponent.

Predict before you go on: a format with a fixed number of bits can store only a finite number of numbers.
Are they spread along the number line at equal gaps? If not, where are they denser?

To see what a fixed number of bits implies, take a tiny format with one sign bit, three exponent bits and three fraction bits.
The seven cells give 27=1282^{7}=128 possible combinations. Half of them, 6464⁠, have the sign cell equal to 00⁠, and eight of those are reserved for special values, such as infinity and NaN (“not a number”).
So the format has only 64−8=5664-8=56 non-negative values, including zero.
Every positive value has a negative twin with the sign cell equal to 11⁠, so the accuracy for negative numbers is the same as for positive ones.
Here are the non-negative values on a number line.

Two number lines showing every non-negative value of a tiny floating-point format. The dots are close together near zero and spread out as the numbers grow: the gap is 1/8 between 1 and 2, 1/4 between 2 and 4, 1/2 between 4 and 8, and 1 between 8 and 16.
Every non-negative number of a format with three exponent bits and three fraction bits. For normal numbers, the gap between neighbours doubles each time the exponent grows by one. The lower line zooms in on the interval from 0 to 2.

The interactive number line needs JavaScript. The figure above shows the same numbers.

To decode seven cells, read them in three groups. The first cell is the sign ss⁠.
The next three cells are the exponent field: read them as a natural number (including 0) EE from 00 to 77⁠, with the left cell the most significant, so 011011 is E=3E=3⁠.
The last three cells are the fraction field, which we write f1f2f3f_1f_2f_3⁠.
Read them as a whole number in base 22⁠, without the 0.0. in front of them, and call it FF (a whole number from 00 to 77⁠).
The fraction 0.f1f2f30.f_1f_2f_3 is FF divided by 22 raised to the number of fraction cells, here 23=82^{3}=8⁠:

0.f1f2f3=F230.f_1f_2f_3=\frac{F}{2^{3}}

For the fraction cells 101101 we have F=5F=5⁠, and placing the values in the formula gives

0.1012=523=580.101_2=\frac{5}{2^{3}}=\frac{5}{8}

The exponent ee in the formula can be negative, for example 2−2=142^{-2}=\tfrac14⁠.
The exponent field, however, is a non-negative whole number.

Suggest a way: the three exponent cells can store only the numbers 0 to 7.
How would you store a negative exponent in them, for example −2-2⁠, without adding a sign cell?

Hint

What happens if we add the same constant to every exponent before storing it?

To store negative exponents without another sign cell, the exponent is shifted by a constant called the bias.
The exponent field is E=e+biasE=e+\text{bias}⁠, so e=E−biase=E-\text{bias}⁠.

When kk is the number of exponent cells, the usual bias is 2k−1−12^{k-1}-1⁠.
In the tiny format k=3k=3⁠, so the bias is 22−1=32^{2}-1=3⁠. (binary64 has k=11k=11 and bias 210−1=10232^{10}-1=1023⁠.)

Examples in the tiny format:

  • e=0e=0 is stored as E=3E=3 (cells 011011⁠);
  • e=−2e=-2 is stored as E=1E=1 (cells 001001⁠);
  • e=3e=3 is stored as E=6E=6 (cells 110110⁠).

For EE from 11 to 66 the value is

x=(−1)s×2E−3×(1+F23)x=(-1)^{s}\times 2^{E-3}\times\left(1+\frac{F}{2^{3}}\right)

Work it out: the exponent field has eight values, and this formula covers six of them.
The cells 0 000 000 have E=0E=0⁠. Use the same formula for E=0E=0 and F=0,1,…,7F=0,1,\dots,7⁠, and list the values.
Which number do the cells 0 000 000 store, and which positive number is the smallest?
How large is the interval from 00 to that number, compared with the gap between neighbouring numbers?
A format needs a zero, because a calculation such as x−xx-x must have a result that can be stored.
It should also have no hole next to zero, where the gap between neighbours is suddenly much larger than the gaps just above it.
Then decide what E=0E=0 should mean, so that the format has a zero and no hole next to it.

Hint

For E=0E=0 the power of 2 is 20−3=182^{0-3}=\frac18⁠, so the value is 18(1+F8)\frac18\left(1+\frac{F}{8}\right)⁠.
Multiply out and write it over the common denominator 6464⁠.

Solution

With E=0E=0 the formula gives

20−3(1+F8)=18+F64=8+F64\begin{aligned} 2^{0-3}\left(1+\frac{F}{8}\right)&=\frac18+\frac{F}{64}\\ &=\frac{8+F}{64} \end{aligned}

so for F=0,1,…,7F=0,1,\dots,7 the values are 864=0.125\frac{8}{64}=0.125⁠, 964=0.140625\frac{9}{64}=0.140625⁠, and so on up to 1564=0.234375\frac{15}{64}=0.234375⁠.
The gap between neighbours is 164\frac1{64}⁠. The cells 0 000 000 store 0.1250.125⁠, not zero, and no pattern of cells stores zero.
The smallest positive number is 0.1250.125⁠, so the interval from 00 to it is 864\frac{8}{64}⁠:
eight times the gap between neighbours. So the formula as it stands leaves E=0E=0 with no zero and a hole beside it, and E=0E=0 needs a rule of its own.
Three candidate rules follow on this page.

We compare three meanings of E=0E=0 on the numbers below 0.50.5⁠, by the two requirements above:
a zero, and no hole next to it.

Version A: no change. The formula for EE from 11 to 66 is used for E=0E=0 too.
There is no zero, and between 00 and the smallest positive number 0.1250.125 there is a hole of eight gaps.

A number line from 0 to 0.5. Eight gray dots between 0.125 and 0.234375 are 1/64 apart, and purple dots start at 0.25. Nothing is stored between 0 and 0.125, a hole of 8 gaps of 1/64.
Version A: one formula for every value of E. There is no zero, and the hole from 0 to 0.125 is eight times the gap 1/64 between its neighbours.

Version B: E=0E=0 means zero only. The cheapest way to get a zero is to define that the first pattern of E=0E=0 stores it, and to give up the other seven patterns.
Now zero exists, but the other seven patterns with E=0E=0 store nothing.
The smallest positive number is 0.250.25⁠, the number with E=1E=1 and F=0F=0⁠.
The gap above it is

21−3×18=14×18=1322^{1-3}\times\frac{1}{8}=\frac14\times\frac18=\frac{1}{32}

so the hole from 00 to 0.250.25 is 14=8×132\frac14=8\times\frac{1}{32}⁠: again eight times the gap between neighbours.

A hole also makes subtraction misleading. Take the stored numbers x=0.28125x=0.28125 and y=0.25y=0.25⁠, which are different.
Their difference is 132\frac1{32}⁠, but this version stores nothing between 00 and 0.250.25⁠, so the computer rounds the result to the nearest stored number, 00⁠:
it reports x−y=0x-y=0 although x≠yx\neq y⁠.

A number line from 0 to 0.5. A gray dot at 0, then nothing until the purple dots that start at 0.25 with gaps of 1/32. The hole from 0 to 0.25 is 8 gaps of 1/32.
Version B: E=0 stores zero only. The hole from 0 to 0.25 is eight times the gap 1/32 above 0.25.

Version C: the updated formula. We start from the natural continuation of the pattern:
when EE drops from 1 to 0, the exponent should drop by 1.
Lowering the exponent by 1 is multiplying by 2−12^{-1}⁠, and this is equivalent to moving all the digits of the significand one place to the right of the binary point, for example

2−3×1.f1f2f3=2−2×0.1f1f2f32^{-3}\times1.f_1f_2f_3=2^{-2}\times0.1f_1f_2f_3

so the leading 1 moves to the right side of the point. After the move we would need four fraction cells, and there are only three.
So we keep the exponent at −2-2⁠, as for E=1E=1⁠, and let the leading 1 travel to the right inside the three cells:
0.1f1f20.1f_1f_2⁠, then 0.01f10.01f_1⁠, then 0.0010.001⁠, and at the end all the cells are zeros.
In short, when E=0E=0 two things change. The leading cell, the digit before the point in the significand, is 00 instead of 11⁠, so the significand is 0.f1f2f30.f_1f_2f_3 instead of 1.f1f2f31.f_1f_2f_3⁠.
The exponent is the one of E=1E=1⁠, which is 1−bias=−21-\text{bias}=-2⁠, and not 0−bias=−30-\text{bias}=-3⁠.
The value is

x=(−1)s×21−3×(0+F8)=(−1)s×14×F8=(−1)s×F32\begin{aligned} x&=(-1)^{s}\times 2^{1-3}\times\left(0+\frac{F}{8}\right)\\ &=(-1)^{s}\times\frac14\times\frac{F}{8}\\ &=(-1)^{s}\times\frac{F}{32} \end{aligned}

This is zero when F=0F=0⁠, and seven very small numbers when F=1,…,7F=1,\dots,7⁠:
132,232,…,732\frac1{32},\frac2{32},\dots,\frac7{32}⁠. They are called subnormal numbers; the numbers with EE from 11 to 66 are normal.
The gap between subnormal numbers is 132\frac1{32}⁠, exactly the gap just above 0.250.25⁠, so there is no hole.
The difference of 0.281250.28125 and 0.250.25 is now stored exactly. More generally, take two different stored numbers xx and yy with the same sign and absolute value below 0.50.5⁠:
their difference is a nonzero multiple of 132\frac1{32} with absolute value below 0.50.5⁠, so it is stored too, and x−y=0x-y=0 only when x=yx=y⁠.

A number line from 0 to 0.5. Gray dots at 0 and 1/32 up to 7/32, then purple dots from 0.25, all with the same gap of 1/32. There is no hole.
Version C: no leading 1 and the exponent of E=1. Zero and the seven subnormal numbers 1/32 to 7/32 continue the same grid of gap 1/32 down to zero.

The exponent is 1−bias1-\text{bias} so that the gap between subnormal numbers is 132\frac1{32}⁠, the same as the gap above 0.250.25⁠:
the numbers then follow each other at equal gaps from 0 up to 0.250.25 and keep the same gap after it, with no hole.
With 0−bias=−30-\text{bias}=-3 the values would be 2−3×F8=F642^{-3}\times\frac F8=\frac{F}{64}⁠: the gap would be 164\frac1{64}⁠, half of the gap above 0.250.25⁠, and the largest subnormal number 764\frac7{64} would leave a hole of 964\frac{9}{64} below 0.250.25⁠.
With 1−bias1-\text{bias} the largest subnormal number 732\frac{7}{32} is exactly one gap of 132\frac1{32} below 0.250.25⁠.

The updated formula is one formula for both kinds of number.
For E=0,1,…,6E=0,1,\dots,6 it is written first in a short form, with the named quantities ee and hh⁠, and then in full:

x=(−1)s⏟sign×2e⏟power of two×(h+F2p)⏟significand=(−1)s×2max⁡(E,1)−bias×(min⁡(E,1)+F2p)\begin{aligned} x&=\underbrace{(-1)^{s}}_{\text{sign}}\times\underbrace{2^{e}}_{\text{power of two}}\times\underbrace{\left(h+\frac{F}{2^{p}}\right)}_{\text{significand}}\\[6pt] &=(-1)^{s}\times2^{\max(E,1)-\text{bias}}\times\left(\min(E,1)+\frac{F}{2^{p}}\right) \end{aligned}

Here xx is the stored number, ss the sign cell, EE and FF the whole numbers in the exponent and fraction fields, and p=3p=3 the number of fraction cells in the tiny format.
The two named quantities are:

  • e=max⁡(E,1)−biase=\max(E,1)-\text{bias} is the exponent that is actually used: E−biasE-\text{bias} for normal numbers, and 1−bias1-\text{bias} when E=0E=0⁠. Here max⁡(E,1)\max(E,1) is the larger of EE and 11⁠. It is the same e=E−biase=E-\text{bias} as before, now extended to E=0E=0⁠;
  • h=min⁡(E,1)h=\min(E,1) is the leading cell of the significand: 11 when E≠0E\neq0 and 00 when E=0E=0⁠. Here min⁡(E,1)\min(E,1) is the smaller of EE and 11⁠. The letter comes from hidden bit: the leading cell is not stored in any of the cells, its value is fixed by the exponent field.

The words under the factors name their roles: the sign, the power of two and the significand.

Placing the values of the tiny format, with p=3p=3 and bias 33⁠, for two patterns of cells:

  • the cells 0 011 101 have E=3E=3 and F=5F=5⁠;
  • the cells 0 000 101 have E=0E=0 and F=5F=5⁠.
2max⁡(3,1)−3(1+58)=138=1.6252max⁡(0,1)−3(0+58)=532\begin{aligned} 2^{\max(3,1)-3}\left(1+\frac58\right)&=\frac{13}{8}=1.625\\[4pt] 2^{\max(0,1)-3}\left(0+\frac58\right)&=\frac{5}{32} \end{aligned}

The hardware makes both choices with one test: are all the exponent cells 00⁠?
If at least one of them is 11⁠, then h=1h=1 and e=E−biase=E-\text{bias}⁠. If all of them are 00⁠, then h=0h=0 and e=1−biase=1-\text{bias}⁠.
So the two kinds of numbers are computed by one mechanism, a choice of the leading cell and of the exponent that is used, and not by two separate formulas.

The remaining value of the exponent field, E=7E=7⁠, holds no number:
with F=0F=0 it is infinity, and the other seven values of FF are NaN (Not a Number), the result of an operation with no value, such as zero divided by zero.
These are the eight combinations that E=7E=7 gives, so they are not counted as values.

One way to see the bias: a split into two halves

This is our own explanation of why the bias has this value, a design rationale and not a statement quoted from the IEEE 754 standard.

Each value of EE covers an interval between two consecutive powers of 2.
The reason: for a fixed ee the numbers are 2e×1.f2^{e}\times1.f⁠, and the multiplier 1.f1.f runs from 11 up to just below 22⁠.
In the tiny format, with bias 33⁠:

EEeePositive values obtained
00−2-2⁠, as for E=1E=1zero and numbers below 0.250.25
11−2-2from 0.250.25 up to below 0.50.5
22−1-1from 0.50.5 up to below 11
3300from 11 up to below 22
4411from 22 up to below 44
5522from 44 up to below 88
6633from 88 up to below 1616
77infinity and NaN

The bias 2k−1−12^{k-1}-1 splits the possible values of EE into two equal halves:
E=0,1,2,3E=0,1,2,3 give e≤0e\le0⁠, that is numbers whose absolute value is below 22⁠, and E=4,5,6,7E=4,5,6,7 give e>0e>0⁠, that is 22 or more.
In general the first half is the first 2k−12^{k-1} values, from 00 up to the bias.
The split is around 22⁠, not around 11⁠, because e=0e=0 covers the interval from 11 to 22⁠.
Splitting around 11 (that is e<0e<0 versus e≥0e\ge0⁠) would not give equal halves:
three values of EE against five. The sign cell is separate, so the same split holds for negative numbers by absolute value.

Now use the format yourself. Work it out: which number does the pattern 0 101 100 store?
Decode it with the formula (exponent field 101, bias 3), then set those bits in the explorer to check.
Stepping with the buttons shows what happens to the gap when the exponent changes.

The interactive explorer needs JavaScript. The figure above shows the same numbers.

IEEE 754: the format real computers use

Almost every computer follows the IEEE 754 standard of the Institute of Electrical and Electronics Engineers, first published in 1985 and updated since.
On almost all computers Python’s float is the 64-bit format, and NumPy offers the others.

FormatBits: s / e / f (sign, exponent, fraction)Bias
binary16 (half precision)1 / 5 / 1015
binary32 (single precision)1 / 8 / 23127
binary64 (double precision)1 / 11 / 521023

The exponent is stored as a non-negative integer EE⁠, the bias is added so that no sign bit is needed for the exponent, and the true exponent is e=E−biase=E-\text{bias}⁠.
For a normal binary64 number, with FF the integer value of the 52 fraction bits:

x=(−1)s×2E−1023×(1+F252)x=(-1)^{s}\times 2^{E-1023}\times\left(1+\frac{F}{2^{52}}\right)

Worked example: 85.125. In binary:

85.125=1010101.0012=26×1.010101001285.125=1010101.001_2=2^{6}\times1.010101001_2

The exponent is 6, so the stored exponent is 127+6=133=100001012127+6=133=10000101_2 in binary32 and 1023+6=1029=1000000010121023+6=1029=10000000101_2 in binary64.
The fraction bits are 010101001 followed by zeros. In binary32 the result is (sign, exponent, fraction):

0 10000101 01010100100000000000000

The exponent field also has reserved patterns:

Exponent fieldFraction fieldMeaning
all zeroszerothe number zero (there is a +0 and a −0)
all zerosnot zerosubnormal number: very small, with fewer significant bits
all oneszeroinfinity (positive or negative)
all onesnot zeroNaN, “not a number”
anything elseanya normal number

The rows for zero, subnormal numbers and normal numbers are one formula, the formula of the tiny format with the binary64 values:
bias 10231023 and p=52p=52 fraction cells, with FF the integer value of the fraction field.
For every exponent field except all ones

x=(−1)s⏟sign×2e⏟power of two×(h+F252)⏟significand=(−1)s×2max⁡(E,1)−1023×(min⁡(E,1)+F252)\begin{aligned} x&=\underbrace{(-1)^{s}}_{\text{sign}}\times\underbrace{2^{e}}_{\text{power of two}}\times\underbrace{\left(h+\frac{F}{2^{52}}\right)}_{\text{significand}}\\[6pt] &=(-1)^{s}\times2^{\max(E,1)-1023}\times\left(\min(E,1)+\frac{F}{2^{52}}\right) \end{aligned}

Here e=max⁡(E,1)−1023e=\max(E,1)-1023 is the exponent that is used (E−1023E-1023 for normal numbers, −1022-1022 when E=0E=0⁠), and h=min⁡(E,1)h=\min(E,1) is the hidden bit, the same two quantities as in the tiny format.
The test that sets hh is whether all 11 exponent cells are zero.

Infinity results from an overflow, a number too large for the format.
A result too small for the format is an underflow: it becomes a subnormal number or zero.
NaN is the result of an undefined operation such as infinity minus infinity.

The range of binary64

The binary64 formula gives the smallest numbers directly. The smallest positive normal number has E=1E=1⁠, F=0F=0 and h=1h=1⁠.
The smallest positive subnormal number has E=0E=0⁠, F=1F=1 and h=0h=0⁠, and in both cases e=max⁡(E,1)−1023=−1022e=\max(E,1)-1023=-1022⁠:

(E,F)=(1,0): 2−1022(1+0252)=2−1022(E,F)=(0,1): 2−1022(0+1252)=2−1022−52=2−1074\begin{aligned} (E,F)=(1,0):\ & 2^{-1022}\left(1+\frac{0}{2^{52}}\right)\\ &=2^{-1022}\\[8pt] (E,F)=(0,1):\ & 2^{-1022}\left(0+\frac{1}{2^{52}}\right)\\ &=2^{-1022-52}=2^{-1074} \end{aligned}

The largest subnormal number, with F=252−1F=2^{52}-1⁠, is 2−1022(1−2−52)2^{-1022}\left(1-2^{-52}\right)⁠: exactly 2−10742^{-1074} below 2−10222^{-1022}⁠, so binary64 has no hole next to zero either.
The range table follows from the binary64 formula.

QuantityValue
largest finite numberabout 1.8×103081.8\times10^{308}
smallest positive normal numberabout 2.2×10−3082.2\times10^{-308} (2−10222^{-1022}⁠)
smallest positive subnormal numberabout 4.9×10−3244.9\times10^{-324} (2−10742^{-1074}⁠)

A result above the first row overflows to infinity, which then spreads through later calculations.
A result smaller than about half of the last row underflows to zero, silently:
a contribution that small is simply dropped from a sum.

Absolute error and relative error

We define a measure of the distance between a given number and a desired one.
Let xx be the true value and x^\hat x the stored or approximate one.
The absolute error is ∣x−x^∣|x-\hat x|⁠.

Which approximation is better? In the first the number is 1200012000 and the absolute error is 11⁠.
In the second the number is 0.0000120.000012 and the absolute error is 0.0000010.000001⁠.
Decide, and give your reason in one sentence.

Solution

Measure each error as a fraction of the size of the number. In the first approximation the fraction is

112000≈0.0083%,\frac{1}{12000}\approx0.0083\%,

and in the second it is

0.0000010.000012≈8.3%\frac{0.000001}{0.000012}\approx8.3\%

The second distance is much smaller than the first, but as a fraction of the size of the number it is 1000 times larger.
The first approximation is better.

The measure in the solution, the error as a fraction of the size of the number, is the relative error:
∣x−x^∣∣x∣\frac{|x-\hat x|}{|x|} (for x≠0x\neq0⁠). For example, comparing 0.3330.333 with 13\tfrac13 gives an absolute error of 13000≈3.3×10−4\tfrac{1}{3000}\approx3.3\times10^{-4} and relative error exactly 10−310^{-3}⁠.

Bound it yourself: a number x=d.ddd…×10ex=d.ddd\ldots\times10^{e} is rounded to kk significant digits, giving x^\hat x⁠.
What is the largest possible absolute error, and what does it say about the relative error?

Hint

Rounding changes the number by at most half a unit of the last digit that is kept.
What is that unit worth, and what is the smallest value that ∣x∣|x| can take?

The bound

Now the claim about significant digits can be stated exactly.
If we round a number x=d.ddd…×10ex=d.ddd\ldots\times10^{e} to kk significant digits and get x^\hat x⁠, the absolute error is at most half a unit in the kk-th digit, that is 12⋅10 e−k+1\tfrac12\cdot10^{\,e-k+1}⁠.
Since ∣x∣≥10e|x|\ge10^{e}⁠, the relative error is bounded:

∣x−x^∣∣x∣ ≤ 12⋅10 1−k = 5×10−k\frac{|x-\hat x|}{|x|}\ \le\ \frac12\cdot10^{\,1-k}\ =\ 5\times10^{-k}

The bound depends only on kk⁠, not on the size of the number.

An example with k=3k=3⁠: round 3.141593.14159 to 3.143.14 and 31415.931415.9 to 3140031400⁠.
The formula gives both the same bound:

∣x−x^∣∣x∣ ≤ 5×10−3=0.005\frac{|x-\hat x|}{|x|}\ \le\ 5\times10^{-3}=0.005

The absolute errors are very different: 0.001590.00159 for the first and 15.915.9 for the second, 10,000 times larger.
The relative errors are about the same, and both are below the bound:

0.001593.14159≈0.00051,15.931415.9≈0.00051\frac{0.00159}{3.14159}\approx0.00051,\qquad \frac{15.9}{31415.9}\approx0.00051

The tiny format shows three facts, and they hold for real formats too.

  1. Only finitely many numbers exist. Everything else is replaced by a nearby stored number.
  2. The (absolute) gap between neighbours grows with the size of the numbers: for normal numbers it is constant between two consecutive powers of 2 and doubles at each power of 2; the subnormal numbers have the same gap as the smallest normal numbers.
  3. For ordinary numbers, the relative gap, that is, the gap divided by the number, stays within fixed limits. In the tiny format it is always between 116\tfrac{1}{16} and 18\tfrac{1}{8}⁠: the gap is 18\tfrac18 at 1 and 1 at 8, which is 18\tfrac18 of the number both times.

The gap between 1 and the next larger stored number is called machine epsilon, written εmach\varepsilon_{\text{mach}}⁠.
In the tiny format it equals 2−3=182^{-3}=\frac18⁠. Near 1 the gap and the relative gap are equal, because we divide by 1; so machine epsilon is also the relative gap near 1, and it is the largest of the relative gaps of normal numbers (the relative gap of subnormal numbers is larger).

Try: in the tiny format, what is the number right after 4, and the number right after 0.5?
Use the number line of the tiny format, then confirm with its Next number button.
The line is in the section “From bits to a floating-point number”.

Solution

From the number line, the gap between 4 and 8 is 12\tfrac12⁠, so the number after 4 is 4.5.
The gap between 0.5 and 1 is 116\tfrac1{16}⁠, so the number after 0.5 is 0.5625.

Back to the opening question. The binary expansions of 0.1, 0.2 and 0.3 never end, so binary64 stores a nearby number for each of them (with 52 fraction bits).
The computer adds the two stored numbers and rounds the sum to a stored number again, and this number differs from the stored 0.3, so 0.1 + 0.2 == 0.3 is False.
The exact stored values, the exact sum and the tie that decides the result are in the lecture Back to the Question: Rounding and Machine Epsilon.

Continue learning

The next step is the lecture Back to the Question: Rounding and Machine Epsilon.
It answers the opening question with exact numbers and shows how large the rounding error can be.
The practice session comes after it.