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_1666
input parameter=1666
f_sfn_threshold = 2047.180786
0.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_1666
input parameter=1666
f_sfn_threshold = 2047.180800
0.18080000
[root@localhost test_float_compare]#


 

 

 

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值