3.2. Floating-Point Constants
Floating-point constants can be written either in decimal or in hexadecimal notation. These notations are described in the next two sections.
3.2.1. Decimal Floating-Point Constants
An ordinary floating-point constant consists of a sequence of decimal digits containing a decimal point. You may also multiply the value by a power of 10, as in scientific notation : the power of 10 is represented simply by an exponent, introduced by the letter e or E. A floating-point constant that contains an exponent does not need to have a decimal point. Table 3-2 gives a few examples of decimal floating-point constants .
The decimal point can also be the first or last character . Thus 10. and .234E6 are permissible numerals. However, the numeral 10 with no decimal point would be an integer constant, not a floating-point constant.
The default type of a floating-point constant is double. You can also append the suffix F or f to assign a constant the type float, or the suffix L or l to give a constant the type long double, as this example shows:
float f_var = 123.456F; // Initialize a float variable. long double ld_var = f_var * 987E7L; // Initialize a long double variable // with the product of a // multiplication performed with // long double precision.
3.2.2. Hexadecimal Floating-Point Constants (C99)
The C99 standard introduced hexadecimal floating-point constants , which have a key advantage over decimal floating-point numerals: if you specify a constant value in hexadecimal notation, it can be stored in the computer's binary floating-point format exactly, with no rounding error, whereas values that are "round numbers" in decimal notationlike 0.1may be repeating fractions in binary, and have to be rounded for representation in the internal format. (For an example of rounding with floating-point numbers, see Example 2-2.)
A hexadecimal floating-point constant consists of the prefix 0x or 0X, a sequence of hexadecimal digits with an optional decimal point (which perhaps we ought to call a "hexadecimal point" in this case), and an exponent to base two. The exponent is a decimal numeral introduced by the letter p or P. For example, the constant 0xa.fP-10 is equal to the number (10 + 15/16) x 2-10 (not 2-16) in decimal notation. Equivalent ways of writing the same constant value are 0xA.Fp-10, 0x5.78p-9, 0xAFp-14, and 0x.02B3p0. Each difference of 1 in the exponent multiplies or divides the hexadecimal fraction by a factor of 2, and each shift of the hexadecimal point by one place corresponds to a factor (or divisor) of 16, or 24.
In hexadecimal floating-point constants , you must include the exponent, even if its value is zero. This step is necessary in order to distinguish the type suffix F (after the exponent) from the hexadecimal digit F (to the left of the exponent). For example, if the exponent were not required, the constant 0x1.0F could represent either the number 1.0 with type float, or the number 1 + 15/256 with the default type double.
Like decimal floating-point constants , hexadecimal floating-point constants also have the default type double. Append the suffix F or f to assign a constant the type float, or the suffix L or l to give it the type long double.