Variable Type float and double

  1. These types are used to store numbers that have a fractional part

  2. Since a computer can only use a finite number of bits to represent these numbers, the representation is in general only approximate

    For example, a number gotten by 2.0/3.0 = 0.666... cannot be represented exactly. So is the number 0.1, because of the binary nature of the bits used for storage.

  3. Floats and doubles can be written in fixed point or scientific notation in the input or in the output

    For example, the number 39.14658 is in fixed point notation. It appears as 3.914658E1 in scientific notation

  4. A float or a double in C++ will automatically be displayed in scientific notation if the number is either very small or very large. To specify that the output must be in fixed point notation, use the member function setf to set a flag for fixed point format before sending the result to cout for output:

    cout.setf(ios::fixed);

    If a float or a double happens to have no fractional part, the number will be displayed looking like an integer, without the decimal point. For example 3.000 will be displayed as 3. You can specify that the decimal point must always appear with the statement:

    cout.setf(ios::showpoint);

    Multiple flags can be set by separating them with the | operator:

    cout.setf(ios::fixed | ios::showpoint);

    The number of decimal places to be displayed can be specified using

    cout.precision(n);

    where n specifies the number of decimal places to be displayed.