Rohan G Rohan G - 1 month ago 6
C Question

read the data from file (precision to 15 decimal places) in C

My text file which needs to be read is in following format (data.dat) -

0.001505882352941

34.900000000000018

121.800000000000010

i want to assign the following values to given buffer which is of type float and constraint is that i cannot change the data type of buffer

fprintf(file_ptr, "%f", &buffer);


i tried other methods also

double val;
fprintf(file_ptr,"%lf", &val);
buffer = (float *) val;


but i am not getting proper values

i am running this program on cygwin window

values after decimal is always constant in number till 15 places

Answer

which is of type float and constraint is that i cannot change the data type of buffer

Then you've got a problem.

values after decimal is always constant in number till 15 places

That's about the number of significant digits a double precision float can hold. There are 53 bits available for the mantissa in a IEEE 754 double precision floating point value. That gives you log10(2^53) = 15.95 decimal digits of precision (round that down to 15). That's what it can hold.

If you need higher precision you must use a different data type possibly combined with using a multiprecision math library.

Comments