1

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?

9
  • printf with %.xf Commented Apr 4, 2019 at 17:17
  • 4
    You may find these three links of interest: 1) Floating-Point Determinism , 2) What Every Computer Scientist Should Know About Floating-Point Arithmetic , 3) Is floating point math broken? . Commented Apr 4, 2019 at 17:19
  • 2
    Floating-point math is all about being fast. Getting precisely specified results isn't fast. Java tried to require all implementations to use IEEE 64-bit floating-point for double; 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. Commented Apr 4, 2019 at 17:22
  • 1
    @Erel I don't think it's guaranteed cross platform to get consistent results, but that finally depends on how floating point values are represented with the underlying FPU architecture. Conclusion: Don't rely on consistent FP representations ever. Use epsilon approximation instead. Commented Apr 4, 2019 at 17:31
  • Is this question just about whether cout is guaranteed to print the exact same float in the exact same way every time on all these different systems, or does the question include whether x will 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 how x is computed. However, I can already tell you that, in that case, the answer is almost certainly: no. Commented Apr 4, 2019 at 17:33

1 Answer 1

2

Is the output guaranteed to be the same in all the above combinations?

No.

Very little about floating point representation is guaranteed by the C++ standard.

can I guarantee identical output by lowering the precision, e.g:

cout << setprecision(4) << x;

No.

If the accurate result of the calculation would be just at the border of rounding direction, then an arbitrarily small difference between the calculation could change the result. Rounding would magnify the difference between the calculations.

If not, what more can I do to have consistent output across machines?

  • Use something other than hardware floating point. Either software floating point or fixed point arithmetic.
  • Either use fixed width integers to implement that arithmetic so that one system doesn't have more precision than another, or use arbitrary precision.
  • If not using arbitrary precision, then only use expressions whose order of evaluation is defined by the language, so that differences between compiler don't result in different amounts of error.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.