Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does hexadecimal floating point need to have a specified exponent?

Tags:

c

standards

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?

like image 987
countunique Avatar asked Jun 18 '14 14:06

countunique


1 Answers

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.

like image 70
Keith Thompson Avatar answered Oct 24 '22 01:10

Keith Thompson