Wednesday, October 19, 2005
Floating Point Fractional Constants in C99
Today, when looking into the C99 standard for clarifying a doubt I had, I encountered a
strange construct in C99 hexadecimal constants where "P" is allowed to be present. Later,
with exprimentation, I found that it is for supporting floating point "fractional" constants.
For example, 0x1P1 means 1 * 2 power 1 => 2
0x1P-1 means 1 * 2 power -1 => 0.5
Note that this construct is only allowed for hex-constants and the power is 2 not 10 or 16.
With this, just like the e/E notation for exponents, fractional values can be conveniently expressed
with P format. For example to represent a fraction, say, EPSILON, I can use P notation:
#define EPSILON 0X1P-23F
instead of:
#define EPSILON 1.19209e-07
But I don't know what P means (may be "Power of"?).
strange construct in C99 hexadecimal constants where "P" is allowed to be present. Later,
with exprimentation, I found that it is for supporting floating point "fractional" constants.
For example, 0x1P1 means 1 * 2 power 1 => 2
0x1P-1 means 1 * 2 power -1 => 0.5
Note that this construct is only allowed for hex-constants and the power is 2 not 10 or 16.
With this, just like the e/E notation for exponents, fractional values can be conveniently expressed
with P format. For example to represent a fraction, say, EPSILON, I can use P notation:
#define EPSILON 0X1P-23F
instead of:
#define EPSILON 1.19209e-07
But I don't know what P means (may be "Power of"?).