I have a very simple example of a C++ program that when compiled and executed produces a different result between MS C++ and GCC
#include <iomanip>
#include <iostream>
#include <limits>
#include <math.h>
int main()
{
double arg = -65.101613720114472;
double result = std::exp2(arg);
std::cout << std::setprecision(std::numeric_limits<double>::max_digits10) << result << std::endl;
return 0;
}
On Windows I compile this with Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29334 for x64
using the command cl example.cpp
and running the resultant executable produces 2.5261637809256962e-20
as text output.
On Linux I compile this with gcc version 9.3.0 (Ubuntu 9.3.0-10ubuntu2)
using the command g++ example.cpp
and running the executable produces 2.5261637809256965e-20
as text output. Notice the difference in the least significant digit
I have tried compiling 32/64 bit versions, different levels of optimisation, on Windows I have tried different values for the /fp: flag and on Linux the -frounding, -mfpmath, etc flags nothing has produced results different from the ones above.
Is it possible to get my program above to produce the same result from the std::exp2()
call on both Windows and Linux?
If not, where is the difference happening? Is it something specific to the compiler implementations, or the implementations of the math.h library, or is it some other subtlety I'm missing?
EDIT: Just to clarify;
This is not a question about the text representation of doubles.
The bitwise representation of the arg double is identical on both platforms. The bitwise representation of the result double is different at the least significant bit between the two platforms. The Windows and Linux executables were both run on the same machine and hence on the same processor architecture.
My best guess is that the implementation of the exp2() function performs some calculations in a slightly different order between the two platforms either because of compiler optimisations or math.h implementation differences.
If anyone knows where the implementation difference is or if it is possible to use compiler flags during either compilation to make them match, that would be the answer to this question.