-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具有大约 16-17 个十进制数字的精度。
现在,这不是小数点后的位数——这是有效位数的总数,句号。(这就是为什么它被称为浮点数。)

因此,您读入的数字和您打印出来的数字在第 17 位之后有所不同,这并非巧合:

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