I have two programs written in C++, say, calc1
and calc2
, which produce results x1
and x2
, respectively. They could be float
or double
.
I mean to write x1
and x2
to file, and read them with another program also in C++, my_oper
, which operates with x1
and x2
.
I need to keep the full precision that C++ uses for their variables, exactly as if all were done in a single program.
I also mean not to use text files (I do not care about the files being human-readable), so as to reduce the disk space and read/write time.
What should I use for writing and reading?
Question How do I print a double value with full precision using cout? does not fully address the present OP.
Major issue #1: Solutions posted aim at human-readable forms of output, so they do not care about saving disk space. That is an essential part of the question here.
Major issue #2: It does not seem to deal with the whole range of doubles (~ 2^-53 - 2^+53, plus exponents). See snippet below.
And even if it did, issue 1 above already states a different purpose.
Note that if the result from the exact arithmetic is x1=0.1 (e.g.), but the value stored is something slightly different x1* (say, in the 7th significant digit for a float), I want to write and then read x1*, I don't care about x1.
The following snippet
int iexpbase = 0;
fstream file;
file.precision(dbl::max_digits10);
file.precision(17);
file.open("math_precision_io.dat", fstream::out);
for (iexpbase = -100 ; iexpbase <= 100 ; iexpbase+=10) {
double d2 = pow(10, iexpbase);
file << fixed << d2 << endl;
}
file.close();
produces this output
0.00000000000000000
0.00000000000000000
0.00000000000000000
0.00000000000000000
0.00000000000000000
0.00000000000000000
0.00000000000000000
0.00000000000000000
0.00000000000000000
0.00000000010000000
1.00000000000000000
10000000000.00000000000000000
100000000000000000000.00000000000000000
1000000000000000019884624838656.00000000000000000
10000000000000000303786028427003666890752.00000000000000000
100000000000000007629769841091887003294964970946560.00000000000000000
999999999999999949387135297074018866963645011013410073083904.00000000000000000
10000000000000002257809904681181209619930771246774403173921223738392576.00000000000000000
100000000000000013190646323278015613777155861640004848960018902522128665705185280.00000000000000000
1000000000000000192640537007097094236487266187713695528897613397388406395002972996141842432.00000000000000000
10000000000000002101697803323328251387822715387464188032188166609887360023982790799717755191065313280.00000000000000000