Re: float to string to float, with first float == second float
On Oct 6, 1:39 pm, Rune Allnor <all...@tele.ntnu.no> wrote:
On 6 Okt, 11:16, Carsten Fuchs <CarstenFu...@T-Online.de> wrote:
I would like to serialize a float f1 to a string s, then
unserialize s back to a float f2 again, such that:
* s is minimal (use only the precision that is required)
and preferably in decimal notation,
* f1==f2 (exact same value after the roundtrip)
(The first property is for human readers and file size, the
second is for data integrity.)
You should choose between *either* human readability *or* data
integrity. You can't have both.
Sure you can. You just have to define "human readability" and
"data integrity" appropriately, then write the correct code. In
practice, just outputting with seven digits precision will
fulfill both definitions most of the time.
[...]
Some arguments why *not* to do what you want:
1) If data integrity is a priority, store the data on a
binary file format: While imperfect, the binary format
is consistent. Of course, if the usual number formats
are unacceptable to you, you could use some binary encoded
decimal format.
Before starting to state what should be done, define "data
integrity". The original poster did, and for his definition, it
can be proven that seven decimal digits are sufficient for an
IEEE float.
--
James Kanze