本文介绍了浮点不能准确地打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#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:
- http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
- http://www.hack3r.com/forum-topic/what-every-programmer-should-know-about-floating-point-arithmetic
这篇关于浮点不能准确地打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!