c++ - 为什么在 MSVC 中默认将浮点值(如 3.14)视为 double 值?

标签 c++ visual-c++ floating-point double

为什么我需要放置 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/

相关文章:

c++ - `is_trivially_destructible` 不适用于继承类

c++ - 将 STL vector<bool> 快速转换为 bool * array

c++ - 组织项目依赖

visual-studio-2010 - C#字符串与std:string:传递字符串时出现问题

c# - 表达式和分配的对象之间的浮点不一致

c++ - 在容器中正确使用多态性?

visual-c++ - Directx11 SDK 6月(2010) VC++ 2010 上的初始化

algorithm - 浮点哈希表

c++ - 寻找半 float 或四分之一 float 库

c++ - Visual Studio malloc 限制