Floating-Point Arithmetic

Floating-point arithmetic is how computers store and compute with real numbers: each number is kept as a sign, a fixed number of significant digits and an exponent, like scientific notation in base 2. Since only finitely many digits fit, most numbers are rounded when stored, and every operation can add a small round-off error.

Before this lesson: Absolute and Relative Error, Scientific Notation and Exponent Rules

Scientific notation with a fixed number of digits

Picture a calculator that can show only 4 significant digits. It can store 3.142 or 0.0006667, but not \pi or \frac{2}{3} exactly. Every number gets squeezed into the form \pm d_1.d_2d_3d_4 \times 10^e, and whatever does not fit is lost.

Real computers do the same thing in base 2. A floating-point number has three parts: a sign, a significand (also called the mantissa) with a fixed number of digits, and an exponent that slides the point left or right. The point “floats” to wherever the leading digit is, which is where the name comes from.

Because the number of digits is fixed, the gaps between floating-point numbers grow with their size. Between 1 and 2 they are tiny; between 10^{15} and 10^{16} they are much wider. The relative spacing, though, stays about the same everywhere.

Number line from 0.5 to 8 with a tick at every number in a small binary floating-point system that keeps 4 significant bits. Between 0.5 and 1 the ticks are 0.0625 apart, between 1 and 2 they are 0.125 apart, between 2 and 4 they are 0.25 apart, and between 4 and 8 they are 0.5 apart. Each time the numbers double, the spacing doubles too.
With a fixed number of significant bits, the gap between neighboring floating-point numbers doubles each time the numbers double.

Rounding and chopping

Let \operatorname{fl}(x) be the floating-point number stored for x. With k significant decimal digits there are two common rules:

  • Chopping drops every digit after the kth. Its relative error is at most 10^{1-k}.
  • Rounding rounds to the nearest k-digit number. Its relative error is at most \frac{1}{2} \cdot 10^{1-k}, half as much.

The bound for rounding is called the unit roundoff u. Every stored number satisfies

\operatorname{fl}(x) = x(1 + \delta), \qquad |\delta| \le u

so storing a number never changes it by more than the relative amount u. The same model holds for each arithmetic step: the computer finds the exact result of x + y, x - y, xy or x/y, then rounds it. That means \operatorname{fl}(x + y) = (x + y)(1 + \delta) with |\delta| \le u.

IEEE 754 double precision

Almost every computer today follows the IEEE 754 standard. Its 64-bit double precision format, used by Python, JavaScript, Excel and MATLAB, stores a number as \pm 1.f \times 2^{e}.

PartDouble precision (64 bits)Single precision (32 bits)
Sign1 bit1 bit
Exponent11 bits8 bits
Fraction f52 bits (53 significant bits with the hidden leading 1)23 bits (24 significant bits)
Machine epsilon \varepsilon = 2^{1-p}2^{-52} \approx 2.22 \times 10^{-16}2^{-23} \approx 1.19 \times 10^{-7}
Unit roundoff u = 2^{-p}2^{-53} \approx 1.11 \times 10^{-16}2^{-24} \approx 5.96 \times 10^{-8}
Decimal digits, about p \log_{10} 2about 16about 7
Largest numberabout 1.80 \times 10^{308}about 3.40 \times 10^{38}

Here p is the number of significant bits. Machine epsilon \varepsilon is the gap between 1 and the next larger floating-point number. With round-to-nearest, the unit roundoff is half of it.

How to do floating-point arithmetic by hand

  1. Write each number in scientific notation and round (or chop) it to k significant digits.
  2. Do the exact operation on the stored numbers. For addition, line up the decimal points first.
  3. Round (or chop) the result to k significant digits.
  4. Repeat for each later operation, rounding after every step.
  5. Compare with the exact answer to find the round-off error.

Floating-point arithmetic examples

Example 1: Storing π with 4 digits

Find \operatorname{fl}(\pi) with 4-digit chopping and with 4-digit rounding, and the relative error of each.

\pi = 3.14159\ldots

Write the digits of \pi.

\operatorname{fl}_{\text{chop}}(\pi) = 3.141

Chop: drop every digit after the 4th.

\frac{|\pi - 3.141|}{\pi} \approx \frac{0.000593}{3.14159}

Set up the relative error (see absolute and relative error).

\approx 1.89 \times 10^{-4}

Divide.

\operatorname{fl}_{\text{round}}(\pi) = 3.142

Round: the 5th digit is 5, so round up.

\frac{|\pi - 3.142|}{\pi} \approx \frac{0.000407}{3.14159}

Set up the relative error.

