Namespaces
Variants
Views
Actions

Talk:cpp/language/floating literal

From cppreference.com

[edit] Hexadecimal floating literals in C++

I have tested floating point literals in C++ (MinGW) without the use of scanf or something, it worked perfectly.

Yes, C-style fp literals are one of the non-standard language extensions provided by gcc. --Cubbi 17:41, 21 July 2013 (PDT)
Maybe that can be added to c/language/floating_literal, the entire page is missing there though. --Bazzy 02:25, 22 July 2013 (PDT)
That would be the best outcome. P12 04:37, 22 July 2013 (PDT)

[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, does strtof support this textual floating-point representation? -P12 10:34, 6 March 2012 (PST)
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) e or E followed with optional minus or plus sign and nonempty sequence of decimal digits (defines exponent)
  • (optional) a suffix type specifier as a l, f, L or F
-- 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)