Public bug reported:

If I compile the code using g++ and -O2 optimization and try the algorithm
with this input "10000 100 10" I get a wrong answer.
I can get the correct solution if I compile the same code don't using any 
optimization.


#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cmath>

using namespace std;

#define DEBUG

struct Point
{
   double x, y;
   Point() : x(0), y(0) {}
   Point(const double _x, const double _y) : x(_x), y(_y) {}
};

struct Retta
{
   double x, y, z;
   Retta() : x(0), y(0), z(0) {}
   Retta(const Point& p1, const double m)
   {
      // y - y1 = m(x - x1)
      x = m;
      y = -1;
      z = p1.y-m*p1.x;
   }
   Retta(const Point& p1, const Point& p2)
   {
      if (p1.x == p2.x)
      {
         x = 1;
         y = 0;
         z = -p1.x;
      }
      else if (p1.y == p2.y)
      {
         x = 0;
         y = 1;
         z = -p1.y;
      }
      else
      {
         double m = (p1.y-p2.y)/(p1.x-p2.x);
         x = m;
         y = -1;
         z = p1.y-m*p1.x;
      }
   }
};

Point p1, p2, p3;
Retta r1, r2;

const double EPS = 1E-6;

int solve()
{
   int sols = 0;
   double x1, x2;
   for (int i = 1; i < p2.y; i++)
   {
      x1 = ((-r1.y*i-r1.z)/r1.x);
      x2 = ((-r2.y*i-r2.z)/r2.x);


      x1 = (ceil(x1) == x1 ? x1+1 : ceil(x1));
      x2 = (floor(x2) == x2 ? x2-1: floor(x2));

      sols += static_cast<int>(x2-x1+1);
      #ifdef DEBUG
         printf("level %d = [%lf, %d] [%lf, %d]  D = %lf [%d]\n", i, x1, i, x2, 
i, x2-x1+1, sols);
      #endif
   }   
   return sols;
}

void read(FILE* in)
{
   int a, b, c;
   fscanf(in, "%d%d%d", &a, &b, &c);
   p1 = Point(0, 0);
   p2 = Point(a, b);
   p3 = Point(c, 0);   
   r1 = Retta(p1, p2);
   r2 = Retta(p3, p2);
   #ifdef DEBUG
      printf("R1: %lf x + %lf y + %lf = 0\n", r1.x, r1.y, r1.z);
      printf("R2: %lf x + %lf y + %lf = 0\n", r2.x, r2.y, r2.z);
   #endif
}

int main()
{
   FILE* in = fopen("fence9.in", "r");
   FILE* out = fopen("fence9.out", "w");
   #ifdef DEBUG
      in = stdin;
      out = stdout;
   #endif
   read(in);
   fprintf(out, "%d\n", solve());
   return 0;
}

** Affects: Ubuntu
     Importance: Undecided
         Status: Unconfirmed

-- 
Seem optimization -O2 don't work g++
https://launchpad.net/bugs/82190

-- 
ubuntu-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to