2014-09-24 51 views
-2
#include <stdio.h> 
#include <conio.h> 
#include <math.h> 
float t, delt = 0.004000,n; 

printf("enter the value for t="); 
scanf("%f", &t); 
n = t/ delt; 
printf("n is %f",n); 
getch(); 

输出: 对于T输入值= 1 n为249.999985浮点除法

我期待250我在做什么错?

+0

也许SO应该把[this](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html)页面的链接粘贴在顶部 – IllusiveBrian 2014-09-24 01:57:29

+0

SO应该会自动显示在发布之前为dups。像bugzilla一样 – thang 2014-09-24 01:58:13

回答

3

你正在反对floating point representations是近似值的常见问题。你不妨请执行下列操作之一:

  • Round your answer使用偏舍入策略。
  • 在打印之前将小数点后的小数位截断(使用类似%.0f的东西)。
  • 使用任意精度的算术库,如GMP

这其中哪一个是明智的完全取决于您的应用程序的限制。