Thread overview | |||||
---|---|---|---|---|---|
|
May 19, 2014 Write double to text file, then read it back later without losing precision | ||||
---|---|---|---|---|
| ||||
I would like to write a double to a text file as hexadecimal and then read it back in without losing information. Could someone tell me whether this is possible? It would happen in the same program, would I have to worry about different architectures? Thanks very much Andrew |
May 19, 2014 Re: Write double to text file, then read it back later without losing precision | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Brown | Andrew Brown:
> I would like to write a double to a text file as hexadecimal and then read it back in without losing information.
Is this good enough for you?
void main() {
import std.stdio, std.math;
auto fout = File("ouput.txt", "w");
fout.writef("%a", PI);
fout.close;
auto fin = File("ouput.txt");
real r;
fin.readf("%f", &r);
fin.close;
assert(PI == r);
}
Bye,
bearophile
|
May 19, 2014 Re: Write double to text file, then read it back later without losing precision | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | I'm sure it will be, thank you very much.
On Monday, 19 May 2014 at 15:57:53 UTC, bearophile wrote:
> Andrew Brown:
>
>> I would like to write a double to a text file as hexadecimal and then read it back in without losing information.
>
> Is this good enough for you?
>
> void main() {
> import std.stdio, std.math;
>
> auto fout = File("ouput.txt", "w");
> fout.writef("%a", PI);
> fout.close;
>
> auto fin = File("ouput.txt");
> real r;
> fin.readf("%f", &r);
> fin.close;
> assert(PI == r);
> }
>
> Bye,
> bearophile
|
Copyright © 1999-2021 by the D Language Foundation