I'm learning about programming languages now (BNFs, scanners, etc) and it seems weird that hexadecimal floating point constants in C require an exponent.
From the C99 standard,
§6.4.4.2 Floating constants
hexadecimal-floating-constant:
hexadecimal-prefix hexadecimal-fractional-constant
binary-exponent-part floating-suffix[opt]
hexadecimal-prefix hexadecimal-digit-sequence
binary-exponent-part floating-suffix[opt]
It seems easy to write a scanner that will parse hexadecimal floating point constants with an optional exponent. Why does the C standard make the exponent optional for decimal floating points and required for hex floating points?
This is covered in the C99 Rationale, section 6.4.4.2:
A new feature of C99: C99 adds hexadecimal notation because it more clearly expresses the significance of floating constants. The binary-exponent part is required, instead of optional as it is for decimal notation, to avoid ambiguity resulting from an
f
suffix being mistaken as a hexadecimal digit.
For example, 1.0f
is a decimal floating-point constant of type float
, but 0x1.0f
would be ambiguous, and must be written as 0x1.0p0f
.
For the same reason, the exponent is introduced by p
rather than e
because e
is a valid hexadecimal digit.
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