My C++ program calculates some floating-point number x and prints it:
cout << x;
It is important to me that the output be the same in all computers. Since "all" is very broad, let's focus on the following:
- different compilers from among g++ 6/7/8, clang++ 5/6;
- different Linux systems - Ubuntu 16.04/18.04;
- stand-alone Linux vs. Windows-subsystem Linux;
- different hardware - Intel vs. AMD with different specs.
Is the output guaranteed to be the same in all the above combinations?
If not, can I guarantee identical output by lowering the precision, e.g:
cout << setprecision(4) << x;
? If not, what more can I do to have consistent output across machines?
printfwith %.xfdouble; serious users rebelled because it was so slow (Intel hardware is tuned for 80-bit calculations, and 64-bit doubles are widened to 80 bits for calculations), and Java now has "strict" math that nobody uses, and ordinary math which serious FP folks use.coutis guaranteed to print the exact samefloatin the exact same way every time on all these different systems, or does the question include whetherxwill be computed to come out as exactly the same value on all of them? If it's about the latter, we'll have to know howxis computed. However, I can already tell you that, in that case, the answer is almost certainly: no.