I have a multiline file. First line contains an integer N, followed by exactly N lines, where each of them contains a pair of floats seperated by a white space. Each float has exactly two decimal digits. Now i have the following simple code, which stores the floats in memory and prints them.
#include <stdio.h>
#include <stdlib.h>
struct coordinates {
float x;
float y;
};
typedef struct coordinates coords;
int main(int argc, char** argv) {
unsigned int N, i;
coords *points;
FILE *fp;
if (argc != 2) {
printf("Usage: ./bigCircle <input_file>\n");
exit(0);
}
fp = fopen(argv[1], "r");
N = fscanf(fp, "%u", &N);
points = (coords *)malloc(N*sizeof(coords));
for (i=0; i<N; i++) {
fscanf(fp, "%f %f", &points[i].x, &points[i].y);
printf("%f %f\n", points[i].x, points[i].y);
}
return 0;
}
The problem is that the floats printed have far more non-zero decimal digits and become the right ones only when rounded to the second decimal place. For example, for the pair
99999.72 -50167.43
my program prints
99999.718750 -50167.429688
Why is this happening? Why don't i get
99999.720000 -50167.430000
?