为什么我需要放置 3.14f 而不是 3.14 来禁用所有这些警告? 这是否有连贯的原因?
最佳答案
这就是 C++(和 C)标准所决定的。浮点文字是 double 类型,如果你需要它们是 float ,你可以在它们后面加上一个 f
后缀。似乎没有任何具体说明原因,但我猜它是 a) 为了与 C 的兼容性,以及 b) 在精度和存储之间进行权衡。
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.
关于c++ - 为什么在 MSVC 中默认将浮点值(如 3.14)视为 double 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4353780/