I have a file named "Q.csv" that is a text file containing:
-4.999118817962964961e-07 1.500000000000000197e-08 9.151794806638024753e-08 9.151794806638024753e-08
I'd like to read this as a 2x2 matrix into a C program. Currently my program looks like this.
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE* inf;
inf = fopen("Q.csv","r");
int nrows = 2;
int ncols = 2;
double grid[nrows][ncols];
double t;
size_t x, y;
for (x=0; x<ncols; ++x){
for(y=0; y< nrows; ++y){
fscanf(inf,"%lg ",&t);
grid[x][y]=t;
printf("%lg\n",grid[x][y]);
}
}
fclose(inf);
}
This program works. But it doesn't output all of the precision that was originally in Q.csv - for example, it outputs the first number as -4.99912e-07. If I take away the %lg and change it to %.18g, for example, it outputs the wrong thing. Same if I put %.18lg. In fact, both of these result in the mystery number 4.94065645841246544e-324 repeated (where is this coming from?!)
What can I do to get all of the precision in the file into C?