Definition of floating literal in C++ standard

M

murali.desikan

Hi,

The definition of floating literal in the C++ (ISO/IEC 14882:2003)
grammar is as follows (Note: I have replaced the "opt" subscript used
in the standard with [ ] to indicate optional symbol).

------BEGIN------
floating-literal:
fractional-constant [exponent-part] [floating-suffix]
digit-sequence exponent-part [floating-suffix]
fractional-constant:
[digit-sequence] . digit-sequence
digit-sequence .
exponent-part:
e signopt digit-sequence
E signopt digit-sequence
sign: one of
+ -
digit-sequence:
digit
digit-sequence digit
floating-suffix: one of
f l F L
------END------

The above specification does not allow for an optional sign (+ or -)
to be specified for the fractional-constant (for eg. -2.1e+2). Is this
a defect in the standard or am I missing something?

Thanks,
Murali
 
J

Jerry Coffin

[ ... ]
The above specification does not allow for an optional sign (+ or -)
to be specified for the fractional-constant (for eg. -2.1e+2). Is this
a defect in the standard or am I missing something?

That's correct -- a sequence like -2.1e+2 is a unary negation operator
followed by a floating point literal. The literal itself is never
negative.
 
D

Dominic Connor, Quant Headhunter

That's correct -- a sequence like -2.1e+2 is a unary negation operator
followed by a floating point literal. The literal itself is never
negative.
Thank you for the clarity.

What's the motivation for this, rather than just allow for numeric
constants to be negative ?
 
J

James Kanze

Thank you for the clarity.
What's the motivation for this, rather than just allow for
numeric constants to be negative ?

The effects allowing them to be negative would have elsewhere.
Given "a-42", for example, you really don't want it to resolve
to two tokens, the symbol "a" and the integral constant -42,
without an operator.
 
A

Andrew Koenig

The effects allowing them to be negative would have elsewhere.
Given "a-42", for example, you really don't want it to resolve
to two tokens, the symbol "a" and the integral constant -42,
without an operator.

Also--and this is really the opposite side of the same coin--once the
language gives a meaning to -x as an expression, -42 gains an analogous
meaning automatically. Therefore, not only is there no need to define a
meaning for - as part of a literal, but doing so would run the risk of
introducing an ambiguity.
 
A

Alf P. Steinbach

* Andrew Koenig:
Also--and this is really the opposite side of the same coin--once the
language gives a meaning to -x as an expression, -42 gains an analogous
meaning automatically. Therefore, not only is there no need to define a
meaning for - as part of a literal, but doing so would run the risk of
introducing an ambiguity.

I started to write that I disagreed about the "no need", considering
e.g. LONG_MIN and "a--5" versus "a+-5" (consistency, least surprise),
but then I thought about "a=-5"...

Cheers,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top