C++ Eigen::inverse function renders totally wrong array with ffast-math option

81 views Asked by At

In Ubuntu18.04, I got issue with eigen library with array inversion function, when compile with -O3 and -ffast-math option, the number in the even number of column have wrong sign for them.

The steps to re-create:

  1. docker run -v $PWD:/work -it ubuntu:18.04
  2. apt update
  3. apt install libeigen3-dev -y Install gcc version 10.3.0
  4. apt install software-properties-common -y
  5. add-apt-repository ppa:ubuntu-toolchain-r/test -y && apt update
  6. apt install gcc-10 gcc-10-base gcc-10-doc g++-10 -y
  7. Create the file using eigen, I use file name eigen_test.cpp:
#include <iostream>
#include <eigen3/Eigen/Eigen>
int main()
{
    Eigen::Matrix4d t;
    t << 1.0, -0.0, 0.0, -0.0,
         0.0, 1.0, -0.0, -0.0,
         0.0, 0.0, 1.0, -0.0,
         0.0, 0.0, 0.0, 1.0;
    std::cout << "Original matrix: \n" <<t << std::endl;
    std::cout << "Inversed matrix: \n" << t.inverse() << std::endl;
}
  1. Compile with ffast-math option:
$ g++-10 eigentest.cpp -O3 -ffast-math -o a.out
$ ./a.out
Original matrix: 
 1 -0  0 -0
 0  1 -0 -0
 0  0  1 -0
 0  0  0  1
Inversed matrix: 
 1 -0 -0  0
-0 -1 -0 -0
-0  0  1  0
-0 -0 -0 -1
  1. Re-compile without ffast-math option:
$ g++-10 eigentest.cpp -O3 -o a.out
$ ./a.out
Original matrix: 
 1 -0  0 -0
 0  1 -0 -0
 0  0  1 -0
 0  0  0  1
Inversed matrix: 
 1  0 -0  0
-0  1  0  0
-0 -0  1 -0
 0  0 -0  1

The output of two results are different, the one compiled with option: -ffast-math get -1 in the (2, 2) and (4, 4) position, and they are wrong, the correct value should be 1. Does anyone know why?

0

There are 0 answers