2014-09-24 53 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

這其中哪一個是明智的完全取決於您的應用程序的限制。