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;
}