Why floating point value such as 3.14 are considered as double by default in MSVC?

前端 未结 5 1751
逝去的感伤
逝去的感伤 2020-11-27 08:27

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条回答
  •  孤城傲影
    2020-11-27 08:51

    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.

提交回复
热议问题