0

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
  • Just replace `cout` in the dupe target with `name_of_fle_stream` and you're all set. – NathanOliver Aug 08 '18 at 12:53
  • "What should I use for writing and reading?" the same size of variable for each I'd have thought would be a good start – UKMonkey Aug 08 '18 at 12:54
  • What is the other program? Which programming language is it written? What is wrong with simply writing float/double as byte buffers? If the other program can interpret it, then that's the solution. – geza Aug 08 '18 at 16:23
  • Another possibility is to use `printf("%a", num);` (maybe there is something similar in iostream as well). – geza Aug 08 '18 at 16:31
  • @NathanOliver - Please see edit. I guess that shows I'm not all set. – sancho.s ReinstateMonicaCellio Aug 09 '18 at 01:53
  • @UKMonkey - That is what I think it should be done, and I guess it should be simple. But googling variations of the title of the OP barely brought links actually pointing at the topic, so I though there would be more to it. In the meantime, I was working on this. – sancho.s ReinstateMonicaCellio Aug 09 '18 at 01:55
  • @geza - What is the other program? Another program (I wouldn't know how else to answer this). Which programming language is it written? C++. What is wrong with simply writing float/double as byte buffers? See my answer to comment by UKMonkey. – sancho.s ReinstateMonicaCellio Aug 09 '18 at 01:57
  • @geza - `printf("%a", num)`-style is what I think should work. Definitely not the proposed dupe target. – sancho.s ReinstateMonicaCellio Aug 09 '18 at 02:01
  • If the other program is C++ as well, then a simple `float a = ...; fwrite(&a, 1, sizeof(a), f);` should work. That's the fastest and simplest solution. – geza Aug 09 '18 at 06:01

0 Answers0