Saving variables to file with desired precision

12 visualizaciones (últimos 30 días)
John
John el 19 de Mzo. de 2014
Editada: José-Luis el 19 de Mzo. de 2014
Suppose I want to save the value of a variable, say sqrt(2), to a file, keeping a given number of digits, for example 30 digits, and then I want to load that value again in workspace, without any loss of precision, which is the correct way to achieve this in Matlab?

Respuestas (1)

José-Luis
José-Luis el 19 de Mzo. de 2014
Editada: José-Luis el 19 de Mzo. de 2014
A double might not have sufficient capacity to store 30 digits of precision. It only has the capacity for around 15 decimal digits. Please read the article about doubles on wikipedia if you want to understand better.
Here is an illustration of their limitation:
a(1) = 10^-40;
a(2) = 1234567890.12345678901234567890;
fid = fopen('test.txt','w');
fprintf(fid,'%30.30f \n',a); %floating point with a width and precision of 30 numbers
fclose(fid);
This might be an interesting read as well.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by