I am currently porting some codes from Windows to Linux (Ubuntu) from a MS VS2008 compiler to GCC.
The problem is that the following code has different results:
double d = 19.53125;
std::cout.precision(4);
std::cout.setf(std::ios::fixed);
std::cout << d << std::endl;
Windows output: 19.5313
Linux output: 19.5312
If I set the precision to 5 the output is the same.
I expect that 19.53125 will be rounded up to 19.5313.
Any suggestions how I can get the desired rounding behavior?
Note: Windows code runs native on Windows 10 laptop and the Linux code runs inside a 64-bit Ubuntu 18.04 LTS VM.
Bankers Rounding is an algorithm for rounding quantities to integers, in which numbers which are equidistant from the two nearest integers are rounded to the nearest even integer. Thus, 0.5 rounds down to 0; 1.5 rounds up to 2.
GCC C++ standard library applies the Bankers Rounding. 19.53125 -> 19.5312, since 2 is the nearest even digit.
Additional info regarding C, but C++ respects C rounding: C++ Rounding behavior consistency for ties with sprintf