Why do I need to put 3.14f instead of 3.14 to disable all those warnings ? Is there a coherent reason reason for this ?
5 Answers
That's what the C++ (and C) standard decided. Floating point literals are of type double, and if you need them to be floats, you suffix them with a f
. There doesn't appear to be any specifically stated reason as to why, but I'd guess it's a) For compatibility with C, and b) A trade-off between precision and storage.
2.13.3 Floating literals The type of a floating literal is double unless explicitly specified by a suffix. The suffixes f and F specify float, the suffixes l and L specify long double. If the scaled value is not in the range of representable values for its type, the program is ill-formed.
-
And that pretty much definitively answers the OP's question, doesn't it? – JUST MY correct OPINION Dec 04 '10 at 14:18
C and C++ prefer double to float in a couple of ways. As you noticed, fractional literals are double unless explicitly made floats. Also, floats can't be passed in varargs, they're always promoted to double (in the same way char and short are promoted to int in varargs).
It's probably better to think of float
as being a contracted double
, rather than double
being an extended float
. That is, double
is the preferred floating point type, and float
is used whenever a smaller version of double
is required for some particular case. That's the closest I know to a coherent reason, and then the rule makes sense, even if you happen to be in the case where you need a smaller version.

- 273,490
- 39
- 460
- 699
This is not peculiar to MSVC, it is required by the language standard.
I would suggest that it made sense not to reduce precision unless explicitly requested, so the default is double.
The 6 significant digits of precision that a single-precision float provides is seldom sufficient for general use and certainly on a modern desktop processor would be used as a hand coded optimisation where the writer has determined that it is sufficient and necessary; so it makes sense that an explicit visible marker is required to specify a single-precision literal.

- 88,407
- 13
- 85
- 165
-
+1; @gokoon: there are other suffixes that you should use if you want to get the proper type or size. For example, if you want an unsigned 64 bit value, you use the suffix `ull` – franji1 Dec 04 '10 at 16:05
This is probably a standard in C world. Double is preferred since it's more precise and you probably won't see any performance differences. Read this post.
-
Almost yes on a today's machine, but on an embedded device which has am FPU I'm not so sure... – jokoon Dec 04 '10 at 13:54
-
@gokoon: that's true; by default I assumed we're talking about "normal" machines. By that I mean desktop PCs and servers. On an embedded device, optimization is important and having to make a choice between short and int in some cases is not uncommon. – darioo Dec 04 '10 at 13:56
-
Even with an FPU, a double requires twice the space of a float, so when processing *very large* amounts of data, memory bandwidth may become significant even when the individual register level operations take the same length of time. – Clifford Dec 04 '10 at 14:11
Because double
can approximate 3.14 much better than float
, maybe? Here are the exact values:
3.140000000000000124344978758017532527446746826171875 (double
)
3.1400001049041748046875 (float
)

- 256,549
- 94
- 388
- 662