double f_64 = 3.35f;
double f1_64 = 3.35;
如果使用后缀"f"会有什么影响?
使用在线 FPU 编译器,十六进制结果如下
带后缀 f
- 0x400ACCCCC0000000
,
不带后缀 f
- 0x400ACCCCCCCCCCCD
.
f
后缀强制编译器将该值视为float
,而不是在下面的赋值中没有多大意义的double
。
double f_64 = 3.35f;
// Why force a value to float when you've allocated memory for a double
请记住,double
是精度的 2 倍 float
.根据您的特定需求选择类型。
但是,说,你正在做
float ans;
ans = 3/2; // ans is trimmed to an int
ans = 3/2.0f; // The decimals are retained