Bug#906708: Bug#906753: Acknowledgement (GCC's -O2 optimization breaks floating point precision or something else)

Lumin cdluminate at gmail.com
Mon Aug 20 20:53:55 BST 2018


This is the minimal code for repro #906753:

OK with -O0, FAIL with -O2 on i386, ppc64el, ...

masssq1 and masssq2 are computed from the same vector [1.1, 2.2, 3.3, 4.4],
but the results are different!

==========================================================

#include <cmath>
#include <algorithm>	// for swap
#include <iostream>

namespace HepMC {
class FourVector {
public:
  double m_x, m_y, m_z, m_t;
  FourVector( double xin, double yin, double zin, double tin=0) : m_x(xin), m_y(yin), m_z(zin), m_t(tin) {}
  inline double m2() const {
    return m_t*m_t - (m_x*m_x + m_y*m_y + m_z*m_z);
  }
  inline double m() const {
    double mm = m2();
    return mm < 0.0 ? -std::sqrt(-mm) : std::sqrt(mm);
  }
};
} // HepMC

int main()
{
  double eps = 1.e-15; // allowed differnce between doubles

  // FourVector
  HepMC::FourVector vector(1.1,2.2,3.3,4.4);
  HepMC::FourVector v4    (1.1,2.2,3.3,4.4);

  //vector = v4;

  double masssq1 = v4.m2();
  double mass1 = v4.m();
  double masssq2 = vector.m2();
  double mass2 = vector.m();

  if( fabs( masssq1 - masssq2 ) > eps ) {
     std::cout << "different mass sq values: " << masssq1 << " " << masssq2 << std::endl;
     std::cout << "difference is : " << ( masssq1 - masssq2 ) << std::endl;
  }

  return 0;
}



More information about the debian-science-maintainers mailing list