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

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Auradrummer
Posts: 260
Joined: Thu Apr 17, 2008 1:38 pm
Location: Brasopolis - Brazil

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

Post 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!
Last edited by Auradrummer on Wed Feb 14, 2018 12:46 pm, edited 1 time in total.
Professional Software Developer and Amateur Game Designer ;-)
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

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

Post by hendu »

Float comparison, rounding error. You must never compare floats with ==, they might differ by 0.0000001.
Auradrummer
Posts: 260
Joined: Thu Apr 17, 2008 1:38 pm
Location: Brasopolis - Brazil

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

Post by Auradrummer »

Oh man, should be just this? I'll make a try ...
Professional Software Developer and Amateur Game Designer ;-)
Auradrummer
Posts: 260
Joined: Thu Apr 17, 2008 1:38 pm
Location: Brasopolis - Brazil

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

Post 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!!!!
Professional Software Developer and Amateur Game Designer ;-)
devsh
Competition winner
Posts: 2057
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

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

Post 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.
Post Reply