Według standardu (C++11 draft n3337):
floating-literal:
fractional-constant exponent-part(opt) floating-suffix(opt)
digit-sequence exponent-part floating-suffix(opt)
fractional-constant:
digit-sequence(opt) . digit-sequence
digit-sequence .
exponent-part:
e sign(opt) digit-sequence
E sign(opt) digit-sequence
sign: one of
+ -
digit-sequence:
digit
digit-sequence digit
digit: one of
0 1 2 3 4 5 6 7 8 9
floating-suffix: one of
f l F L
Zgodnie z tą gramatyką te wszystkie zapisy są prawidłowe:
1.2
3.
.4
5e6
7E-8
Oczywiście do tego dochodzą jeszcze sufiksy. Sufiks f
(bądź F
, wielkość nie ma znaczenia) oznacza, że literał jest typu float
, natomiast sufiks l
( lub L
) oznacza typ long double
. Jeżeli sufiksu brak, literał jest traktowany jako typu double
.
Edit Co do pytania Azariena z komentarza: "co wg ciebie oznacza double d = 1.2L
?"
Oznacza rzutowanie long double
do double
. Przyklad (dla GCC 4.7.1):
double d1 = 1.2L;
double d2 = 1.2e500L;
double d3 = 1.2e500; // warning: floating constant exceeds range of 'double' [-Woverflow]
long double d4 = 1.2e500L;
cout << d1 << "\n"; // output: 1.2
cout << d2 << "\n"; // output: inf
cout << d3 << "\n"; // output: inf
cout << d4 << "\n"; // output: 1.2e+500