今天在根据需求写代码时候,偶尔发现linux 下 设置变量类型 float 和double 计算时,
得到的结果是不一样的。
要求: 设定值 = 传入值 * 10 * 122.88 /1000;
case: 设定值 = 1666*10*122.88/1000
= 2047.1808
设置成 float时,代码:
#include
#include
unsigned int fun(unsigned int sfn_threshold)
{
float f_sfn_threshold;
printf("input parameter=%d\n",sfn_threshold);
f_sfn_threshold = (float)sfn_threshold*1.2288;
printf("f_sfn_threshold = %8lf\n",f_sfn_threshold);
printf("%.8lf\n",fabs(f_sfn_threshold -(unsigned int)f_sfn_threshold ));
return 0;
}
int main(int argc, char **argv)
{
unsigned int a=1666;
unsigned int b=10000;
unsigned int c=12888;
unsigned int d=65535;
fun(a);
return 0;
}
执行结果(与要得到的结果不一致):
[root@localhost test_float_compare]# gcc test_float_double_diff.c -o test_float_double_diff_1666
[root@localhost test_float_compare]#
[root@localhost test_float_compare]# ./test_float_double_diff_1666
input parameter=1666
f_sfn_threshold = 2047.180786
0.18078613
[root@localhost test_float_compare]#
设置成 double时,代码:
#include
#include
unsigned int fun(unsigned int sfn_threshold)
{
double f_sfn_threshold;
printf("input parameter=%d\n",sfn_threshold);
f_sfn_threshold = (double)sfn_threshold*1.2288;
printf("f_sfn_threshold = %8lf\n",f_sfn_threshold);
printf("%.8lf\n",fabs(f_sfn_threshold -(unsigned int)f_sfn_threshold ));
return 0;
}
int main(int argc, char **argv)
{
unsigned int a=1666;
unsigned int b=10000;
unsigned int c=12888;
unsigned int d=65535;
fun(a);
return 0;
}
执行结果:
[root@localhost test_float_compare]#
[root@localhost test_float_compare]# ./test_float_double_diff_1666
input parameter=1666
f_sfn_threshold = 2047.180800
0.18080000
[root@localhost test_float_compare]#