首页 > 解决方案 > 带后缀“f”的双常量用法

问题描述

double f_64  = 3.35f;
double f1_64 = 3.35;

如果使用后缀“f”有什么影响?

使用在线 FPU 编译器,十六进制结果如下

带后缀f- 0x400ACCCCC0000000
不带后缀f- 0x400ACCCCCCCCCCCD

标签: cfloating-pointdouble

解决方案


f后缀强制编译器将值与下面的赋值中的floata 相反,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

推荐阅读