linux 下 float 和 double 精度计算差别

今天在根据需求写代码时候,偶尔发现linux 下 设置变量类型 float 和double 计算时,,

得到的结果是不一样的。

要求:设定值 = 传入值 * 10 * 122.88 /1000;

case: 设定值 = 1666*10*122.88/1000

= 2047.1808

设置成 float时,代码:

#include<stdio.h>#include <math.h>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_1666input parameter=1666f_sfn_threshold = 2047.1807860.18078613[root@localhost test_float_compare]#

设置成 double时,代码:

#include<stdio.h>#include <math.h>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_1666input parameter=1666f_sfn_threshold = 2047.1808000.18080000[root@localhost test_float_compare]#

一个人行走,若是寂寞了,寻一座霓虹灯迷离闪烁,

linux 下 float 和 double 精度计算差别

相关文章:

你感兴趣的文章:

标签云: