我試圖存儲十進制數字的所有數字。我決定使用modf爲此目的。我的代碼段是;modf函數無法正常工作
struct high_precision scan_high(int *j)
{
int i,a;
struct high_precision mynum;
double num1, fracpart, intpart;
printf("Enter the values> ");
scanf("%lf", &num1);
if(num1 < 0)
mynum.sign = -1;
else
mynum.sign = 1;
num1 = fabs(num1);
fracpart = modf(num1, &intpart);
if (intpart > 0 && intpart < 10)
a = 1;
while(intpart == 0) {
fracpart *= 10;
fracpart = modf(fracpart, &intpart);
a -= 1;
}
for(i=0;fracpart > 0 && intpart != 0;i++){
if(intpart > 0){
mynum.digits[i] = intpart;
}
fracpart *= 10;
fracpart = modf(fracpart, &intpart);
}
*j = i;
mynum.decpt = a;
return(mynum);
}
但不知何故,它不工作,因爲我想。
Enter the values> 0.009876
0.876000 9.000000
0.760000 8.000000
0.600000 7.000000
它必須停在這條線。但是,它是繼續計數;
1.000000 5.000000
1.000000 9.000000
1.000000 9.000000
1.000000 9.000000
1.000000 9.000000
1.000000 9.000000
0.999998 9.000000
0.999977 9.000000
0.999767 9.000000
0.997669 9.000000
0.976694 9.000000
0.766942 9.000000
0.669420 7.000000
0.694198 6.000000
0.941983 6.000000
0.419827 9.000000
0.198267 4.000000
0.982671 1.000000
0.826707 9.000000
0.267069 8.000000
0.000000 2.000000
0.000000 0.000000
[什麼每臺計算機科學家應該知道關於浮點運算(http://download.oracle.com/docs/cd/E19957-01/806-3568 /ncg_goldberg.html) – pmg
[Wikipedia:Floating Point Number](http://en.wikipedia.org/wiki/Floating_point_number) –