\approx 1.30 \times 10^{-4}

Divide.

Answer: Chopping gives 3.141 with relative error about 1.89 \times 10^{-4}, under its bound 10^{-3}. Rounding gives 3.142 with relative error about 1.30 \times 10^{-4}, under the unit roundoff 5 \times 10^{-4}.

Example 2: Adding numbers of different sizes

Use 4-digit rounding to find 1234 + 0.5678.

1234 = 1.234 \times 10^3,\quad 0.5678 = 5.678 \times 10^{-1}

Both inputs already have 4 digits.

1234 + 0.5678 = 1234.5678

Add exactly, lining up the decimal points.

\operatorname{fl}(1234.5678) = 1235

Round to 4 significant digits: the 5th digit is 5, so round up.

|1234.5678 - 1235| = 0.4322

Find the absolute error.

\frac{0.4322}{1234.5678} \approx 3.50 \times 10^{-4}

Find the relative error.

Answer: The computer stores 1235. Most of the digits of 0.5678 were lost, but the relative error 3.50 \times 10^{-4} is still under u = 5 \times 10^{-4}. Adding a much smaller number can lose its digits entirely: in 4-digit arithmetic, \operatorname{fl}(1000 + 0.4) = 1000.

Example 3: The order of addition matters

With 4-digit rounding, compute (1000 + 0.4) + 0.4 and 1000 + (0.4 + 0.4). The exact sum is 1000.8.

1000 + 0.4 = 1000.4

Add exactly.

\operatorname{fl}(1000.4) = 1000

Round to 4 digits: the 0.4 is lost.

1000 + 0.4 = 1000.4

Add the second 0.4 exactly.

\operatorname{fl}(1000.4) = 1000

Round again: lost again.

0.4 + 0.4 = 0.8

In the other order, add the small numbers first.

1000 + 0.8 = 1000.8

Add exactly.

\operatorname{fl}(1000.8) = 1001

Round to 4 digits.

Answer: The first order gives 1000 and the second gives 1001, which is closer to 1000.8. Floating-point addition is not associative. A good habit is to add the smallest numbers first.

Example 4: Why 0.1 + 0.2 is not 0.3 on a computer

Many programmers are surprised that a computer says 0.1 + 0.2 is not equal to 0.3. Find the binary form of 0.1 to see why.

0.1 \times 2 = 0.2

Multiply the fraction by 2. The integer part, 0, is the first bit.

0.2 \times 2 = 0.4

Second bit 0.

0.4 \times 2 = 0.8

Third bit 0.

0.8 \times 2 = 1.6

Fourth bit 1. Keep only the fraction 0.6.

0.6 \times 2 = 1.2

Fifth bit 1. Keep the fraction 0.2.

0.2 \times 2 = 0.4

This repeats the second line, so the bits repeat.

0.1 = 0.0\overline{0011}_2

Write the repeating binary expansion.

\operatorname{fl}(0.1) = 0.1000000000000000055\ldots

Double precision keeps 53 bits and rounds.

\operatorname{fl}(0.2) = 0.2000000000000000111\ldots

0.2 repeats in binary too.

\operatorname{fl}(0.3) = 0.2999999999999999888\ldots

0.3 rounds down.

\operatorname{fl}(\operatorname{fl}(0.1) + \operatorname{fl}(0.2)) = 0.3000000000000000444\ldots

Add the stored values, then round the sum to 53 bits.

Answer: 0.1, 0.2 and 0.3 are all repeating fractions in binary, so none is stored exactly. The stored sum lands on the floating-point number just above 0.3, while 0.3 itself is stored just below it. The two differ by 2^{-54} \approx 5.55 \times 10^{-17}, the gap between neighboring doubles near 0.3. To compare computed numbers, test whether |a - b| is smaller than a tolerance instead of testing whether they are equal.

Example 5: Finding machine epsilon

Machine epsilon can be found by halving a number until adding it to 1 no longer changes 1. The table follows this test in double precision.

Step\varepsilonIs \operatorname{fl}(1 + \varepsilon) \gt 1?
12^{-50} \approx 8.88 \times 10^{-16}Yes
22^{-51} \approx 4.44 \times 10^{-16}Yes
32^{-52} \approx 2.22 \times 10^{-16}Yes: 1 + 2^{-52} is the next double after 1
42^{-53} \approx 1.11 \times 10^{-16}No: 1 + 2^{-53} is a tie, and the tie rounds to 1
1 = 1.\underbrace{00\cdots0}_{52}{}_2

Write 1 with the 52 stored fraction bits.

