0x0.3p10
represents what value?
And what's the meaning of the p
in the statement above?
A "floating-point constant" is a decimal number that represents a signed real number. The representation of a signed real number includes an integer portion, a fractional portion, and an exponent. Use floating-point constants to represent floating-point values that can't be changed.
C99 supports floating-point numbers that can be written in hexadecimal format. In hexadecimal format the exponent is a decimal number that indicates the power of two by which the significant part is multiplied. Therefore 0x1. fp3= 1.9375*8=1.55e1 .
A hexadecimal floating-point constant is shorthand for binary scientific notation, which is an abstract — yet direct — representation of a binary floating-point number.
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
, orP
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 whenFLT_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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With