I have tested my code developed on a ubuntu 18.04 bionic docker image on a ubuntu 20.04 focal docker image. I saw that there were a problem with my unit test and I have narrowed the root cause to a simple main.cpp
#include <iostream>
#include <iomanip>
#include <math.h>
int main()
{
const float DEG_TO_RAD_FLOAT = float(M_PI / 180.);
float theta = 22.0f;
theta = theta * DEG_TO_RAD_FLOAT;
std::cout << std::setprecision(20) << theta << ' ' << sin(theta) << std::endl;
return 0;
}
On the bionic docker image, I have upgraded my version of g++ using the commands :
sudo apt-get install -y software-properties-common
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt install -y gcc-9 g++-9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 --slave /usr/bin/g++ g++ /usr/bin/g++-9 --slave /usr/bin/gcov gcov /usr/bin/gcov-9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70 --slave /usr/bin/g++ g++ /usr/bin/g++-7 --slave /usr/bin/gcov gcov /usr/bin/gcov-7
My version of g++ are the same : 9.4.0.
On ubuntu 18.04, the program outputs : 0.38397243618965148926 0.37460657954216003418
On ubuntu 20.04, the program outputs : 0.38397243618965148926 0.37460660934448242188
As you can see the difference is on the sin(theta), on the 7th decimal. The only difference I can think of is the version of libc which is 2.27 on the ubuntu 18.04 and 2.31 on the ubuntu 20.04.
I have tried several g++ options -mfpmath=sse, -fPIC,-ffloat-store, -msse, -msse2
but it had no effects.
The real problem is that on my Windows version of the code compiled with /fp:precise
,
I get the same results than the Ubuntu 18.04 :
0.38397243618965148926 0.37460657954216003418
Is there any way to force the g++ compiler to keep the same results as my Windows compiler please?