读取浮点值而不会丢失精度

时间:2017-04-21 00:32:04

标签: c

我有一个包含浮点数值的数据集((12008 * 12008)矩阵,存储为csv格式),如:

0.00000000000000000129097521168892,0.00000822381709160264,-0.0000202130952444797,-0.0000242966927641499,0.000030546233920949,0.0000371753288365733,-0.00000425477255333099,0.0000417806937053693,-0.0000108963812606789,-0.0000116825931954974,0.00000630880107874073,-0.0000148294201214316,0.000015379692507603,0.00000188679818192867,0.00000244261959932239,-0.00000000000000000151132910441927,-0.0000219241408193178,0.000017429377110585,0.00000870784208574737,0.0000364006097348257,0.0000113488247245831,0.000000061277075989572,0.00000139732542828701,0.000000106947553933263,0.0000326417023213338,0.000000461629854407632,0.0000154355086839126,-0.00000952557006126632,-0.00000661974638741755,0.0000100492673229086,-0.00000234761929597247,0.000000562031028984242,-0.0000111189586356939,0.0000147613296909272,-0.0000412808264307332,0.0000144980218289582,-0.00000396573532860471,0.00000284216015813941,-0.000000340292198448977,0.000000000000000000673559708847384,-0.000000000000000000477244282125147,0.0000295672329145256,0.000000265057585538249,-0.0000259880981140332,-0.000000655572300400449,0.0000275203484322834,0.00000939709816031401,-0.000000722013848489603,-0.000000131975990695569,-0.0000305715257167805,0.0000166359409438876,-0.0000108549024616569,0.0000035413589251256,0.0000134733827428785,0.00000033136969072632,-0.0000000227012317723535,0.0000251344669810146,0.00000507204772254958,-0.00000000193093573409854,

我注意到当我把它读到一个浮点缓冲区时,我的精度太高了,所以得到类似的东西:

0.0000000
-0.000082

我想在我的程序中读取数据集中的确切值而不会丢失精度到缓冲区。我使用以下小程序来阅读,但我不知道如何修改它以便它不会失去精度:

float *data = malloc((12008 * 12008) * sizeof(float));
FILE *fp ;
fp=fopen("./eig_matrix.csv","rb");
if(fp!= NULL) fread(data, (12008 * 12008)*sizeof(float),1,fp);
fclose(fp);

1 个答案:

答案 0 :(得分:1)

float类型是C中浮点数据类型中最不精确的类型。它们是ranked,如下所示:

  • float
  • double
  • long double

每种类型的长度(以字节为单位)根据特定的C实现而不同;要理解的主要是每种类型都有精度限制,更精确的类型会占用更多空间。选择更精确的数据类型可能足以满足您的需要。

用二进制数完全捕获小数精度是不可能的。十进制表示法中存在某些确切的数字,它们是二进制表示法中的无限重复数字(例如0.2),并且打印函数通常仅显示直到某个小数位的数字。这些组合可能意味着数字可以使用比原始数字精度更少或更多的小数位显示。

如果需要精确控制数字的小数精度(达到给定的程度),则可以使用fixed-point notation。这通常通过将整数值与已知(和固定)缩放因子一起包含来完成,但需要修改算术运算以考虑缩放因子。