Page 1 of 1

Bizarre bug... printf making the diference! [SOLVED]

Posted: Wed Feb 14, 2018 1:28 am
by Auradrummer
Masters, me again,

Now, I have a question that some experienced programmers can help, and I don't know if is regarding Irrlicht or not. Take a look in this simple code:

Code: Select all

Data_path * Segment::get_data_path_for_triangle(triangle3df tri)
{
    track->updateAbsolutePosition();
    CMatrix4<f32> mat = track->getAbsoluteTransformation();
    for(u32 i = 0; i < path.size(); i++)
    {
        for(u32 j = 0; j < path[i]->tri.size(); j++)
        {
            vector3df v_a = path[i]->tri[j].pointA;
            vector3df v_b = path[i]->tri[j].pointB;
            vector3df v_c = path[i]->tri[j].pointC;
 
            mat.transformVect(v_a);
            mat.transformVect(v_b);
            mat.transformVect(v_c);
 
            triangle3df tri_p(v_a, v_b, v_c);
 
            printf("\t%5.2f %5.2f %5.2f\n\t%5.2f %5.2f %5.2f\n\t%5.2f %5.2f %5.2f\n\n",
                tri_p.pointA.X, tri_p.pointA.Y, tri_p.pointA.Z,
                tri_p.pointB.X, tri_p.pointB.Y, tri_p.pointB.Z,
                tri_p.pointC.X, tri_p.pointC.Y, tri_p.pointC.Z);
 
            if(tri_p.pointA.X == tri.pointA.X && tri_p.pointB.X == tri.pointB.X && tri_p.pointC.X == tri.pointC.X &&
               tri_p.pointA.Y == tri.pointA.Y && tri_p.pointB.Y == tri.pointB.Y && tri_p.pointC.Y == tri.pointC.Y &&
               tri_p.pointA.Z == tri.pointA.Z && tri_p.pointB.Z == tri.pointB.Z && tri_p.pointC.Z == tri.pointC.Z)
            {
                printf("^^^ ABOVE ^^^\n");
                return path[i];
            };
        };
    };
    return NULL;
};
The "data_path" structure is a simple structure that holds a vector of triangles. I send a triangle to this function and it returns the "data_path" it belongs to. Really not complicated.

This code works fine but, this is the annoying part, only if I let that "printf" there. If I remove the printf, the conditional stops working! I program in C++ over eight years and never heard something like that! I have a theory that is some typecasting involved that the printf is casting, but no conclusion at all. Someone saw anything like this before?

Thanks!

Obs.: I really get confused if I must ask for help from a programmer or a priest!

Re: Bizarre bug... printf making the diference!

Posted: Wed Feb 14, 2018 9:23 am
by hendu
Float comparison, rounding error. You must never compare floats with ==, they might differ by 0.0000001.

Re: Bizarre bug... printf making the diference!

Posted: Wed Feb 14, 2018 12:26 pm
by Auradrummer
Oh man, should be just this? I'll make a try ...

Re: Bizarre bug... printf making the diference! [SOLVED]

Posted: Wed Feb 14, 2018 12:47 pm
by Auradrummer
Ok hendu, worked and I'l really stuck with aproximation of floats before. But, WHY the printf was affecting the float precision? This is something that I cannot understand!!!!

Re: Bizarre bug... printf making the diference! [SOLVED]

Posted: Wed Feb 14, 2018 2:32 pm
by devsh
because your triangle corners are results of matrix multiplication, which means that every floating point entry equals something like x_p = Ax+By+Cz+D where xyz are input point coords, and ABCD come from the matrix.

Since the matrix is defined outside the loop and doesn't change, it gets treated as a constant.

if you do x_p == x then it can get optimized as (A-1)x + By+Cz+D == 0

Now there will be tremendous precision loss in such a comparison when A is not near to 1.