Floating point hex notation in Go-specification

I am having problems implementing the hexadecimal float notation as a regex.

In the specification the notation for floating hexadecimal notation has been defined as follows:

hex_float_lit     = "0" ( "x" | "X" ) hex_mantissa hex_exponent .
hex_mantissa      = [ "_" ] hex_digits "." [ hex_digits ] |
                    [ "_" ] hex_digits |
                    "." hex_digits .
hex_exponent      = ( "p" | "P" ) [ "+" | "-" ] decimal_digits .

In the last line the hex_exponent which must follow the hex_mantissa (see first line: hex_float_lit) defines the start of the hex_exponent being a [pP]{1}[+-]?[0-9]+(?:[_]?[0-9])* (please correct this regex of the last line if I am wrong). If the part after the mantissa must begin with a p or P from the following examples on the same link this for example 0x15e will not match, since after x and the mantissa-part comes no pP. But this 0x15e compiles successfully in Go. How is it possible after this syntax-declaration that 0x15e is correct? Where is this case covered in the notation above?

Any helps are appreciated, Thank you

A

e matches as a hex_digit. 0x15e is a valid hexadecimal integer, 350 in decimal.

1 Like

0x15e compiles in Go because it is not a hex floating-point literal. It is a hex integer literal.

The rule you quoted (hex_mantissa followed by p or P) applies only to hex floating-point numbers, like:

  • 0x1.fp3

  • 0x1p10

In 0x15e, the e is simply a hex digit (since hex digits are 0–9 and a–f). So the whole value is just a hex integer, not a float.

So this case is covered by the hex integer literal rule, not the hex float literal rule.