浮点不能准确地打印 [英] Floating point does not print accurately

查看:110
本文介绍了浮点不能准确地打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  用C 浮点问题


 #包括LT&;&stdio.h中GT;
主要()
{
        诠释A,B;
        浮F;
        scanf函数(%2D%3D%4F,&安培;一,和b,&安培; F);
        的printf(%D%F,A,B,F);
}

当我运行这个程序,输入2 4 56.8,它给输出2 4 56.799999 .....但我希望2 4 56.8 ....为什么会这样?


解决方案

这是正确的。浮点数是近似值。正如0.33333333是一个近似1/3,56.7999999是56.8近似值。有0.1没有确切的浮点重新presentation。

请参阅一些什么已经被写入:

Possible Duplicate:
Floating point issue in C

#include<stdio.h>
main()
{
        int a,b;
        float f;
        scanf("%2d%3d%4f",&a,&b,&f);
        printf("%d %d %f",a,b,f);
}

When i run this program and input 2 4 56.8 ,it gives output 2 4 56.799999.....but I would expect 2 4 56.8....why is it so???

解决方案

That is correct. Floating point numbers are approximations. Just as 0.33333333 is an approximation to 1/3, 56.7999999 is an approximation for 56.8. There is no exact floating point representation for 0.1.

See some of what has been written:

这篇关于浮点不能准确地打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