1 + 2^{-52} = 1.\underbrace{00\cdots01}_{52}{}_2

Adding 2^{-52} sets the last stored bit, so the sum fits.

1 + 2^{-53} = 1.\underbrace{00\cdots0}_{52}1_2

Adding 2^{-53} needs a 53rd fraction bit, which does not fit.

\operatorname{fl}(1 + 2^{-53}) = 1

It is exactly halfway, and IEEE ties round to the neighbor whose last bit is 0.

Answer: The last \varepsilon that changes 1 is 2^{-52} \approx 2.22 \times 10^{-16}, the machine epsilon of double precision. The unit roundoff is half of it, 2^{-53} \approx 1.11 \times 10^{-16}, so a double carries about 16 significant decimal digits.

Common mistakes

MistakeExample of the errorFix
Thinking short decimals are stored exactlyAssuming 0.1 is exact in a computer0.1 repeats in binary. Only fractions with a power of 2 in the denominator, like 0.5 or 0.625, are exact.
Testing computed numbers for equalityChecking whether 0.1 + 0.2 = 0.3Check whether |a - b| is less than a small tolerance.
Confusing machine epsilon with the smallest numberSaying no double is smaller than 2.22 \times 10^{-16}Machine epsilon is a relative gap near 1. Doubles go down to about 2.2 \times 10^{-308}.
Rounding only at the endDoing every step of Example 3 exactly and rounding onceA computer rounds after every operation, so round after each step.
Assuming the order of operations does not matterExpecting (1000 + 0.4) + 0.4 to equal 1000 + (0.4 + 0.4)Floating-point addition is not associative. Add small numbers first.
Counting bits as decimal digitsSaying a double has 52 or 53 decimal digitsIt has 53 bits, which is about 53 \log_{10} 2 \approx 16 decimal digits.

Related ideas

Error growth. Each operation adds a relative error of at most u, so a sum of n numbers can pick up an error of roughly nu in the worst case. Usually the errors partly cancel, and the growth is closer to \sqrt{n}\,u.

Overflow and underflow. A result larger than the largest double, about 1.8 \times 10^{308}, overflows to infinity. A result smaller than about 2.2 \times 10^{-308} underflows toward 0, first through less precise subnormal numbers.

Subtraction. Subtracting two nearly equal floating-point numbers is exact or close to it, but it can expose the round-off already in them and wipe out most correct digits. The next lesson, Loss of Significance, shows how to rewrite formulas to avoid this.

Practice problems

  1. Find \operatorname{fl}\left(\frac{2}{3}\right) with 4-digit chopping and with 4-digit rounding, and the relative error of each.
  2. Use 4-digit rounding to find 12.34 + 0.05678.
  3. Use 4-digit rounding to find 9.999 + 0.002.
  4. With 4-digit rounding, compute (5000 + 0.3) + 0.3 and 5000 + (0.3 + 0.3).
  5. Find the unit roundoff for 6-digit decimal rounding.
  6. Single precision keeps 24 significant bits. Find its machine epsilon, its unit roundoff and about how many decimal digits it carries.
  7. Write 0.625 in binary. Is it stored exactly?
  8. Write 0.2 in binary. Is it stored exactly?
  9. About how large can the absolute error be when 123456.789 is stored in double precision?
  10. With 4-digit rounding, find \frac{1}{3} + \frac{1}{3} + \frac{1}{3} by storing \frac{1}{3} and adding from left to right. What is the error?
Show answers

1.

\frac{2}{3} = 0.666666\ldots

Write the digits.

\operatorname{fl}_{\text{chop}} = 0.6666

Chop after the 4th digit.

\frac{|2/3 - 0.6666|}{2/3} \approx 1.0 \times 10^{-4}

Divide the gap 0.0000667 by \frac{2}{3}.

\operatorname{fl}_{\text{round}} = 0.6667

Round: the 5th digit is 6, so round up.

\frac{|2/3 - 0.6667|}{2/3} \approx 5.0 \times 10^{-5}

Divide the gap 0.0000333 by \frac{2}{3}.

Answer: 0.6666 with relative error about 10^{-4}; 0.6667 with relative error about 5 \times 10^{-5}

2.

12.34 + 0.05678 = 12.39678

Add exactly.

\operatorname{fl}(12.39678) = 12.40

Round to 4 significant digits.

Answer: 12.40

3.

9.999 + 0.002 = 10.001

Add exactly.

\operatorname{fl}(10.001) = 10.00

Round to 4 significant digits.

Answer: 10.00 (the 0.001 is lost)

4.

5000 + 0.3 = 5000.3

Add exactly.

