Variables of type float
represent single-precision floating-point numbers and have approximately seven significant digits on most of today’s systems. Variables of type double
represent double-precision floating-point numbers. These require twice as much memory as float
variables and provide approximately 15 significant digits on most of today’s systems—approximately double the precision of float
variables. Most programmers represent floating-point numbers with type double
. In fact, C++ treats all floating-point numbers you type in a program’s source code (such as 7.33 and 0.0975) as double
values by default. Such values in the source code are known as floating-point literals. See Appendix C, Fundamental Types, for the ranges of values for float
s and double
s.
In conventional arithmetic, floating-point numbers often arise as a result of division—when we divide 10 by 3, the result is 3.3333333..., with the sequence of 3s repeating infinitely. The computer allocates only a fixed amount of space to hold such a value, so clearly the stored floating-point value can be only an approximation.
Due to the imprecise nature of floating-point numbers, type double
is preferred over type float
, because double
variables can represent floating-point numbers more accurately. For this reason, we use type double
throughout the book.