C中从文件中读取数据(精确到小数点后15位)
read the data from file (precision to 15 decimal places) in C
我需要读取的文本文件格式如下 (data.dat) -
0.001505882352941
34.900000000000018
121.800000000000010
我想将以下值分配给给定的 float 类型的缓冲区,约束是我不能更改缓冲区的数据类型
fprintf(file_ptr, "%f", &buffer);
我也试过其他方法
double val;
fprintf(file_ptr,"%lf", &val);
buffer = (float *) val;
但我没有得到正确的值
我是 运行 这个在 cygwin 上的程序 window
小数点后的值在 15 位之前始终为常数
which is of type float and constraint is that i cannot change the data type of buffer
那你就有问题了
values after decimal is always constant in number till 15 places
这是一个双精度浮点数可以容纳的有效数字位数。 IEEE 754 双精度浮点值中的尾数有 53 位可用。这给你 log10(2^53) = 15.95 个十进制数字的精度(四舍五入到 15)。这就是它所能容纳的。
如果您需要更高的精度,您必须使用不同的数据类型,并可能结合使用多精度数学库。
我需要读取的文本文件格式如下 (data.dat) -
0.001505882352941
34.900000000000018
121.800000000000010
我想将以下值分配给给定的 float 类型的缓冲区,约束是我不能更改缓冲区的数据类型
fprintf(file_ptr, "%f", &buffer);
我也试过其他方法
double val;
fprintf(file_ptr,"%lf", &val);
buffer = (float *) val;
但我没有得到正确的值
我是 运行 这个在 cygwin 上的程序 window
小数点后的值在 15 位之前始终为常数
which is of type float and constraint is that i cannot change the data type of buffer
那你就有问题了
values after decimal is always constant in number till 15 places
这是一个双精度浮点数可以容纳的有效数字位数。 IEEE 754 双精度浮点值中的尾数有 53 位可用。这给你 log10(2^53) = 15.95 个十进制数字的精度(四舍五入到 15)。这就是它所能容纳的。
如果您需要更高的精度,您必须使用不同的数据类型,并可能结合使用多精度数学库。