0

I wrote some code in coding coding competition. I use gcc-4.6.3 Linaro. When i use command:

 g++ main.cpp -x c++ -O2 -o binary

Output of program - "NO"

g++ main.cpp -x c++ -o binary

Without optimization - "YES"

I test this code on online compilers: 1) http://ideone.com/lv8OZs 2) http://coliru.stacked-crooked.com/ These compilers are not too old, but situation not changed. On http://ideone.com/qqHbLO i uncomment one output-stream string - and after compilation with optimization result was changed to "YES".

Is it a bug of gcc? May be i don't understand something about optimization. Can anybody explain me?

Code /* it's useless example code */

#include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>

using namespace std;
int main() {
        int x1=0;
        int y1=0;
        int x2=1;
        int y2=1;
        int x3=0;
        int y3=1;
        int x4=1;
        int y4=0;

        std::vector<double> vec;
        vec.push_back(sqrt((double)((x2- x1)*(x2-x1) + (y2-y1)*(y2-y1))));
        vec.push_back(sqrt((double)((x2- x3)*(x2-x3) + (y2-y3)*(y2-y3))));
        vec.push_back(sqrt((double)((x4- x3)*(x4-x3) + (y4-y3)*(y4-y3))));
        vec.push_back(sqrt((double)((x4- x1)*(x4-x1) + (y4-y1)*(y4-y1))));
        vec.push_back(sqrt((double)((x4- x2)*(x4-x2) + (y4-y2)*(y4-y2))));
        vec.push_back(sqrt((double)((x3- x1)*(x3-x1) + (y3-y1)*(y3-y1))));

        std::sort(vec.begin(),vec.end());


        if(vec[0]==vec[3])
        {   
            std::cout<<"Test"<<std::endl;
            double sqrt1 = sqrt(vec[0]*vec[0]+vec[1]*vec[1]);
            double sqrt2 = sqrt(vec[2]*vec[2]+vec[3]*vec[3]);
            /*
            std::cout<<"vec[4] ="<<vec[4]<<std::endl;
            std::cout<<"sqrt1 = "<<sqrt1<<std::endl;
            */
            if(vec[4]==sqrt1 && vec[5] == sqrt2)
            {   
                std::cout<<"YES"<<std::endl;
                return 0;
            }   
        }   

        std::cout<<"NO";
        return 0;
}
sepp2k
  • 363,768
  • 54
  • 674
  • 675
AlexBG
  • 386
  • 1
  • 7
  • 4
    Comparing floating point numbers for equality in this way rarely gives you the desired results. Taking different paths to the "same" number can easily give you slightly different results. You should generally compare to a reasonable degree of precision, not for absolute equality. – Crowman Apr 26 '14 at 17:25
  • 1
    Here is a good question that may explain what @PaulGriffiths means: http://stackoverflow.com/questions/17333/most-effective-way-for-float-and-double-comparison – Stefan Falk Apr 26 '14 at 17:27

1 Answers1

0

It would be a problem of comparing floating point numbers.

The comment section have two other SO question which explains clearly why that should not be done, and have lots of advice how to deal with comparing floating point numbers.

You can generally not compare floats/doubles with == as there may always be some rounding error depending on the order of the operations.

With the optimization, the operations are simply done in a different more efficient order, and that impact the rounding.

Soren
  • 14,402
  • 4
  • 41
  • 67