# Floating-point Numbers Aren't Real

### From WikiContent

Line 6: | Line 6: | ||

Knowing the spacing in the neighborhood of a floating-point number can help you avoid classic numerical blunders. For example, if you're performing an iterative calculation, such as searching for the root of an equation, there's no sense in asking for greater precision than the nu | Knowing the spacing in the neighborhood of a floating-point number can help you avoid classic numerical blunders. For example, if you're performing an iterative calculation, such as searching for the root of an equation, there's no sense in asking for greater precision than the nu | ||

- | |||

mber system can give in the neighborhood of the answer. Make sure that the tolerance you request is no smaller than the spacing there; otherwise you'll loop forever. | mber system can give in the neighborhood of the answer. Make sure that the tolerance you request is no smaller than the spacing there; otherwise you'll loop forever. | ||

- | Since floating-point numbers are approximations of real numbers, there is inevitably a little error present. This error, called | + | Since floating-point numbers are approximations of real numbers, there is inevitably a little error present. This error, called ''roundoff'', can lead to surprising results. When you subtract nearly equal numbers, for example, the most significant digits cancel each other out, so what was the least significant digit (where the roundoff error resides) gets promoted to the most significant position in the floating-point result, essentially contaminating any further related computations (a phenomenon known as ''smearing''). You need to look closely at your algorithms to prevent such "catastrophic cancellation". To illustrate, consider solving the equation <code>x<sup>2</sup> - 100000x + 1</code> with the quadratic formula. Since the operands in the expression <code>-b + sqrt(b<sup>2</sup?> - 4)</code> are nearly equal in magnitude, you can instead compute the root <code>r<sub>1</sub> = -b - sqrt(b<sup>2</sup> - 4)</code>, and then obtain <code>r<sub>2</sub> = 1/r<sub>1</sub></code>, since for any quadratic, <code>ax<sup>2</sup> + bx + c, r<sub>1</sub>r<sub>2</sub> = c</code>. |

By [[Chuck Allison]] | By [[Chuck Allison]] |

## Revision as of 19:27, 2 January 2009

Floating-point numbers are not "real numbers" in the mathematical sense, even though they are called *real* in some programming languages, such as Pascal and Fortran. Real numbers have infinite precision and are therefore continuous and non-lossy; floating-point numbers have limited precision, so they are finite, and they resemble "badly-behaved" integers, because they're not evenly spaced throughout the range.

To illustrate, assign 2147483647 (the largest signed 32-bit integer) to a 32-bit `float`

variable (`x`

, say), and print it. You'll see 2147483648. Now print `x - 64`

. Still 2147483648. Now print `x-65`

and you'll get 2147483520! Why? Because the spacing between adjacent floats in that range is 128, and floating-point operations round to the nearest floating-point number.

IEEE floating-point numbers are fixed-precision numbers based on base-two scientific notation: 1.d_{1}d_{2}...d_{p-1} × 2^{e}, where *p* is the precision (24 for `float`

, 53 for `double`

). The spacing between two consecutive numbers is 2^{1-p+e}, which can be safely approximated by ε|x|, where ε is the *machine epsilon* (2^{1-p}).

Knowing the spacing in the neighborhood of a floating-point number can help you avoid classic numerical blunders. For example, if you're performing an iterative calculation, such as searching for the root of an equation, there's no sense in asking for greater precision than the nu mber system can give in the neighborhood of the answer. Make sure that the tolerance you request is no smaller than the spacing there; otherwise you'll loop forever.

Since floating-point numbers are approximations of real numbers, there is inevitably a little error present. This error, called *roundoff*, can lead to surprising results. When you subtract nearly equal numbers, for example, the most significant digits cancel each other out, so what was the least significant digit (where the roundoff error resides) gets promoted to the most significant position in the floating-point result, essentially contaminating any further related computations (a phenomenon known as *smearing*). You need to look closely at your algorithms to prevent such "catastrophic cancellation". To illustrate, consider solving the equation `x`

with the quadratic formula. Since the operands in the expression ^{2} - 100000x + 1`-b + sqrt(b`

are nearly equal in magnitude, you can instead compute the root ^{2} - 4)`r`

, and then obtain _{1} = -b - sqrt(b^{2} - 4)`r`

, since for any quadratic, _{2} = 1/r_{1}`ax`

.
^{2} + bx + c, r_{1}r_{2} = c

This work is licensed under a Creative Commons Attribution 3

Back to 97 Things Every Programmer Should Know home page