Talk:cpp/language/floating literal
From cppreference.com
[edit] syntax
The literals can be seen as four different parts:
- integral dot decimal exponent suffix
The valid combinations are:
| int | dot | dec | exp |
|---|---|---|---|
| X | X | X | X |
| X | X | X | |
| X | X | ||
| X | X | ||
| X | X | X | |
| X | X | ||
| X | X | X |
where X means "present in literal"
The suffix is always optional
Splitting in regex:
[0-9]+ \. [0-9]* exponent?
| int | dot | dec | exp |
|---|---|---|---|
| X | X | X | X |
| X | X | X | |
| X | X | ||
| X | X | X |
[0-9]* \. [0-9]+ exponent?
| int | dot | dec | exp |
|---|---|---|---|
| X | X | X | X |
| X | X | X | |
| X | X | ||
| X | X | X |
[0-9]+ exponent
| int | dot | dec | exp |
|---|---|---|---|
| X | X |
RegEx may not be an optimal representation of this
Bazzy 15:02, 5 March 2012 (PST)
- As for the representation, what do you think about something similar to this? -P12 01:38, 6 March 2012 (PST)
- Can it be used to express efficiently the fact that every part of a floating point is optional but only some combinations are allowed?
- Another option may be the BNF like in the ISO docs. It's quite clean but makes harder to get how the accepted literals look like
- Maybe it would be clearer with both a formal notation and a textual description.
- -- Bazzy 09:02, 6 March 2012 (PST)
- Given the fact, that the decimal floating point expression at that page already describes 6 out of 7 cases (except that decimal point isn't optional), I think it's sufficiently efficient. For the last case (
|X| | |X|), we could add a separate description. By the way, doesstrtofsupport this textual floating-point representation? -P12 10:34, 6 March 2012 (PST)
- Given the fact, that the decimal floating point expression at that page already describes 6 out of 7 cases (except that decimal point isn't optional), I think it's sufficiently efficient. For the last case (
- the int+exp can be obtained with: nonempty digit sequence without decimal-point but with the exponent
- Would the following work?
- nonempty sequence of decimal digits optionally containing a decimal point character (defines significand)
- (optional)
eorEfollowed with optional minus or plus sign and nonempty sequence of decimal digits (defines exponent) - (optional) a suffix type specifier as a
l,f,LorF
- -- Bazzy 11:27, 6 March 2012 (PST)
- Yes, I was thinking exactly about that. The point character is not optional though. I'll update the page with this. -P12 13:58, 6 March 2012 (PST)
- The point is optional. The ISO C document on strtof says as follows:
- a nonempty sequence of decimal digits optionally containing a decimal-point character, then an optional exponent part as defined in 6.4.4.2;
- And it's the fact that it's optional to allow the syntax like 1e4
- I'll update here with the textual description
- -- Bazzy 14:06, 6 March 2012 (PST)
- According to your first table the dot is mandatory in all cases except the fourth. We could add a separate description for the fourth entry. -P12 14:18, 6 March 2012 (PST)
- The point is optional. The ISO C document on strtof says as follows: