C++ integral types, such as
C++ provides several data types for storing floating-point numbers in memory,
including The following example demonstrates proper way of expressing division of one whole number by another by converting expression from integral type to floating-point type: int numerator = 5; int denominator = 3; double ratio = static_cast< double >( numerator ) / denominator; |
Variables of type float represent single-precision floating-point numbers and have seven significant digits on most 32-bit systems today. Variables of type double represent double-precision floating-point numbers. They require twice as much memory as float variables and provide 15 significant digits on most 32-bit systems, and improve precision of floating-point variables by approximately magnitude of 2, hence the name of the type: double. For the range of values required by most programs, variables of type float should suffice, but you can use double to "play it safe." In some programs, even variables of type double will be inadequate. Such programs are beyond the scope of this course. Most programmers represent floating-point numbers with type double. In fact, C++ treats all floating-point numbers that you type in a program's source code (such as 7.33 and 0.0975) as doubles by default. Such values in the source code are known as floating-point constants. A floating-point literal, such as 7.33 or 0.0975, by default is of type double. Floating-point numbers often arise as a result of division. In conventional arithmetic, 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 viewed as an approximation. Common programming error occurs when using floating-point numbers in a manner that assumes they are represented exactly, e.g. using them in comparisons for equality. This can often lead to incorrect results. Floating-point numbers are represented only approximately by most computers, whereas equality comparisons attempt a precise match. 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 shall use type double throughout this course. |