\operatorname{fl}(5000.3) = 5000

Round to 4 digits: the 0.3 is lost.

5000 + 0.3 = 5000.3

Add the second 0.3 exactly.

\operatorname{fl}(5000.3) = 5000

Round: the 0.3 is lost again.

0.3 + 0.3 = 0.6

In the other order, add the small numbers first.

5000 + 0.6 = 5000.6

Add exactly.

\operatorname{fl}(5000.6) = 5001

Round up to 4 digits.

Answer: 5000 and 5001; the exact sum is 5000.6, so the second order is better.

5.

u = \frac{1}{2} \cdot 10^{1-6}

Use u = \frac{1}{2} \cdot 10^{1-k} with k = 6.

= 5 \times 10^{-6}

Simplify.

Answer: u = 5 \times 10^{-6}

6.

\varepsilon = 2^{1-24} = 2^{-23} \approx 1.19 \times 10^{-7}

Machine epsilon 2^{1-p} with p = 24.

u = 2^{-24} \approx 5.96 \times 10^{-8}

Unit roundoff 2^{-p}.

24 \log_{10} 2 \approx 7.2

Convert bits to decimal digits.

Answer: \varepsilon \approx 1.19 \times 10^{-7}, u \approx 5.96 \times 10^{-8}, about 7 decimal digits

7.

0.625 \times 2 = 1.25

First bit 1. Keep 0.25.

0.25 \times 2 = 0.5

Second bit 0.

0.5 \times 2 = 1.0

Third bit 1. The fraction is now 0, so stop.

0.625 = 0.101_2

Read the bits: \frac{1}{2} + \frac{1}{8} = 0.625.

Answer: 0.101_2, which ends, so 0.625 is stored exactly.

8.

0.2 \times 2 = 0.4,\quad 0.4 \times 2 = 0.8

First two bits 0, 0.

0.8 \times 2 = 1.6,\quad 0.6 \times 2 = 1.2

Next two bits 1, 1. The fraction is back to 0.2.

0.2 = 0.\overline{0011}_2

The pattern repeats forever.

Answer: 0.\overline{0011}_2; it repeats, so 0.2 is rounded when stored.

9.

|x - \operatorname{fl}(x)| \le u|x|

Use \operatorname{fl}(x) = x(1 + \delta) with |\delta| \le u.

\approx 1.11 \times 10^{-16} \cdot 123456.789

Substitute u = 2^{-53}.

\approx 1.37 \times 10^{-11}

Multiply.

Answer: at most about 1.37 \times 10^{-11}

10.

\operatorname{fl}\left(\frac{1}{3}\right) = 0.3333

Store \frac{1}{3} with 4 digits.

0.3333 + 0.3333 = 0.6666

Add. The result already has 4 digits.

0.6666 + 0.3333 = 0.9999

Add again.

|1 - 0.9999| = 0.0001

Compare with the exact sum, 1.

Answer: 0.9999, with an error of 0.0001

FAQ

What is machine epsilon?

Machine epsilon is the distance from 1 to the next larger floating-point number. In double precision it is 2^{-52} \approx 2.22 \times 10^{-16}, and the largest relative error from rounding a number is half of that.

Why does 0.1 + 0.2 not equal 0.3 on a computer?

In binary, 0.1, 0.2 and 0.3 are repeating fractions, so each is rounded when stored. The rounded 0.1 plus the rounded 0.2 rounds to 0.30000000000000004, while 0.3 is stored as a number just below 0.3.

How many digits does a double have?

A double keeps 53 significant bits, which is about 15 to 17 significant decimal digits. A good rule of thumb is 16.

What is the difference between rounding and chopping?

Chopping drops the extra digits, and rounding goes to the nearest k-digit number. Rounding has half the maximum relative error, \frac{1}{2} \cdot 10^{1-k} instead of 10^{1-k}, and it is what IEEE 754 uses by default.

Summary

  • A floating-point number is a sign, a significand with a fixed number of digits and an exponent.
  • Storing a number gives \operatorname{fl}(x) = x(1 + \delta) with |\delta| \le u, and every operation is rounded the same way.
  • With k-digit decimal rounding, u = \frac{1}{2} \cdot 10^{1-k}; chopping has twice that bound.
  • IEEE double precision has 53 significant bits, machine epsilon 2^{-52} \approx 2.22 \times 10^{-16} and about 16 decimal digits.
  • Floating-point addition is not associative, and most decimals, like 0.1, are not stored exactly.

Previous lesson: Absolute and Relative Error

Next lesson: Loss of Significance

Back to the Numerical Methods Index