http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55787



             Bug #: 55787

           Summary: Comparisons of double values don't work correctly

    Classification: Unclassified

           Product: gcc

           Version: 4.7.2

            Status: UNCONFIRMED

          Severity: critical

          Priority: P3

         Component: c++

        AssignedTo: unassig...@gcc.gnu.org

        ReportedBy: ta...@hm.edu





Created attachment 29027

  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=29027

example code (as .ii)



A simple comparison shows that floating point operations with the c++-type

double doesn't set the floating status flag correctly.



Changing the type of the members of the class Load to float or long double

makes the application work correctly.



In the attached example the free function 

bool operator<(const Load &, const Load &); 

doesn't work correctly.



The example returns the output:

Compare: true

instead of

Compare: false



This example was compiled in different configurations and on different machines

with the same wrong result:



System configuration 1:

---

Intel(R) Core(TM)2 CPU 6420 @ 2.13GHz

3 GB RAM (pae)

Windows XP Professional Version 2002 SP3

32 bit arch

---

Es werden eingebaute Spezifikationen verwendet.

COLLECT_GCC=g++

COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.7.2/lto-wrapper.exe

Ziel: mingw32

Konfiguriert mit: ../gcc-4.7.2/configure

--enable-languages=c,c++,ada,fortran,objc,obj-c++ --disable-sjlj-exceptions

--with-dwarf2 --enable-shared --enable-libgomp --disable-win32-registry

--enable-libstdcxx-debug --disable-build-poststage1 -with-cxx

--enable-version-specific-runtime-libs --build=mingw32 --prefix=/mingw



Thread-Modell: win32

gcc-Version 4.7.2 (GCC)

---

The build process call was:

g++ -v -save-temps -Wall -pedantic -Wextra -o derror.exe src\derror.cpp



--- derror.cpp ---

// test routine to show the double error

#include <iostream>

#include <stdexcept>

using namespace std;



// class definition

class Load

{

    double m_dPowerOut, m_dEfficiency;

public:

    Load(double powerOut, double eff=1) :

      m_dPowerOut(powerOut), m_dEfficiency(eff) { };

      virtual double consumption() const;

      virtual ~Load() { }

};



bool operator<(const Load &a, const Load &b);



// class method definition

double Load::consumption(void) const

{

    if (m_dEfficiency==0.0)

            throw logic_error("Efficiency must not be 0");

    return m_dPowerOut/m_dEfficiency;

}



/// free function

// ... doesn't work correctly

bool operator<(const Load &a, const Load &b)

{

    return a.consumption() < b.consumption();

}

/*

// ... seems to work correctly, but the float-status-flag IMHO 

//     still isn't correct

bool operator<(const Load &a, const Load &b)

{

    double bcon=b.consumption();

    return a.consumption() < bcon;

}

*/



int main()

{

    try

    {

        Load pump(53.0, 0.43);

        bool cmp=(pump < pump);  // should be false !!!

        cout << "Compare: " << (cmp ? "true" : "false") << endl;

    }

    catch (const exception &ex)

    {

        cerr << "Exception: " << ex.what() << endl;

    }

    return 0;

}

Reply via email to