hexadecimal floating constant in C

0x0.3p10 is an example of a hexadecimal floating point literal, introduced in C99(1). The p separates the base number from the exponent.

The 0x0.3 bit is called the significand part (whole with optional fraction) and the exponent is the power of two by which it is scaled.

That particular value is calculated as 0.3 in hex, or 3 * 16-1 (3/16) multiplied by 210 (1024), which gives 3 * 1024 / 16 or 192. The following program confirms this:

#include <stdio.h>
int main (void) {
    double d = 0x0.3p10;
    printf ("%.f\n", d); // Outputs 192
    return 0;
}

Section 6.4.4.2 Floating constants of C99 has all the details:

A floating constant has a significand part that may be followed by an exponent part and a suffix that specifies its type. The components of the significand part may include a digit sequence representing the whole-number part, followed by a period ., followed by a
digit sequence representing the fraction part.

The components of the exponent part are an e, E, p, or P followed by an exponent consisting of an optionally signed digit sequence. Either the whole-number part or the fraction part has to be present; for decimal floating constants, either the period or the exponent part has to be present.

The significand part is interpreted as a (decimal or hexadecimal) rational number; the digit sequence in the exponent part is interpreted as a decimal integer. For decimal floating constants, the exponent indicates the power of 10 by which the significand part is to be scaled. For hexadecimal floating constants, the exponent indicates the power of 2 by which the significand part is to be scaled.

For decimal floating constants, and also for hexadecimal floating constants when FLT_RADIX is not a power of 2, the result is either the nearest representable value, or the larger or smaller representable value immediately adjacent to the nearest representable value, chosen in an implementation-defined manner. For hexadecimal floating constants when FLT_RADIX is a power of 2, the result is correctly rounded.


(1) These literals were also added to C++ in the C++17 iteration of the standard, though the ability to format and parse them was part of C++11.

Leave a Comment