> For best portability, convert your numbers to text format. Text format
usually is independent of machine characteristics; including when the
compiler libraries or OS libraries change.
Try reading these sections of the FAQ:
http://www.parashift.com/c++-faq-lit...alization.html
http://www.parashift.com/c++-faq-lit...html#faq-29.16
http://www.eskimo.com/~scs/c-faq/s14.html
http://groups.google.com/groups?hl=e....c%252B%252B.*
If you are storing the double variables and retrieving them without
any conversion, there _should_ be no loss of precision. But then
there are many numbers that cannot be precisely represented by
a fixed real number format.
Also search the web for "IEEE floating point format".
Portability is one of the reason I'm using text streams. (Other reasons
are transparency and clarity.) So I thought that that was what I was doing.
It appears to me that I am in fact getting an accuracy loss in this
process. This program appears to demonstrate that this occurs:
#include <iostream>
#include <sstream>
#include <cmath>
int signed main() {
double sqrt2=std::sqrt(2.0);
std::ostringstream ot;
ot << sqrt2;
std::istringstream it(ot.str());
double sqrt2io;
it >> sqrt2io;
std::cout << (sqrt2-sqrt2io) << std::endl;
}
// Output on my machine (GCC 3.2.2, RH Linux 9) is:
// 3.56237e-06
// I would expect 0 if the same number was being read.
But this is the naive approach to it and perhaps I'm missing some
subtleties. (If so, I would love to be enlightened.)
On a side note, I _know_ I read the faq cover to cover, but I'm positive
this serialization stuff wasn't in there. I assume it was added since
(about six years ago), which I suppose is good, meaning it's adapting to
new needs. I guess I better rebrowse it.
But it doesn't look like the faq (or at least this page of it) addresses
my precision issues.
Anyway, thank you for your time to respond.
Adam H. Peterson