Why is a floating point value, such as 3.14, considered by default in MSVC?

Why do I need to put 3.14f instead of 3.14 to disable all of these warnings? Is there a common reason for this?

+8
c ++ double floating-point visual-c ++
Dec 04 '10 at
source share
5 answers

What the C ++ (and C) standard decided. Floating point literals are of type double, and if you need them to float, you suffix them with f . There seems to be no specific reason why, but I would suggest that a) for compatibility with C and b) a trade-off is between accuracy and storage.

2.13.3 Floating literals A type of lithographic document is double, unless explicitly specified by a suffix. the suffixes f and F indicate float, the suffixes l and L indicate a long double. If the scale value is not in the range of the represented values ​​for its type, the program is poorly formed.

+17
Dec 04 '10 at 13:44
source share

C and C ++ prefer double swimming in several ways. As you noticed, fractional literals are double unless floats are explicitly made. In addition, floats cannot be passed to varargs; they always go up to double (in the same way, char and short go up to int in varargs).

It is probably best to think of a float as an abbreviated double rather than a double being an extended float . That is, double is the preferred floating-point type, and float used whenever a smaller version of double is required for a particular case. What is closest I know to the sequential reason, and then the rule makes sense, even if you happen to need a smaller version.

+5
Dec 04 '10 at 14:17
source share

This is not MSVC specific, it is required by locale.

I would suggest that it makes sense not to reduce the accuracy unless explicitly requested, so the default value is double.

6 significant digits of accuracy, which provides floating single-point accuracy, are rarely sufficient for general use and, of course, on a modern desktop processor will be used as manual coded optimization, where the writer has determined that this is sufficient and necessary; therefore, it makes sense that an explicit visible marker is required to indicate a literal with one precision.

+1
Dec 04 '10 at 14:25
source share

This is probably the standard in the world of C. Double is preferable because it is more accurate and you probably won't see any performance differences. Read this post .

0
Dec 04 '10 at
source share

Because a double can approach 3.14 much better than a float , maybe? Here are the exact values:

3.140000000000000124344978758017532527446746826171875 ( double )

3.1400001049041748046875 ( float )

0
Dec 04 '10 at 17:16
source share



All Articles