## 2.3. Floating-Point TypesC also includes special numeric types that can represent nonintegers with a decimal point in any position. The standard float For variables with single precision double For variables with double precision long double For variables with extended precision
A floating-point value can be stored only with a limited precision, which is determined by the binary format used to represent it and the amount of memory used to store it. The precision is expressed as a number of significant digits. For example, a "precision of six decimal digits" or "six-digit precision" means that the type's binary representation is precise enough to store a real number of six decimal digits, so that its conversion back into a six-digit decimal number yields the original six digits. The position of the decimal point does not matter, and leading and trailing zeros are not counted in the six digits. The numbers 123,456,000 and 0.00123456 can both be stored in a type with six-digit precision. In C, arithmetic operations with floating-point numbers are performed internally with double or greater precision. For example, the following product is calculated using the float height = 1.2345, width = 2.3456; // Float variables have single // precision. double area = height * width; // The actual calculation is // performed with double // (or greater) precision. If you assign the result to a C defines only minimal requirements for the storage size and the binary format of the floating-point types
. However, the format commonly used is the one defined by the International Electrotechnical Commission (IEC) in the 1989 standard for binary floating-point arithmetic, IEC 60559. This standard is based in turn on the Institute of Electrical and Electronics Engineers' 1985 standard IEEE 754. Compilers can indicate that they support the IEC floating-point standard by defining the macro
The header file The program in Example 2-2 starts by printing the typical values for the type ## Example 2-2. Illustrating the precision of type float#include <stdio.h> #include <float.h> int main( ) { puts("\nCharacteristics of the type float\n"); printf("Storage size: %d bytes\n" "Smallest positive value: %E\n" "Greatest positive value: %E\n" "Precision: %d decimal digits\n", sizeof(float), FLT_MIN, FLT_MAX, FLT_DIG); puts("\nAn example of float precision:\n"); double d_var = 12345.6; // A variable of type double. float f_var = (float)d_var; // Initializes the float // variable with the value of d_var. printf("The floating-point number " "%18.10f\n", d_var); printf("has been stored in a variable\n" "of type float as the value " "%18.10f\n", f_var); printf("The rounding error is " "%18.10f\n", d_var - f_var); return 0; } The last part of this program typically generates the following output: The floating-point number 12345.6000000000 has been stored in a variable of type float as the value 12345.5996093750 The rounding error is 0.0003906250 In this example, the nearest representable value to the decimal 12,345.6 is 12,345.5996093750. This may not look like a round number in decimal notation, but in the internal binary representation of the floating-point type it is exactly representable, while 12,345.60 is not. |