tl;dr
I'm just looking for two functions, f
from double
to string
and g
from string
to double
, such that g(f(d)) == d
for any double d
(scalar and real double
).
Original question
How do I convert a double
to a string
or char
array in a reversible way? I mean, in such a way that afterward I can convert that string
/char
array back to double
retrieving the original result.
I've found formattedDisplayText
, and in some situations it works:
>> x = eps
x =
2.220446049250313e-16
>> double(formattedDisplayText(x, 'NumericFormat', 'long')) - x
ans =
0
But in others it doesn't
x = rand(1)
x =
0.546881519204984
>> double(formattedDisplayText(x, 'NumericFormat', 'long')) - x
ans =
1.110223024625157e-16
As regards this and other tools like num2str
, mat2str
, at the end they all require me to decide a precision, whereas I would like to express the idea of "use whatever precision is needed for you (MATLAB) to be able to read back your own number".