user-0 提问: 关于c语言的一个小问题

回复:
user: user-1
content: 下面是程序输出 输出的数字 依次为  0.1的累加和i, i取整的值， i的值 0.1与累加次数的乘积j j取整的值  =========================== 0.0 = 0		i=0.0 == j=0.0 ==> 0 0.1 != 0		i=0.1 == j=0.1 ==> 0 0.2 != 0		i=0.2 == j=0.2 ==> 0 0.3 != 0		i=0.3 == j=0.3 ==> 0 0.4 != 0		i=0.4 == j=0.4 ==> 0 0.5 != 0		i=0.5 == j=0.5 ==> 0 0.6 != 0		i=0.6 < j=0.6 ==> 0 0.7 != 0		i=0.7 < j=0.7 ==> 0 0.8 != 0		i=0.8 < j=0.8 ==> 0 0.9 != 0		i=0.9 < j=0.9 ==> 0 1.0 != 0		i=1.0 < j=1.0 ==> 1 1.1 != 1		i=1.1 < j=1.1 ==> 1 1.2 != 1		i=1.2 < j=1.2 ==> 1 1.3 != 1		i=1.3 == j=1.3 ==> 1 1.4 != 1		i=1.4 == j=1.4 ==> 1 1.5 != 1		i=1.5 > j=1.5 ==> 1 1.6 != 1		i=1.6 > j=1.6 ==> 1 1.7 != 1		i=1.7 > j=1.7 ==> 1 1.8 != 1		i=1.8 > j=1.8 ==> 1 1.9 != 1		i=1.9 > j=1.9 ==> 1 2.0 != 2		i=2.0 > j=2.0 ==> 2 2.1 != 2		i=2.1 > j=2.1 ==> 2 2.2 != 2		i=2.2 > j=2.2 ==> 2 2.3 != 2		i=2.3 > j=2.3 ==> 2 2.4 != 2		i=2.4 > j=2.4 ==> 2 2.5 != 2		i=2.5 > j=2.5 ==> 2 2.6 != 2		i=2.6 > j=2.6 ==> 2 2.7 != 2		i=2.7 > j=2.7 ==> 2 2.8 != 2		i=2.8 > j=2.8 ==> 2 2.9 != 2		i=2.9 > j=2.9 ==> 2 3.0 != 3		i=3.0 > j=3.0 ==> 3 3.1 != 3		i=3.1 > j=3.1 ==> 3 3.2 != 3		i=3.2 > j=3.2 ==> 3 3.3 != 3		i=3.3 > j=3.3 ==> 3 3.4 != 3		i=3.4 > j=3.4 ==> 3 3.5 != 3		i=3.5 > j=3.5 ==> 3 3.6 != 3		i=3.6 > j=3.6 ==> 3 3.7 != 3		i=3.7 > j=3.7 ==> 3 3.8 != 3		i=3.8 > j=3.8 ==> 3 3.9 != 3		i=3.9 > j=3.9 ==> 3 4.0 != 4		i=4.0 > j=4.0 ==> 4 4.1 != 4		i=4.1 > j=4.1 ==> 4 4.2 != 4		i=4.2 > j=4.2 ==> 4 4.3 != 4		i=4.3 > j=4.3 ==> 4 4.4 != 4		i=4.4 == j=4.4 ==> 4 4.5 != 4		i=4.5 == j=4.5 ==> 4 4.6 != 4		i=4.6 < j=4.6 ==> 4 4.7 != 4		i=4.7 < j=4.7 ==> 4 4.8 != 4		i=4.8 < j=4.8 ==> 4 4.9 != 4		i=4.9 < j=4.9 ==> 4 5.0 != 4		i=5.0 < j=5.0 ==> 5 5.1 != 5		i=5.1 < j=5.1 ==> 5 5.2 != 5		i=5.2 < j=5.2 ==> 5 5.3 != 5		i=5.3 < j=5.3 ==> 5 5.4 != 5		i=5.4 < j=5.4 ==> 5 5.5 != 5		i=5.5 < j=5.5 ==> 5 5.6 != 5		i=5.6 < j=5.6 ==> 5 5.7 != 5		i=5.7 < j=5.7 ==> 5 5.8 != 5		i=5.8 < j=5.8 ==> 5 5.9 != 5		i=5.9 < j=5.9 ==> 5 6.0 != 5		i=6.0 < j=6.0 ==> 6 6.1 != 6		i=6.1 < j=6.1 ==> 6 6.2 != 6		i=6.2 < j=6.2 ==> 6 6.3 != 6		i=6.3 < j=6.3 ==> 6 6.4 != 6		i=6.4 < j=6.4 ==> 6 6.5 != 6		i=6.5 < j=6.5 ==> 6 6.6 != 6		i=6.6 < j=6.6 ==> 6 6.7 != 6		i=6.7 < j=6.7 ==> 6 6.8 != 6		i=6.8 < j=6.8 ==> 6 6.9 != 6		i=6.9 < j=6.9 ==> 6 7.0 != 6		i=7.0 < j=7.0 ==> 7 7.1 != 7		i=7.1 < j=7.1 ==> 7 7.2 != 7		i=7.2 < j=7.2 ==> 7 7.3 != 7		i=7.3 < j=7.3 ==> 7 7.4 != 7		i=7.4 < j=7.4 ==> 7 7.5 != 7		i=7.5 < j=7.5 ==> 7 7.6 != 7		i=7.6 < j=7.6 ==> 7 7.7 != 7		i=7.7 < j=7.7 ==> 7 7.8 != 7		i=7.8 < j=7.8 ==> 7 7.9 != 7		i=7.9 < j=7.9 ==> 7 8.0 != 7		i=8.0 < j=8.0 ==> 8 8.1 != 8		i=8.1 < j=8.1 ==> 8 8.2 != 8		i=8.2 < j=8.2 ==> 8 8.3 != 8		i=8.3 < j=8.3 ==> 8 8.4 != 8		i=8.4 < j=8.4 ==> 8 8.5 != 8		i=8.5 < j=8.5 ==> 8 8.6 != 8		i=8.6 < j=8.6 ==> 8 8.7 != 8		i=8.7 < j=8.7 ==> 8 8.8 != 8		i=8.8 < j=8.8 ==> 8 8.9 != 8		i=8.9 < j=8.9 ==> 8 9.0 != 8		i=9.0 < j=9.0 ==> 9 9.1 != 9		i=9.1 < j=9.1 ==> 9 9.2 != 9		i=9.2 < j=9.2 ==> 9 9.3 != 9		i=9.3 < j=9.3 ==> 9 9.4 != 9		i=9.4 < j=9.4 ==> 9 9.5 != 9		i=9.5 < j=9.5 ==> 9 9.6 != 9		i=9.6 < j=9.6 ==> 9 9.7 != 9		i=9.7 < j=9.7 ==> 9 9.8 != 9		i=9.8 < j=9.8 ==> 9 9.9 != 9		i=9.9 < j=9.9 ==> 9 10.0 != 9		i=10.0 < j=10.0 ==> 10 10.1 != 10		i=10.1 < j=10.1 ==> 10 10.2 != 10		i=10.2 < j=10.2 ==> 10 10.3 != 10		i=10.3 < j=10.3 ==> 10 10.4 != 10		i=10.4 < j=10.4 ==> 10 10.5 != 10		i=10.5 < j=10.5 ==> 10 10.6 != 10		i=10.6 < j=10.6 ==> 10 10.7 != 10		i=10.7 < j=10.7 ==> 10 10.8 != 10		i=10.8 < j=10.8 ==> 10 10.9 != 10		i=10.9 < j=10.9 ==> 10 11.0 != 10		i=11.0 < j=11.0 ==> 11 11.1 != 11		i=11.1 < j=11.1 ==> 11

user: user-1
content: 浮点数运算有误差。尽量不要对浮点数比较相等，比较大小时也要尽量加上一个极小的冗余值。  下面的代码可显示，10个0.1累加是小于10的，N个0.1相加与N*0.1多数情况下是不相等的，有大有小。      1  #include <stdio.h>   2    3    4 int main()   5 {   6         double i;   7         int n=0;   8         double j;   9         for(i=0;i!=10;i+=0.1, n++)  10         {  11                 j = n*0.1;  12                 int x = (int)i;  13                 char* s;  14                 if (i==x) s = "=";  15                 else s = "!=";  16                 printf("%.1f %s %d\t\t",i, s, x);  17   18                 int k = (int)j;  19                 if (i<j) printf("i=%.1f < j=%.1f ==> %d\n", i,j,k);  20                 else if (i==j) printf("i=%.1f == j=%.1f ==> %d\n", i,j,k);  21                 if (i>j) printf("i=%.1f > j=%.1f ==> %d\n", i,j,k);  22                 if(i>11)  23                         break;  24         }  25         return 0;  26 }