-1

I got a Problem with my strtod() it seems to add some digits. I'm reading

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

from a File And after skipping the integer 2 I get the following output

output: 5241021356985.030273  9 .090000

Here is my Code

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);
Sven Bamberger
  • 899
  • 2
  • 10
  • 21

1 Answers1

0

Typically, type double has about 16-17 decimal digits worth of precision.
Now, that's not some number of digits past the decimal point -- that's the total number of significant digits, period. (That's why it's called floating point.)

So it's no coincidence that the number you read in, and the number you printed back out, differ after about the 17th digit:

 input: 5241021356985.0302
output: 5241021356985.030273
digits: 1234567890123 4567890
                 1          2
Steve Summit
  • 45,437
  • 7
  • 70
  • 103