@bintou
2018-10-22T11:36:08.000000Z
字数 792
阅读 1741
C语言
CSI
浮点数
陷阱
#include <stdio.h>
int main()
{
double i, endFlag = 10.0;
for(i = 9.0; i != endFlag; i += 0.1) {
printf("i = %.11f\n", i);
if(i > endFlag){
printf("Overflow. i = %.11f\n", i);
break;
}
}
i = 9.9;
i += 0.1;
printf("i = %.11f\n", i);
if(endFlag != i)
printf("They are not equal!\n");
else
printf("They are the same!\n");
return 0;
}
#include <stdio.h>
int main()
{
double i, endFlag = 10.0;
for(i = 9.0; i != endFlag; i += 0.1){
printf("i = %.50lf\n", i);
if(i > endFlag){
printf("Overflow. i = %.50lf\n", i);
break;
}
}
i = 9.9;
printf("i = %.50f\n", i);
i += 0.1;
printf("i + 0.1 = %.50f\n", i);
printf("endFlag = %.50f\n", endFlag);
if(endFlag != i)
printf("They are not equal!\n");
else
printf("They are the same!\n");
return 0;
}
#define PRECISION 0.0000000000001
for (i = 0; !(i < 10 + PRECISION && i > 10 - PRECISION); i += 0.1){
printf("%.12lf\n", i);
}