-1

問題が発生しましたstrtod()。数字が追加されているようです。私が読んでいる

2\t5241021356985.0302\t9.09\t825.45

ファイルからそして整数2をスキップした後、次の出力が得られます

output: 5241021356985.030273  9 .090000

これが私のコードです

char *input_string = (char*) calloc(filesize, sizeof (char*));
char *output_string = (char*) calloc(filesize, sizeof (char*));
char *input_end;
fgets(input_string, filesize, infile);
input_end = input_string;
int size_to_read = (int) strtof(input_string, &input_end);
char *temp_string=(char*)calloc(70,sizeof(char*)); // max double value
double temp = 0;
++input_string;
for (int i = 0; i < size_to_read; ++i) {
   temp = strtod(input_string, &input_end);
   sprintf(temp_string, "%lf\t", temp);
   strcat(output_string, temp_string);
   input_string = input_end;
   ++input_string;
}
strcat(output_string, "\0");
printf("output: %s\n", output_string);
4

1 に答える 1

0

通常、型doubleの精度は 10 進数で約 16 ~ 17 桁です。
これは小数点以下の桁数ではなく、有効桁数の合計です。(これが浮動小数点と呼ばれる理由です。)

したがって、読み取った数値と出力した数値が 17 桁目以降で異なるのは偶然ではありません。

 input: 5241021356985.0302
output: 5241021356985.030273
digits: 1234567890123 4567890
                 1          2
于 2016-03-09T20:31:40.203 に答える